Getting row index by Event Args...

B

basulasz

I created a radio button dynamically in a row of a Grid View control. I
enabled auto post back property of this radio button to execute a code. For
example , I create 20 rows and I want to know about which button is checked.
How can i get the index of that row?
 
T

Teemu Keiski

Hi,

you of course handle RadioButton's CheckedChanghed event. In the event cast
'sender' as CheckBox (it's the CB raising the event). Then you get the
current GridViewRow via NamingContainer property of the RadioButton.

For example

protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn=(RadioButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
//Now you can get row index etc via the row object
}

And this event handler has been wired to the CB in aspx (in GridView
TemplateField) like <asp:RadioButton
OnCheckedChanged="RadioButton_CheckedChanged" ... />
 
B

basulasz

Hi Teemu;

Firstly thanks. I have tried that you written, but i thing there is missing
here. let me to say what I do and correct me if there is something wrong.

I have enabled AutoPostBack property as true. And written code for
CheckedChanged event of RadioButton. And tried to print the row index as below

public void rdV_CheckedChanged(object sender, EventArgs e)
{
RadioButton btn = (RadioButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
Label1.Text = row.DataItemIndex.ToString();
}

But when i click the RadioButton I go to next page and only see unchecked
RadioButtons and empty Label1.

I do not understand what is wrong with that?
 
B

basulasz

I have tried the same thing with ImageButton, and I am done with that.

Thanks for help...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top