Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview
Hi,
Please suggest the procedure for getting the value of a Radio button group or any other asp.net element such as a textbox which is written inside a Gridview templatefield into the C# codebehind code. I tried using the ID of radiobutton directly, also used the FindControl function. But none of them are working.
 
Please suggest some solution. Please include sample codes.
 
Thanks in advance.
Posted 27-Nov-12 1:28am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

//code below for hyperlink acces replace hyperlink with ur control
// also u cant access any control directly which are placed inside gridview template field
 
foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
HyperLink myHyperLink = row.FindControl("myHyperLinkID") as HyperLink;
}
}
  Permalink  
Comments
Sriram Mani at 27-Nov-12 23:43pm
   
Thanks Santosh
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I found the solution for my requirement.
protected void present_CheckedChanged(object sender, EventArgs e)
    {
        
            RadioButton button = sender as RadioButton;
            GridViewRow row = button.NamingContainer as GridViewRow;
            RadioButton pres_button = row.FindControl("present") as RadioButton;
    }
 
Using this code, we can access the elements in the template field of a gridview. Firstly we have to get the sender object as any element required. Then create an Gridviewrow object to access the row required. After that you can access any element in the Gridview using the syntax
ElementType element_name=row.FindControl("elementID_given_in_aspx_page") as ElementType;
as an example,
RadioButton pres_button = row.FindControl("present") as RadioButton;
This solution is working fine for me. I hope it'll be useful to all.
Thanks
  Permalink  
Comments
prabhatsp at 3-Oct-13 8:34am
   
Hi,
I have same error problem ,but use textbox instead of ur radio button, By button click i want to get the entry of the textbox,u have any ideas, please suggest me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 DamithSL 235
2 OriginalGriff 230
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100