在我的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
。
答案 0 :(得分:1)
将OnRowDataBound属性添加到.aspx页面中的gridview:
<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..
}
}
答案 1 :(得分: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;
}