0

在我的 GridView 中,我有以下列:

<Columns>  
    <asp:BoundField DataField="report_type" HeaderText="Report Type"   
        SortExpression="report_type" />  

    <asp:BoundField DataField="progress" HeaderText="Progress"   
        SortExpression="progress" />  

    <asp:TemplateField HeaderText="..">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="progress">
                <asp:ListItem Value="0">Incomplete</asp:ListItem>
                <asp:ListItem Value="1">Complete</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField> 
</Columns>

进度列仅用于演示目的,最终将被删除。如何获取进度值以在下拉列表中选择正确的项目列表?

因此,如果值为 ,progress则应选择1下拉菜单Complete。如果 的值为 ,progress则应选择0下拉菜单Incomplete

4

2 回答 2

1

在 .aspx 页面的 gridview 中添加 OnRowDataBound 属性:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="id" OnRowDataBound="GridViewRowEventHandler">

代替

<asp:BoundField DataField="Progress" HeaderText="Progress"   
    SortExpression="progress" /> 

<asp:TemplateField>
   <ItemTemplate>             
       <asp:Label ID="progress_Flags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Progress").ToString()%>'/>
  </ItemTemplate>                    
</asp:TemplateField>

在后面的代码中:

protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       Label flag = (Label)e.Row.FindControl("progress_Flags");
       DropDownList myDropDown = (DropDownList)e.Row.FindControl("DropDownList1");
        if (flag.Text == "1")
        {
            myDropDown.SelectedValue = "1";
        }
    //add more conditions here..

    }          
}
于 2012-08-31T14:00:26.650 回答
0

如果RowDataBound您可以使用e.Row.FindControl

protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{

   GridViewRow row = e.Row;
   DataRowView dr = row.DataItem as DataRowView;
   // now find the control in the row by control ID
   DropDownList myDropDown = row.FindControl("DropDownList1") as DropDownList;
}
于 2012-08-31T13:51:18.793 回答