Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

//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;
}
}
 
Share this answer
 
Comments
Sriram Mani 27-Nov-12 23:43pm    
Thanks Santosh
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
 
Share this answer
 
Comments
prabhatsp 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900