Browsing all articles tagged with Grop Radio Buttons in GridView
How to Add a GridView Column of Radio Buttons
In ASPX File add the following lines of code…
<form id="form1" runat="server">
<div>
<asp:Button ID="btnLoad" runat="server" Text="Load Users" onclick="btnLoad_Click" />
<asp:GridView ID="gvUsers" runat="server" OnRowCreated="gvUsers_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Method1">
<ItemTemplate>
<asp:Literal id="RadioButtonMarkup" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Method2">
<ItemTemplate>
<input name="MyRadioButton" type="radio" value='<%# Eval("Name") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="bSubmit" runat="server" Text="Submit" onclick="bSubmit_Click" />
<br />
<asp:Label ID="lmsg" runat="server"></asp:Label>
</form>
<div>
<asp:Button ID="btnLoad" runat="server" Text="Load Users" onclick="btnLoad_Click" />
<asp:GridView ID="gvUsers" runat="server" OnRowCreated="gvUsers_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Method1">
<ItemTemplate>
<asp:Literal id="RadioButtonMarkup" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Method2">
<ItemTemplate>
<input name="MyRadioButton" type="radio" value='<%# Eval("Name") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="bSubmit" runat="server" Text="Submit" onclick="bSubmit_Click" />
<br />
<asp:Label ID="lmsg" runat="server"></asp:Label>
</form>
In code behind file add the following code…
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLoad_Click(object sender, EventArgs e)
{
using (ProjectDBDataContext db = new ProjectDBDataContext())
{
var roles = from r in db.MUserInfos select r;
gvUsers.DataSource = roles;
gvUsers.DataBind();
}
}
private int UsersSelectedIndex
{
get
{
if (string.IsNullOrEmpty(Request.Form["UsersGroup"]))
return -1;
else
return Convert.ToInt32(Request.Form["UsersGroup"]);
}
}
protected void gvUsers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Grab a reference to the Literal control
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
// Output the markup except for the "checked" attribute
output.Text = string.Format( "<input type=\"radio\" name=\"UsersGroup\" " + "id=\"RowSelector{0}\" value=\"{0}\"", e.Row.RowIndex);
// See if we need to add the "checked" attribute
if (UsersSelectedIndex == e.Row.RowIndex)
output.Text += " checked=\"checked\"";
// Add the closing tag
output.Text += " />";
}
}
protected void bSubmit_Click(object sender, EventArgs e)
{
int x = UsersSelectedIndex;
lmsg.Text = "method1: " +x.ToString() + "</br>";
string selectedValue = Request.Form["MyRadioButton"];
lmsg.Text += "method2: " + selectedValue;
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLoad_Click(object sender, EventArgs e)
{
using (ProjectDBDataContext db = new ProjectDBDataContext())
{
var roles = from r in db.MUserInfos select r;
gvUsers.DataSource = roles;
gvUsers.DataBind();
}
}
private int UsersSelectedIndex
{
get
{
if (string.IsNullOrEmpty(Request.Form["UsersGroup"]))
return -1;
else
return Convert.ToInt32(Request.Form["UsersGroup"]);
}
}
protected void gvUsers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Grab a reference to the Literal control
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
// Output the markup except for the "checked" attribute
output.Text = string.Format( "<input type=\"radio\" name=\"UsersGroup\" " + "id=\"RowSelector{0}\" value=\"{0}\"", e.Row.RowIndex);
// See if we need to add the "checked" attribute
if (UsersSelectedIndex == e.Row.RowIndex)
output.Text += " checked=\"checked\"";
// Add the closing tag
output.Text += " />";
}
}
protected void bSubmit_Click(object sender, EventArgs e)
{
int x = UsersSelectedIndex;
lmsg.Text = "method1: " +x.ToString() + "</br>";
string selectedValue = Request.Form["MyRadioButton"];
lmsg.Text += "method2: " + selectedValue;
}
}
Categories
- Dot Net (12)
- .Net Framework (1)
- Ajax (1)
- ASP.NET (6)
- C#.NET (3)
- Code Snippets (3)
- WCF (1)
- SQL Server (3)
- Tutorials (1)
- Video (1)




