SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    0 Post(s)
    0 Thread(s)

    Problem with EditItemTemplate


    I am working through the "Build Your Own ASP.NET Web Site" book, and am currently stuck on an issue in Chapter 10: Displaying Content Using Data Lists.

    I have a DataList set up like so:

    <asp:DataList ID="employeesList" runat="server" OnItemCommand="employeesList_ItemCommand">
                <asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
                Name: <strong><%# Eval("Name") %></strong><br />
                Username: <strong><%# Eval("Username") %></strong><br />
                <asp:LinkButton ID="detailsButton" runat="server"
                    Text='<%# "View more details about " + Eval("Name") %>'
                    CommandName="MoreDetailsPlease" CommandArgument='<%# Eval("EmployeeID") %>' /><br />
                <asp:LinkButton ID="editButton" runat="server"
                    Text='Edit Employee <%# Eval("Name") %>'
                    CommandName="EditItem" CommandArgument='<%# Eval("EmployeeID") %>' />
                Name: <asp:TextBox ID="nameTextBox" runat="server"
                    Text='<%# Eval("Name") %>' /><br />
                Username: <asp:TextBox ID="usernameTextBox" runat="server"
                    Text='<%# Eval("Username") %>' /><br />
                <asp:LinkButton ID="updateButton" runat="server"
                    Text="Update Item" CommandName="UpdateItem"
                    CommandArgument='<%# Eval("EmployeeID") %>' />
                <asp:LinkButton ID="cancelButton" runat="server"
                    Text="Cancel Editing" CommandName="CancelEditing"
                    CommandArgument='<%# Eval("EmployeeID") %>' />
                <hr />
    Using the ItemCommand event, I check to see when the "editButton" button is clicked and set the EditItemIndex to that Item. This reveals the "nameTextBox" and "usernameTextBox" controls and the "updateButton" button control. I then check (using the ItemCommand event) if the updateButton" is clicked and, if so, call a function which runs a stored procedure on the DB. You will see that the CommandName for the "updateButton" is "UpdateItem". Here is the code for that command:

    else if (e.CommandName == "UpdateItem")
                // Get the employee ID
                int employeeId = Convert.ToInt32(e.CommandArgument);
                // Get the new name
                TextBox nameTextBox =
                string newName = nameTextBox.Text;
                // Get the new username
                TextBox usernameTextBox =
                string newUsername = usernameTextBox.Text;
                // Update the item
                UpdateItem(employeeId, newName, newUsername);
                // Cancel edit mode
                employeesList.EditItemIndex = -1;
                // Refresh the DataList
    You can see that I am "finding" the TextBox controls and setting their values to two new variables and then calling the "UpdateItem" function (which handles the DB update). I have debugged the code and everything should work. The problem is that after grabbing the contents of the TextBox controls, the values in the new variables are set to the original values of the TextBox controls, not the values as they were when the "updateButton" was clicked. In other words, if I change the username or name TextBoxes before clicking "updateButton" it behaves as if I didn't change the values at all.

    Is there some weird ViewState setting that could cause this?

    I have Visual Studio 2005 Professional Edition.

    Please help!

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    0 Post(s)
    0 Thread(s)
    Probably you are re-initializing the control data on each postback. You have to guard the databinding in the Page_Load with a if (!IsPostback) { ... }

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    0 Post(s)
    0 Thread(s)
    Thanks! That was it. I feel stupid....


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts