简体   繁体   English

如何使用转发器的OnItemCommand触发文本框的事件,而不使用LinkBut​​ton?

[英]How to fire event of textbox with OnItemCommand of a repeater , without using LinkButton?

I have a repeater with a textbox inside , and I want to fire an event when I move from one textbox to another textbox , with the OnItemCommand of the repeater . 我有一个内部的文本框的中继器,我想,当我从一个文本框移动到另一个文本框,与触发事件OnItemCommand中继器。

<asp:Repeater ID="RptrPeople" runat="server" OnItemDataBound="RptrPeople_ItemDataBound" OnItemCommand="RptrPeople_ItemCommand">
         <ItemTemplate>
                <asp:HiddenField ID="hf" runat="server" Value="<%# Eval(this.ValuedPerson) %>" />
                <asp:TextBox ID="txtDescription" runat="server" IsRequired="false" Visible="true" AutoPostBack="true"  />
         </ItemTemplate>
</asp:Repeater> 

I tried to use the OnTextChanged of the Textbox , but I can't get the item that fired the event this way . 我试图使用文本框的OnTextChanged ,但我无法获得以这种方式触发事件的项目。

Can anyone please advise on a good way to get the item that fires the event , after I moved from one textbox , using the OnItemCommand (for example , I entered 123 in Textbox #1 , and then moved to Textbox #2 ... then I want to fire the event that takes care of the Textbox that has the 123 value) ? 在我从一个文本框移动后,使用OnItemCommand (例如,我在Textbox #1输入123 ,然后移动到Textbox #2 ...然后,任何人都可以建议获得触发事件的项目的好方法)我想触发处理具有123值的文本框的事件)?

Thanks 谢谢

I tried to use the OnTextChanged of the Textbox , but I can't get the item that fired the event this way . 我试图使用文本框的OnTextChanged,但我无法获得以这种方式触发事件的项目。

The sender argument is always the control that triggered the event: sender参数始终是触发事件的控件:

protected void txtDescription_TextChanged(Object sender, EventArgs e)
{
    TextBox txtDescription = (TextBox) sender;
}

So you should use this instead of OnItemCommand because there the sender is the repeater. 所以你应该使用它而不是OnItemCommand因为sender是转发器。

If you also need to get the reference of the HiddenField use following code: 如果您还需要获取HiddenField的引用,请使用以下代码:

protected void txtDescription_TextChanged(Object sender, EventArgs e)
{
    TextBox txtDescription = (TextBox) sender;
    var item = (RepeaterItem) txtDescription.NamingContainer;
    HiddenField hf = (HiddenField) item.FindControl("hf");
}

The NamingContainer of any control in a RepeaterItem is always the RepeaterItem . RepeaterItem中任何控件的NamingContainer始终是RepeaterItem As an aside, that's working similar for other web-databound controls like GridView or DataList . 顺便说一句,这与其他web-dataatabound控件(如GridViewDataList的工作方式类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM