【问题标题】:How to get Text property of TextBox after ItemCommand eventItemCommand事件后如何获取TextBox的Text属性
【发布时间】:2012-05-19 21:53:10
【问题描述】:

我在一个面板中有一个 TextBox 控件,这个面板在 DataList ItemTemplate 中。

触发 ItemCommand 事件后,一切正常,除了 TextBox.Text 属性始终为空字符串“”,尽管其中有一些文本。

我尝试了几种方法,但都没有成功。如果有人可以帮助我,我将不胜感激。简化代码如下所示。 谢谢!

ASPX 页面:

<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
       <ItemTemplate>

           <asp:Panel ID="pnlReply" runat="server" Visible="False">
             <asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
             <asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
           </asp:Panel><br />
           <asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
       </ItemTemplate>
</asp:DataList>

</asp:Content>

ASPX.CS 后面的页面代码

protected void Page_Load(object sender, EventArgs e)
    {

        FillDataList();

    }

    private void FillDataList()
    {
        List<string> list = new List<string>();
        list.Add("First");
        list.Add("Second");
        list.Add("Third");


        dlDataList.DataSource = list;
        dlDataList.DataBind();
    }

    protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "OpenPanel")
        {
            Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
            pnlReply.Visible = true;
        }
        if (e.CommandName == "Send")
        {
            TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
            //I tried this way also..
            //TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
            Label1.Text = txtTextBox.Text;

        }
    }

【问题讨论】:

    标签: c# asp.net textbox datalist


    【解决方案1】:

    请在页面加载事件中使用IsPostBack。如果没有它,您的 FillDataList(); 将在每次回发时执行并重置您的 DataList

      protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                FillDataList();
    
            }
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多