如何从Datalist中的TextBox获取Text值

时间:2012-09-18 16:48:10

标签: c# asp.net textbox datalist

当我点击LinkBut​​ton时,我试图从TextBox中检索Text值,这两个都在DataList的ItemTemplate中。但是它返回一个空值。

这是aspx页面中的datalist代码:

 <asp:DataList ID="DataList1" runat="server" Width="100%" DataKeyField="UserId" OnItemCommand="itemcommand"
                OnItemDataBound="Data_Bind">
                <ItemTemplate>
                    <table style="width: 100%">
                        <tr>
                            <td>
                                <a href="<%#System.String.Format("ViewProfile.aspx?aid={0}",Eval("UserId")) %>">
                                    <asp:Image ID="Img" Width="100px" Height="80px" BorderStyle="Inset" BorderWidth="2px"
                                        BorderColor="Aqua" runat="server" ImageUrl='<%#System.String.Format("../Content/Upload/{0}", Eval("UserDetail.UserPhoto")) %>' />
                            </td>
                            <td >
                                <asp:Label ID="name" runat="server" Text='<%#Eval("UserDetail.Firstname")%>'></asp:Label>
                                <asp:Label ID="lastname" runat="server" Text='<%#Eval("UserDetail.Lastname")%>'></asp:Label>
                            </td>
                            <td>
                                <asp:Label ID="post" runat="server" Text='<%#Eval("Post.post")%>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <asp:LinkButton ID="Like" runat="server" CommandName="onLike" CommandArgument='<%# Eval("Post.PostId") %>'>Like</asp:LinkButton>
                                <asp:Label ID="lk" runat="server"></asp:Label>
                                <asp:LinkButton ID="Share" runat="server" CommandName="onShare" CommandArgument='<%# Eval("Post.PostId") %>'>Share</asp:LinkButton>
                                <asp:Label ID="sh" runat="server"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                        <td colspan="3">
                          <asp:TextBox ID="txtcomment"  runat="server" Height="40px" Text="abc" Width="600px"></asp:TextBox>
                          <input type="text" id="cmnt1" name="cmnt1" runat="server" />
                           &nbsp;
                            <asp:LinkButton ID="Comment" runat="server" CommandName="onComment" CommandArgument='<%# Eval("Post.PostId") %>'>Comment</asp:LinkButton>
                        </td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <asp:DataList ID="DataList2" runat="server">
                                    <ItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                    <asp:Image ID="Img2" Width="50px" Height="40px" BorderStyle="Inset" BorderWidth="2px"
                                                        BorderColor="Aqua" runat="server" ImageUrl='<%#System.String.Format("../Content/Upload/{0}", Eval("UserDetail.UserPhoto")) %>' />
                                                    <asp:Label ID="fname" runat="server" Text='<%#Eval("UserDetail.Firstname")%>'></asp:Label>
                                                    <asp:Label ID="lname" runat="server" Text='<%#Eval("UserDetail.Lastname")%>'></asp:Label>
                                                </td>
                                                <td>
                                                    <asp:Label runat="server" ID="cmnts" Text='<%#Eval("Comments.Comment")%>'></asp:Label>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:DataList>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>

            </asp:DataList>

这是背后的代码:

 protected void itemcommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "onLike")
        {
            var k = e.CommandArgument;

            User ObjUser = new User();
            ObjUser = (User)Session["user"];
            ObjUser.Post.PostId = Convert.ToInt64(k);

            PostService.OnLike(ObjUser);

            DataList1.DataSource = PostService.GetPosts(ObjUser);
            DataList1.DataBind();
        }
        if (e.CommandName == "onComment")
        {
            var k = e.CommandArgument;

            TextBox cmnt = (TextBox) e.Item.FindControl("txtcomment");
            HtmlInputText cmnt1 = (HtmlInputText)e.Item.FindControl("cmnt1");
            User ObjUser = new User();
            ObjUser = (User)Session["user"];
            ObjUser.Post.PostId = Convert.ToInt64(k);
            ObjUser.Comments.Comment = cmnt.Text;

            PostService.OnComment(ObjUser);

            DataList1.DataSource = PostService.GetPosts(ObjUser);
            DataList1.DataBind();

        }
        if (e.CommandName == "onShare")
        {
            var k = e.CommandArgument;

            User ObjUser = new User();
            ObjUser = (User)Session["user"];
            ObjUser.Post.PostId = Convert.ToInt64(k);

            PostService.OnShare(ObjUser);

            DataList1.DataSource = PostService.GetPosts(ObjUser);
            DataList1.DataBind();

        }

    }

2 个答案:

答案 0 :(得分:0)

FindControl不会递归搜索控件。它只会查找当前孩子的控件。根据您使用的结构,您必须在项目的子控件内进行搜索。

您必须编写自己的递归函数来搜索所有子控件以进行此控件。

或者您可以编写扩展方法,以便您可以使用它来递归搜索您需要的位置。我发现here这个功能应该有所帮助。

public static class ControlExtensions
{
    public static Control FindControlRecursive(this Control control, string id)
    {
        if (control == null) return null;
        //try to find the control at the current level
        Control ctrl = control.FindControl(id);
        if (ctrl == null)
        {
            //search the children
            foreach (Control child in control.Controls)
            {
                ctrl = FindControlRecursive(child, id);
                if (ctrl != null) break;
            }
        }
        return ctrl;
    }
}

答案 1 :(得分:0)

asp:TextBox ID="txtQuantity"  runat="server"></asp:TextBox

string Key = Datalist.DataKeys[e.Item.ItemIndex].ToString();

TextBox Textboxvalue = (TextBox)(e.Item.FindControl("txtQuantity"));

//You will get the value in Textboxvalue