首页 > 代码库 > Repeater 中TextBox 触发TextChanged事件

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事件