Hi Majid,
If you want the same name for all those radio then you can use your
custom radio button
. You just need to know how to form little html markup.
Also note your template control
no
need to inherit from page class. Here is the sample code.
public class GridViewTemplate : ITemplate
{
ListItemType templateType;
string columnName;
public GridViewTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
MyRadioButton button = new MyRadioButton();
button.ClientIDMode = ClientIDMode.Static;
button.ID = "Radio1";
button.Name = "Radio1";
button.Value = "Test";
switch (templateType)
{
case ListItemType.Item:
container.Controls.Add(button);
break;
}
}
}
public class MyRadioButton : RadioButton
{
public string Name { get; set; }
public string Value { get; set; }
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
sb.Append("<input type='radio' id='" + Name + "' name='" + Name + "' value='" + Value + "'/>");
writer.Write(sb.ToString());
}
}
And add it in the same way as you did. For the header you use the default header template available in asp.net
TemplateField rbtnColumn = new TemplateField();
rbtnColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
GridView1.Columns.Add(rbtnColumn);
good luck.
Improved part as per your comment
-------------------------------------------
What you can do is handle the row data bound event of the gridview and assign the value for the radio buttons. Sample code as below.
In the gridview markup assign a handler like this
OnRowDataBound="Gridview1_RowDatabound"
or else in the page load
GridView1.RowDataBound += new GridViewRowEventHandler(Gridview1_RowDatabound);
. Once the handler is assigned then inside the handler
protected void Gridview1_RowDatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
MyRadioButton btn = (MyRadioButton)e.Row.Cells[3].FindControl("Radio1");
btn.Value = e.Row.Cells[0].Text;
}
}
The cell indices you need to change as per your data. Now the radio button's value are assigned for the customer id. Hope that helps.