【问题标题】:Event Handler OnItemCommand with TextBox control带有 TextBox 控件的事件处理程序 OnItemCommand
【发布时间】:2012-03-13 23:04:20
【问题描述】:

我有一个Repeater,它包含一个TextBox 和一个LinkBut​​ton。单击 LinkBut​​ton 时,我需要抓取 TextBox.Text 并做一些事情……

使用事件 Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 我可以使用 TextBox tx = e.Item.FindControl("txCode") as TextBox 获取 TextBox 的值

然而

使用事件 Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e) 我没有得到任何回报。文本框为空。

如何使用“OnItemCommand”从 TextBox 中获取文本/内容?

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
    <ItemTemplate>
        <li>                    
        <asp:TextBox ID="txCode" runat="server"></asp:TextBox>
        <asp:LinkButton CommandName="verifyCode" ID="lbCode" runat="server">Submit<asp:LinkButton>
        </li>
    </ItemTemplate>
</asp:Repeater> 

我可以得到下面的文本框值

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    TextBox tx = e.Item.FindControl("txCode") as TextBox;
    string myText = tx.Text; '<--- working
}

我无法获得下面的文本框值

protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "verifyCode")
    {
        TextBox tx = e.Item.FindControl("txCode") as TextBox;
        string myText = tx.Text;  '<--- NOT working
}

【问题讨论】:

  • 您是否调试过是否引发了此事件?也许您错过了在 Page_Load 中检查 !IsPostBack。将不在回发上的转发器绑定到它的 DataSource。
  • if(!IsPostBack) - 该死的

标签: asp.net repeater


【解决方案1】:

不要在每次回发时将您的Repeater 绑定到它的DataSource。否则 ViewState 无法正确重新加载,这会导致此类问题。

所以在启用ViewState 时,请务必检查Page_Load 中的IsPostBack propertyEnableViewState=true):

if(!IsPostBack)BindRepeaterToDataSource();

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多