• 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

  • 相关阅读:
    开始几天的基本学习
    从这个博客开始我的机器学习深度学习之路
    剑指Offer:面试题3——二维数组中的查找(java实现)
    HIVE配置文件
    C++ 之旅:前言
    leetcode 349:两个数组的交集I
    python学习(三):matplotlib学习
    python学习(二):python基本语法
    Android环境搭建
    LeetCode:237
  • 原文地址:https://www.cnblogs.com/lidaying5/p/6525101.html
Copyright © 2020-2023  润新知