469,903 Members | 2,196 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

How to pass a parameter to an event from a textbox in a gridview

I have a gridview with a textbox in an ItemTemplate, as below. The
OnTextChanged event fires okay but how do I pass a parameter to it, I get an
error when I try "OnTextChanged(""SomeData"")". I would also like to pass
in the value of column 1. I would also like to be able to get the row the
user is on when this event fires. Thank you for your help.

<asp:GridView ID="gv" />
<Columns>
<asp:BoundField DataField="Col1" HeaderText="Column 1"/>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:TextBox ID="txt" OnTextChanged="OnTextChanged"
Autopostback=true Columns=15 runat="server" Text='<%# Bind("Column2Data")
%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Protected Sub OnTextChanged(ByVal sender as Object, ByVal e as EventArgs)
'some procedures here
End Sub


Dec 12 '07 #1
2 8097
You don't need to pass, you can figure it out in the event handler. The
sender parameter points on the textbox. The textbox is inside a TableCell.
The TableCell is inside a row. With this understanding you can navigate up
from the sender to the containing row using Parent property.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
<testerwrote in message news:O2**************@TK2MSFTNGP05.phx.gbl...
>I have a gridview with a textbox in an ItemTemplate, as below. The
OnTextChanged event fires okay but how do I pass a parameter to it, I get
an error when I try "OnTextChanged(""SomeData"")". I would also like to
pass in the value of column 1. I would also like to be able to get the
row the user is on when this event fires. Thank you for your help.

<asp:GridView ID="gv" />
<Columns>
<asp:BoundField DataField="Col1" HeaderText="Column 1"/>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:TextBox ID="txt" OnTextChanged="OnTextChanged"
Autopostback=true Columns=15 runat="server" Text='<%# Bind("Column2Data")
%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Protected Sub OnTextChanged(ByVal sender as Object, ByVal e as EventArgs)
'some procedures here
End Sub


Dec 12 '07 #2
Thanks! that works great! This is my code if it helps anyone, quite simple!
Dim txt as TextBox

'get the cell the control that triggered this event is in

Dim parent1 as DataControlFieldCell = sender.parent

'get the row the cell is in that the control is in that triggered this
event - got that?

Dim parent2 as Gridviewrow = parent1.parent

txt = cType(Parent2.FindControl("myControl"), textbox)

'do whatever

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:O3**************@TK2MSFTNGP02.phx.gbl...
You don't need to pass, you can figure it out in the event handler. The
sender parameter points on the textbox. The textbox is inside a TableCell.
The TableCell is inside a row. With this understanding you can navigate up
from the sender to the containing row using Parent property.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
<testerwrote in message news:O2**************@TK2MSFTNGP05.phx.gbl...
>>I have a gridview with a textbox in an ItemTemplate, as below. The
OnTextChanged event fires okay but how do I pass a parameter to it, I get
an error when I try "OnTextChanged(""SomeData"")". I would also like to
pass in the value of column 1. I would also like to be able to get the
row the user is on when this event fires. Thank you for your help.

<asp:GridView ID="gv" />
<Columns>
<asp:BoundField DataField="Col1" HeaderText="Column 1"/>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:TextBox ID="txt" OnTextChanged="OnTextChanged"
Autopostback=true Columns=15 runat="server" Text='<%# Bind("Column2Data")
%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Protected Sub OnTextChanged(ByVal sender as Object, ByVal e as EventArgs)
'some procedures here
End Sub



Dec 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by rodchar | last post: by
2 posts views Thread by rajkm42 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.