Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am a little be stuck getting the EditItemTemplate/ItemCommand to work on a listview I have created. Please see code below:

ASP.NET
<asp:ListView runat="server" ID="AdminUserControl" OnItemCommand="AdminUserControl_ItemCommand">
                        <LayoutTemplate>
                            <table>
                                <tr>
                                    <th>
                                        <asp:LinkButton ID="UserIDlb" runat="server">User ID</asp:LinkButton>
                                    </th>
                                    <th>
                                        <asp:LinkButton ID="UserNamelb" runat="server">User Name</asp:LinkButton>
                                    </th>
                                    <th>
                                        <asp:LinkButton ID="Emaillb" runat="server">Email Address</asp:LinkButton>
                                    </th>
                                    <th>
                                        <asp:LinkButton ID="Rolelb" runat="server">Role</asp:LinkButton>
                                    </th>
                                    <th>
                                        <asp:Label ID="Controllbl" runat="server">Controls</asp:Label>
                                    </th>
                                    <th></th>
                                </tr>
                                <tr id="itemPlaceholder"  runat="server"></tr>
                            </table>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <tr id="Tr1"  runat="server">
                                <td>
                                    <asp:Label ID="UserIDlbl" runat="server"><%#Eval("UserID") %></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="UserNamelbl" runat="server"><%#Eval("UserName") %></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="Emaillbl" runat="server"><%#Eval("Email") %></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="Rolelbl" runat="server"><%#Eval("Role") %></asp:Label>
                                </td>
                                <td>
                                    <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                                    <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
                                    <asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <tr id="Tr2"  runat="server">
                                <td>
                                    <asp:TextBox ID="UserIDtxt" runat="server" Enabled="false" Text='<%#Eval("UserID") %>' Width="100px"></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="UserNametxt" runat="server" Text='<%#Eval("UserName") %>' Width="100px"></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="Emailtxt" runat="server" Text='<%#Eval("Email") %>' Width="100px"></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="Roletxt" runat="server" Text='<%#Eval("Role") %>' Width="100px"></asp:TextBox>
                                </td>
                                <td>
                                    <asp:Button ID="Updatebtn" runat="server" CommandName="Update" Text="Update" />
                                </td>
                            </tr>
                        </EditItemTemplate>
                    </asp:ListView>


C#
protected void AdminUserControl_ItemCommand(object sender, ListViewCommandEventArgs e)
   {
       if (e.CommandName == "Edit")
       {
           TextBox UserIDtxt = (TextBox)e.Item.FindControl("UserIDtxt");
           TextBox UserNametxt = (TextBox)e.Item.FindControl("UserNametxt");
           TextBox Emailtxt = (TextBox)e.Item.FindControl("Emailtxt");
           TextBox Roletxt = (TextBox)e.Item.FindControl("Roletxt");

           user = new User();
           user.UserID = Convert.ToInt16(UserIDtxt.Text);
           user.UserName = UserNametxt.Text;
           user.Email = Emailtxt.Text;
           user.Role = Roletxt.Text;

           admin.UpdateUsers(user);

       }
   }


Apologies for large amount of HTML but I wanted to make sure the whole listview was there.

The error I am getting is a NullReferenceException on user.UserID = Convert.ToInt16(UserIDtxt.Text). I removed this line of code and ran the app again only to get the same error on the user.UserName line.

I know whats happening - the value isn't being passed from the text box to the user but I have no idea why.

Any help would be appreciated.

Dan
Posted

1 solution

Solved it for anyone who is looking at similar -

protected void AdminUserControl_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        AdminUserControl.EditIndex = e.NewEditIndex;
        UserBLL userbll = new UserBLL();
        AdminUserControl.DataSource = userbll.GetAllUsers();
        AdminUserControl.DataBind();

    }
    protected void AdminUserControl_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        UserBLL userbll = new UserBLL();

        TextBox UserIDtxt = (TextBox)e.NewValues["UserIDtxt"];
        TextBox UserNametxt = (TextBox)e.NewValues["UserNametxt"];
        TextBox Emailtxt = (TextBox)e.NewValues["Emailtxt"];
        TextBox Roletxt = (TextBox)e.NewValues["Roletxt"];

        user = new User();
        user.UserID = Convert.ToInt16(UserIDtxt.Text);
        user.UserName = UserNametxt.Text;
        user.Email = Emailtxt.Text;
        user.Role = Roletxt.Text;

        admin.UpdateUsers(user);
    }
 
Share this answer
 
Comments
fjdiewornncalwe 31-Oct-12 17:09pm    
Thanks for sharing your solution.
DanHodgson88 31-Oct-12 17:16pm    
My pleasure a community doesn't work without sharing :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900