ASP.NET ListView EditItemTemplate/ItemCommand [英] ASP.NET ListView EditItemTemplate/ItemCommand

查看:107
本文介绍了ASP.NET ListView EditItemTemplate/ItemCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有点难以让EditItemTemplate/ItemCommand在我创建的列表视图上工作.请参见下面的代码:

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: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>





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);

       }
   }



对大量HTML表示歉意,但我想确保整个列表视图都在那里.

我得到的错误是user.UserID = Convert.ToInt16(UserIDtxt.Text)上的NullReferenceException.我删除了这行代码,然后再次运行该应用程序,只是在user.UserName行上得到了相同的错误.

我知道发生了什么-该值没有从文本框传递给用户,但我不知道为什么.

任何帮助将不胜感激.

Dan



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

推荐答案

为正在浏览类似内容的任何人解决-

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);
    }


这篇关于ASP.NET ListView EditItemTemplate/ItemCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆