Client side radiobutton selection

G

Guest

Hi Guys,

I have a gridview and the first column is a template that generate a
radiobuttons in the gridvew. and I would like that user will be able to
select one option from the list of the generated radio buttons in my first
column - in Client side. (as if in multiple choice exams)


Thanks.
 
G

Guest

Mike,

Just set GroupName property for your RadioButton inside the template - this
will ensure that only one can be selected
 
G

Guest

That was the first thing I did but It didn't work.
and that's why it has to be done programmatically.
 
G

Guest

Mike,

It looks like you up to a bit of coding. Suppose your Template column is the
first in the grid and looks as follows:

<asp:TemplateField>
<ItemTemplate>
<asp:Literal id="ltrl" runat="server" />
</ItemTemplate>
</asp:TemplateField>

then you would want to handle RowCreated event as follows:

private object _selectedKey = null;

private void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell = e.Row.Cells[0];
string name = grd.ClientID + "$" + "rbo";
Literal ltrl = (Literal)cell.Controls[1];
int index = e.Row.RowIndex;
string id = name.Replace("$", "_") + "_" + index.ToString();
bool isSelected = this.IsPostBack && this.Request.Form[name] != null &&
int.Parse(this.Request.Form[name]) == index;
string selected = (isSelected) ? "checked" : "";
if (isSelected)
{
selectedKey = grd.DataKeys[index];
}
ltrl.Text = string.Format("<input type='radio' id='{0}' name='{1}'
value='{2}' {3} />", id, name, index, selected);
}
}

Note that you can get the value of the selectedKey on your postback.

Hope this helps
 

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

Top