我有一个像这样的 FormView:
<EditItemTemplate>
<th>Test Name</td>
<td><asp:Label runat="server" ID="lblSuite" Text='<%# Eval("Suite") %>'></asp:Label></td>
</EditItemTemplate>
<InsertItemTemplate>
<th>Test Name</td>
<td><asp:DropDownList ID="insertSuite" runat="server"></asp:DropDownList></td>
</InsertItemTemplate>
这意味着在 InsertMode 中,用户可以使用下拉列表更改 Suite
,而在 EditMode 中,用户只能看到 Suite
而不能进行修改。
如果用户单击其中一条记录,FormView 将更改为 EditMode,代码如下:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//getDatasource
FormView1.DataSource = objList;
FormView1.ChangeMode(FormViewMode.Edit);
FormView1.DataBind();
}
如果用户单击 Add New
按钮,FormView 将更改为 InsertMode,代码如下:
FormView1.ChangeMode(FormViewMode.Insert);
protected void btnAddSingle_Click(object sender, EventArgs e)
{
FormView1.ChangeMode(FormViewMode.Insert);
DropDownList drp = (DropDownList)FormView1.FindControl("insertSuite");
drp.DataSource = otherRepo.SuiteDropdownListDataSource(2);
drp.DataTextField = "Name";
drp.DataValueField = "Name";
drp.DataBind();
}
我的问题是:
如果我单击其中一条记录并进入 EditMODe,然后单击 Add New
按钮,则会发生错误。
(DropDownList)FormView1.FindControl("insertSuite")
为空。
我以为这是关于生命周期的东西,但想不通。
最佳答案
在调用 ChangeMode
之后和 FormView1.FindControl
之前,您必须 DataBind
FormView。
所以这是可行的:
FormView1.ChangeMode(FormViewMode.Insert);
FormView1.DataBind();
DropDownList drp = (DropDownList)FormView1.FindControl("insertSuite");
关于c# - 如果我从 FormView.EditMode 更改为 FormView.InsertMode,dropdownlist.databind() 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6421553/