Florence Blogspot about asp.net tutorials and web design and web development

Tuesday, February 5, 2008

radio button inside datagrid

Using Radio buttons to select each row in datagrid
=====================================
Using ASP.net to handle radio button would be slow or very slow if datagrid has much data to display because ASP .net triggers "evey" event of any server side control by reloaing the page while posting some data back to it, which definitely reduce performance.So I suggest you to choose javascript for this in collaboration with ASP .net, infact ASP .net translates most of its commands into javascript.To have radio buttons to select each row inside the datagrid follow the following easy steps (100% tested, if found any problem post here):

.ASPX File:
========
In aspx page add the following javascript function




Your Radio Button ASP .net Control inside the data grid would look like this:

Here I used GroupName property to just Identify the row no in Database, where datagrid is bound to any datasource in page load event. RecID in this case is the Primary Key in the table holding unique RecID for each row in the table. You can replace this with your table's field name, or remove it at all if you donot want to record the row number for each radio button. You can also use the Text property to set text for the radio button.Thats all concerned in aspx page.
.CS File
======
Here Make the following function and call it in Page Load event, this function would set the OnClick event of each RadioButton inside the data grid, let say your data grid name is "grdDataGrid":

public void SetGrdRadiosOnClick()
{
int i;
RadioButton B;
for (i = 0; i
B.Attributes.Add("OnClick", "SelectMeOnly(" + B.ClientID + ", "+ "'grdDataGrid'" + ")");
}
}

You got it, Thats all it need to have radio button functionality to select rows in Datagrid and in fastest way.Further, if you want to get which radio is selected and at which row then you cn use the following function, let say you have a button name "btnSelect" outside the datagrid to process the selected row in datagrid. (Remember we set the Unique Row ID of database in GroupName property of each radio button):GridName is "grdDataGrid"RadioButton inside the grid was "radioSelect" which is repeated for each rowpublic

void btnSelect_Click(object sender, System.EventArgs e)
{
RadioButton B;
int i, RecID=0;
for (i = 0; i
{
B = (RadioButton) grdDataGrid.Items[i].FindControl("radioSelect");
if (B.Checked) RecID = int.Parse(B.GroupName);
}
Response.Redirect("./ProcessRecord.aspx?RecID=" + RecID);
}

God Bless You.

3 comments:

Ruchi said...

Wow,
Nice post I am going to look around your site seems you have some great stuff and will be giving you a link back from my site.

Jack said...

Hey,
Thanks for the great information.
:)

. said...

Hey,

where is the javascript code.

Search