TextBoxt在Datalist中返回空值

时间:2014-01-09 07:51:18

标签: c# asp.net asp.net-ajax

可能是我之前重复的问题,但我真的很累,如何解决这个问题,因为当这个事件触发文本框返回空时:

 protected void Btn_publicpost_Click(object sender, ImageClickEventArgs e)
 {

    string x = txt_publicpost.Text;
 }

我在 datalist 中测试了更新面板并且运行良好,因此问题可能出在 datalist

<asp:DataList ID="DL_userpost" runat="server" CssClass="single-posts" RepeatColumns="1"
    OnItemCommand="DL_userpost_ItemCommand">
    <ItemTemplate>
        <div class="single-posts">
            <div class="post-user">
                <a href="#"></a>
            </div>
            <div class="post-container-all">
                <div class="post-user-single-img">
                    <asp:Image ID="Img_Twasol" runat="server" ImageUrl='<%# Eval("Post_File")%>' Width="130"
                        Height="99" /></div>
                <div class="post-user-single-data-tabs">
                    <div class="post-user-single-data-title">
                        <%#Eval("User_Name")%>
                    </div>
                    <div class="post-user-single-data-text">
                        <%#Eval("Post_Content")%>
                    </div>
                </div>
            </div>
             <div class="comment">
                    <asp:UpdatePanel ID="UPPublicComment" runat="server" >
                            <ContentTemplate>
                                <asp:Panel ID="pnpublicpost" runat="server" DefaultButton="Btn_publicpost">
                                    <asp:TextBox runat="server" ID="txt_publicpost" CssClass="comment-tabs2"></asp:TextBox>
                                    <asp:ImageButton ID="Btn_publicpost" CommandName="publicpost" ValidationGroup="publicpost"
                                        runat="server" OnClick="Btn_publicpost_Click" Style="display: none" />
                                    <asp:Label ID="LblpublicpostPID" runat="server" Text='<%#Eval("Post_ID")%>' Visible="false"></asp:Label>
                                </asp:Panel>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </div>
                </div>
            </div>

        </div>
         </div>
    </ItemTemplate>
</asp:DataList>
  

那么问题出在哪里?

1 个答案:

答案 0 :(得分:0)

问题是你不能使用来自DataList和其他转发器的动态引用,如txt_publicpost。 试试这样的事情

protected void Btn_publicpost_Click(object sender, ImageClickEventArgs e)
{
   var box = (TextBox)((ImageButton)sender).Parent.FindControl("txt_publicpost");
   string x = box.Text;
}