如果我从 FormView.EditMode 更改为 FormView.InsertMode,则 dropdownlist.databind() 时出错
我有一个像这样的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")
为空。
我认为这是关于生命周期的问题,但无法弄清楚。
I have a FormView like:
<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>
Which means in InsertMode, the user can change Suite
using a dropdownlist while in EditMode, the user can only see Suite
but cannot do modification.
If the user click one of the record, FormView was changed into EditMode with the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//getDatasource
FormView1.DataSource = objList;
FormView1.ChangeMode(FormViewMode.Edit);
FormView1.DataBind();
}
If the user click the Add New
button, FormView was changed into InsertMode with the code:
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();
}
My Problem is:
If I click the one of the record and get into EditMOde, Then click the Add New
button, Then error occured.
the (DropDownList)FormView1.FindControl("insertSuite")
is null.
I thought it was something about lifecycle but cannot figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在调用
ChangeMode
之后、FormView1.FindControl
之前对 FormView 进行DataBind
。所以这有效:
You have to
DataBind
the FormView after you've calledChangeMode
and beforeFormView1.FindControl
.So this works: