开发者

Error when dropdownlist.databind() if I change from FormView.EditMode to FormView.InsertMode

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.


You have to DataBind the FormView after you've called ChangeMode and before FormView1.FindControl.

So this works:

FormView1.ChangeMode(FormViewMode.Insert);
FormView1.DataBind();
DropDownList drp = (DropDownList)FormView1.FindControl("insertSuite");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜