绑定下拉列表,当选中该行的复选框时,该下拉列表位于"OncheckedChange"事件的网格视图中


<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" CssClass="outdata">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Center" ItemStyle-CssClass="profiledata" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<input id="Checkbox2" type="checkbox" style="width: 50px !important;" onclick="CheckAll(this)" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" OnCheckedChanged="chkSelect_CheckedChanged" runat="server" />
<asp:HiddenField runat="server" ID="hdnAssid" Value='<%# Eval("Asset_ID") %>' />
</ItemTemplate>
<ItemStyle Width="5px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Stop" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlGrdStops"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

假设我在Gridview中有两列。1.复选框2.下拉列表

复选框的OnCheckedChange事件中的代码应该是什么?每当我选中它时,应该填充同一行的下拉列表。

我尝试了gridview的RowDataBound事件,但数据太大,所以处理起来需要很长时间,而且不必要地检查每一行的复选框。

既然我是新人,请帮忙。提前感谢。:(

您可以使用复选框的NamingContainer来定位同一行中的DropDownList。PS在复选框中将AutoPostBack设置为true。

protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
//cast the sender back to a checkbox
CheckBox cb = sender as CheckBox;
//get the current gridviewrow from the checkbox namingcontainer
GridViewRow row = cb.NamingContainer as GridViewRow;
//use findcontrol to locate the dropdownlist in that row
DropDownList ddl = row.FindControl("ddlGrdStops") as DropDownList;
//add the items to the dropdownlist
ddl.Items.Add(new ListItem() { Text = "DropDownList found", Value = "1" });
}

最新更新