gridview 中 编辑时添加了一个dropdownlist DropDownList ddlType=new DropDownList();
ddlType.ID = "ddlType";
ddlType.Items.Insert(0, new ListItem("是","0"));
ddlType.Items.Insert(1, new ListItem("否", "1"));
e.Row.Cells[4].Text = "";
e.Row.Cells[4].Controls.Add(ddlType);但是在更新的时候就是取不到这个dropdownlist
((DropDownList)(gvClass.Rows[e.RowIndex].FindControl("ddlType"))).SelectedValue
总是出错 : "未将对象引用设置到对象的实例。"
请问高手怎么办?
ddlType.ID = "ddlType";
ddlType.Items.Insert(0, new ListItem("是","0"));
ddlType.Items.Insert(1, new ListItem("否", "1"));
e.Row.Cells[4].Text = "";
e.Row.Cells[4].Controls.Add(ddlType);但是在更新的时候就是取不到这个dropdownlist
((DropDownList)(gvClass.Rows[e.RowIndex].FindControl("ddlType"))).SelectedValue
总是出错 : "未将对象引用设置到对象的实例。"
请问高手怎么办?
<asp:DropDownList ID="ddl" runat="server" Width="98%">
</asp:DropDownList>
</EditItemTemplate>
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
gv.EditIndex = e.RowIndex;
int id = int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
DropDownList ddl= gv.Rows[e.RowIndex].FindControl("ddl") as DropDownList;
gv.EditIndex = -1;
BindData();
}
看仔细啰。用这种方法找到DropDownList:
gv.Rows[e.RowIndex].FindControl(["XXX"]) as DropDownList
我一直这样建议用一个最低级的测试来验证自己是否懂得什么是asp.net:在你的页面上放一个Button,点击它时页面回发,但是后台什么都不做。显然,这个按钮被点击应该对整个页面毫无任何影响,因为它的后台程序中什么都不做。然后你就可以在调试程序时随时点击这个按钮,例如你的所谓“更新”时随时点击这个按钮,如果你发现屏幕上的控件(例如ddlType)不见了,或者状态紊乱了,不要再写更多的asp.net程序了,赶紧搞懂asp.net吧,否则写得越多你对asp.net的误解越深越乱。
将这个DropDownList做到模板列中!!