Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

<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 .

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) ?

Thanks

like image 859
JAN Avatar asked Oct 19 '22 12:10

JAN


1 Answers

I tried to use the OnTextChanged of the Textbox , but I can't get the item that fired the event this way .

The sender argument is always the control that triggered the event:

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.

If you also need to get the reference of the HiddenField use following code:

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. As an aside, that's working similar for other web-databound controls like GridView or DataList.

like image 188
Tim Schmelter Avatar answered Oct 22 '22 02:10

Tim Schmelter