ASP上的ClientSideEvents:Net转发器控制

时间:2012-09-05 07:44:46

标签: c# asp.net repeater

我正在转发器中创建一个文本框控件。这是.cs代码:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    Est estimationItem = (Est)e.Item.DataItem;
    TextBox txtWeekly = (TextBox)e.Item.FindControl("txtWeekly");
    txtWeekly.Text = estimationItem.SMEst.ToString();   
}

这是.aspx代码:

<asp:Repeater ID="WeeklyEst" OnItemDataBound="WeeklyEst_ItemDataBound" runat="server">
    <HeaderTemplate>
        <table>
           <tr>
     </HeaderTemplate>
     <ItemTemplate>
         <td>
              <asp:TextBox ID="txtWeekly" runat="server">  
                 <ClientSideEvents OnTextChanged="function(s, e) { alert('AlertIsHere!');}" />
              </asp:TextBox>
         </td>
     </ItemTemplate>
     <FooterTemplate>
             <td>
             </td>
          </tr> 
        </table>
     </FooterTemplate>
</asp:Repeater>

我想要做的是在文本框中更改文本时触发警报。我怎样才能做到这一点? CliendSideEvent在另一个文本框上触发,但在转发器控件中,它不起作用。

3 个答案:

答案 0 :(得分:0)

使用Repeater控件的ItemDataBound事件,你可以这样做......,如果我理解正确

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item)
        {
            TextBox mytxt = e.Item.FindControl("txtWeekly") as TextBox;
            // you can just pass "this" instead of "mytxt.ClientID" and get the ID from the DOM element
            mytxt.Attributes.Add("onchange", "doStuff('" + mytxt.ClientID + "');");
        }
    }

答案 1 :(得分:0)

如果你不想在代码隐藏中这样做,那么你可以使用jquery,这会更容易

$('。txtbox')。change(function(){   警报($(本).VAL()); });

将cssclass'txtbox'添加到您的文本框中。

答案 2 :(得分:0)

不显眼的方法

怎么样?
<head runat="server">
    <title></title>
    <script type="text/javascript" src="js/jquery-1.7.1.js" ></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // select all input text ending with txtWeekly as repeater control
            // will create something like rptWeeklyEst$ctl00$txtWeekly
            $('input[name$="txtWeekly"][type=text]').change(function () {
                alert('textbox changed to ' + $(this).val());
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="rptWeeklyEst" runat="server">
            <ItemTemplate>
                <asp:TextBox ID="txtWeekly" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:Repeater>
        <br />
        Nope
        <asp:TextBox ID="txtNope" runat="server"></asp:TextBox>
    </div>
    </form>
</body>