Find index or row in gridview with RadioButton
-
Hi, I have included a radiobutton on a gridview using HTML control. I tried getting the index of the row selected by the radio button, but I can't figure out how. I tried using the Request.Form["RadiobuttonName"] but it gives me the value on the cell not the row index. Here is the code When I click the button to retrieve the value.
protected void btnSelect_Click(object sender, EventArgs e)
{
string selectedValue = Request.Form["radSelect"];
int index = Convert.ToInt32(Request.Form["radSelect"]);
}And here is .aspx page
<asp:GridView ID="grdAll" runat="server" OnRowCommand="MPS_Selected" AutoGenerateColumns="false" OnRowDataBound = "grd_rowdatabound">
<Columns>
<asp:BoundField DataField="ID" HeaderText = "" Visible="false" />
<asp:BoundField DataField="Name" HeaderText = "MPS Account Name" />
<asp:BoundField DataField="Industry" HeaderText = "Industry" />
<asp:BoundField DataField="SubIndustry" HeaderText = "Sub-Industry" />
<asp:BoundField DataField="Geo" HeaderText = "Geo" />
<asp:BoundField DataField="LocType" HeaderText = "Location" />
<%--<asp:buttonfield buttontype="Button" commandname="Select" headertext="" text="Select"/>--%>
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
<%--<asp:RadioButton ID = "rdSelect" runat="server" onclick = "RadioCheck(this)" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID")%>' />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
Any help will do. Thanks!
-
Hi, I have included a radiobutton on a gridview using HTML control. I tried getting the index of the row selected by the radio button, but I can't figure out how. I tried using the Request.Form["RadiobuttonName"] but it gives me the value on the cell not the row index. Here is the code When I click the button to retrieve the value.
protected void btnSelect_Click(object sender, EventArgs e)
{
string selectedValue = Request.Form["radSelect"];
int index = Convert.ToInt32(Request.Form["radSelect"]);
}And here is .aspx page
<asp:GridView ID="grdAll" runat="server" OnRowCommand="MPS_Selected" AutoGenerateColumns="false" OnRowDataBound = "grd_rowdatabound">
<Columns>
<asp:BoundField DataField="ID" HeaderText = "" Visible="false" />
<asp:BoundField DataField="Name" HeaderText = "MPS Account Name" />
<asp:BoundField DataField="Industry" HeaderText = "Industry" />
<asp:BoundField DataField="SubIndustry" HeaderText = "Sub-Industry" />
<asp:BoundField DataField="Geo" HeaderText = "Geo" />
<asp:BoundField DataField="LocType" HeaderText = "Location" />
<%--<asp:buttonfield buttontype="Button" commandname="Select" headertext="" text="Select"/>--%>
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
<%--<asp:RadioButton ID = "rdSelect" runat="server" onclick = "RadioCheck(this)" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID")%>' />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
Any help will do. Thanks!
What you have given is not a server side control.
<input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
is a client side html control. 2 things: 1. Convert it into server control using
runat="server"
to access it on server side. 2. In button select, loop through all the rows of grid, use FindControl to find radiobutton control. See if it is selected, if so use the row and it's values Again, using a radiobutton in gridview is little tricky and needs extra effort to group them together so that only one can be selected at a time. If you face issue, just look for article/question on it on this website itself (shared before with lots of users)Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]
-
What you have given is not a server side control.
<input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
is a client side html control. 2 things: 1. Convert it into server control using
runat="server"
to access it on server side. 2. In button select, loop through all the rows of grid, use FindControl to find radiobutton control. See if it is selected, if so use the row and it's values Again, using a radiobutton in gridview is little tricky and needs extra effort to group them together so that only one can be selected at a time. If you face issue, just look for article/question on it on this website itself (shared before with lots of users)Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]
Thanks for the reply. I tried using literal, but I am getting an error
Quote:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
in this line
output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");
-
Thanks for the reply. I tried using literal, but I am getting an error
Quote:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
in this line
output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");
Franco Cipriano wrote:
output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");
You forgot to mention the string to be replaced in {0} Something like:
string.Format("Hi {0}", "there!"); // gives "Hi there!"
Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]
-
Franco Cipriano wrote:
output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");
You forgot to mention the string to be replaced in {0} Something like:
string.Format("Hi {0}", "there!"); // gives "Hi there!"
Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]
Thanks so much! problem solved!
-
Thanks so much! problem solved!
Good to know! :thumbsup:
Sandeep Mewara [My last tip/trick]: Server side Delimiters in ASP.NET[^]