Repeater 中TextBox 触发TextChanged事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Repeater 中TextBox 触发TextChanged事件相关的知识,希望对你有一定的参考价值。

两种方法

1.TextBox 绑定TextChanged 并设置AutoPostBack ="true" ,如果不设置AutoPostBack ="true"则不会触发TextChanged事件,然后在对应的方法里面循环Repeater查找改变项

 

部分代码

<ItemTemplate >
    <tr>
        <td style ="width :9%;"><%#Eval("Date")%> <asp:HiddenField ID ="hfkey" runat ="server" Value =\'<%#Eval("ID")%> \' /></td>
        <td style ="width :9%;"><%#Eval("FBASalesDays")%></td>
        <td style ="width :9%;"><asp:TextBox ID ="txtUPSShipQty"  runat="server" Text =\'<%#Eval("UPSShipQty")%>\' AutoPostBack ="true"   OnTextChanged ="btnCal_Click" ></asp:TextBox> </td>
    </tr>
    </ItemTemplate>

 

后台部分代码

protected void btnCal_Click(object sender, EventArgs e)
        {
            
            foreach (RepeaterItem item in rpdList.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    HiddenField hfkey = item.FindControl("hfkey") as HiddenField;
                    
                    TextBox txtUPSShipQty = item.FindControl("txtUPSShipQty") as TextBox;
                   
                    DataRow[] rows = dt.Select("ID =\'" + hfkey.Value + "\'");

                    if (rows != null && rows.Length > 0)
                    {
                       
                        if (rows[0]["UPSShipQty"].ToString().Trim() != txtUPSShipQty.Text.Trim())
                        {
                            rows[0]["UPSShipQty"] = txtUPSShipQty.Text.Trim();

                            CalFBAShipQty2(dt, 3, "UPSShipQty", hfkey.Value.Trim());
                            break;
                        }
                                        
                    }
                }
            }
            rpdList.DataSource = dt;
            rpdList.DataBind();
        }
View Code

 

 

 

2.让TextBox 触发Repeater 的ItemCommand事件,同样需要绑定TextChanged 并设置AutoPostBack ="true",然后在TextChanged事件中调用ItemCommand

 

html

<asp:Repeater ID="Repeater1" runat="server" 
  onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<%#Eval("txt") %><table>
    <tr>
        <td>
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        </td>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Button"  />
        </td>
        <td>
            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"/>
        </td>
    </tr></table>
</ItemTemplate>
</asp:Repeater>

 

后台代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("id");
        dt.Columns.Add("txt");
        dt.Rows.Add("1", "1txt");
        dt.Rows.Add("2", "2txt");
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    //注意这里的参数设置
    RepeaterCommandEventArgs e1 = new RepeaterCommandEventArgs(((TextBox)sender).NamingContainer as RepeaterItem,sender,new CommandEventArgs("test",null));
    Repeater1_ItemCommand(Repeater1, e1);
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    string ss = e.CommandName;
    if (ss == "test")
        Response.Write("123");
}
View Code

参考:http://bbs.csdn.net/topics/350011338

 

以上是关于Repeater 中TextBox 触发TextChanged事件的主要内容,如果未能解决你的问题,请参考以下文章

经过js修改后如何重新获得Repeater数据

C# 面板上带有 DataReader 和 TextBox 控件的Repeater控件

Repeater控件的

Repeater下拉事件触发修改本行数据

Repeater控件 ---属性(ItemCommand事件)

2017-5-22 Repeater的Command用法