OK here's my page design... there is a DataList with a "Select" imagebutton. When it is clicked, that item is loaded into a FormView in Edit mode for changes. When the page loads, selecting the first item works fine. However if I click another item's select button, the FormView simply disappears. Some code...
The Page
<div id="Left">
<asp:DataList ID="UsersDataList" runat="server" DataKeyField="UserID" OnSelectedIndexChanged="UsersDataList_SelectedIndexChanged"
OnDeleteCommand="UsersDataList_DeleteCommand" RepeatLayout="Flow">
<ItemTemplate>
<span><%#Eval("FirstName") %> <%#Eval("LastName") %></span>
<asp:ImageButton ID="EditImageButton" runat="server" ImageUrl="~/Images/EditIcon.gif" AlternateText="Edit"
CommandName="Select" CommandArgument='<%#Eval("UserId") %>' />
<hr />
</ItemTemplate>
</asp:DataList>
</div>
<div id="Right">
<asp:FormView ID="UserFormView" runat="server" DefaultMode="Insert" OnItemCommand="UserFormView_ItemCommand"
OnItemInserting="UserFormView_ItemInserting">
<InsertItemTemplate>
<asp:Label ID="LoginNameLabel" runat="server" Text="Login Name" CssClass="Label" AssociatedControlID="LoginNameTextBox" />
<asp:TextBox ID="LoginNameTextBox" runat="server" MaxLength="30" CssClass="TextBox" />
<asp:Button ID="AddButton" runat="server" Text="Add User" CommandName="Insert" CssClass="Button" />
<asp:Button ID="ClearButton" runat="server" Text="Clear" CommandName="Cancel" CssClass="Button" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:Label ID="LoginNameLabel" runat="server" Text="Login Name" CssClass="Label" AssociatedControlID="LoginNameTextBox" />
<asp:TextBox ID="LoginNameTextBox" runat="server" MaxLength="30" CssClass="TextBox" />
<asp:Button ID="UpdateButton" runat="server" Text="Update User" CommandName="Update" CssClass="Button" />
<asp:Button ID="DeleteButton" runat="server" Text="Delete User" CommandName="Delete" CssClass="Button" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" CommandName="Cancel" CssClass="Button" />
</EditItemTemplate>
</asp:FormView>
</div>
And the code behind...
protected void UsersDataList_SelectedIndexChanged(object sender, EventArgs e)
{
if (UserFormView.CurrentMode != FormViewMode.Edit)
{
UserFormView.ChangeMode(FormViewMode.Edit);
}
this.LoadUser(Convert.ToInt32((sender as DataList).SelectedValue));
}
private void LoadUser(int userId)
{
// Some code to get the "user" object from the userId
((TextBox)UserFormView.FindControl("LoginNameTextBox")).Text = user.LoginName;
}
The last line of the LoadUser routine throws a null reference exception the 2nd time around of course. If I comment the line out the page loads but the FormView has disappeared. Please help!