请问我使用gridview dropdownlist 动态增加行没有问题,但是当我选取下拉列表的值以后,我再增加一行,下拉列表的值全部恢复为默认状态值,前台dropdownlist的代码:<asp:TemplateField HeaderText="单位">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
后台绑定代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.FindControl("DropDownList1") != null)
{
string Sqldropdownlist = "SELECT * FROM units";
DropDownList list = (DropDownList)e.Row.FindControl("DropDownList1");
list.DataSource = shujuku.GetDataSet(Sqldropdownlist);
list.DataTextField = "danwei";
list.DataValueField = "id";
list.DataBind();
list.SelectedValue = DataBinder.Eval(e.Row.DataItem,"id").ToString();
list.Items.Insert(0, new ListItem("请选择", "0"));
}
}
}
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
后台绑定代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.FindControl("DropDownList1") != null)
{
string Sqldropdownlist = "SELECT * FROM units";
DropDownList list = (DropDownList)e.Row.FindControl("DropDownList1");
list.DataSource = shujuku.GetDataSet(Sqldropdownlist);
list.DataTextField = "danwei";
list.DataValueField = "id";
list.DataBind();
list.SelectedValue = DataBinder.Eval(e.Row.DataItem,"id").ToString();
list.Items.Insert(0, new ListItem("请选择", "0"));
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货