Hi guys i used a grid view to insert multiple rows of record into the database . this is how my grid view looks like
The problem is that all radio buttons can be selected , i need only 1 radio button to be selected for each row .. . My web form works in the way where user have to select the correct answer out of the two textboxes with a radio button then i will have to submit the checked answer to database . is this possible?
aspx : `
<asp:ButtonField Text="SingleClick" CommandName="SingleClick"
Visible="False" />
<asp:TemplateField HeaderText="Question">
<ItemTemplate>
<br />
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RadioButton ID="RadioButton1" runat="server" />
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RadioButton ID="RadioButton2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="+" onclick="btnAdd_Click1" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>`
code behind :
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
//Clear the existing selected row
foreach (GridViewRow oldrow in GridView1.Rows)
{
((RadioButton)oldrow.FindControl("RadioButton1")).Checked = false;
}
//Set the new selected row
RadioButton rb = (RadioButton)sender;
GridViewRow row = (GridViewRow)rb.NamingContainer;
((RadioButton)row.FindControl("RadioButton1")).Checked = true;
}
I assume you're talking about ASP.NET. Did you try grouping the radio buttons together?
Here's an example.
<asp:RadioButton id="rBtn1" GroupName="Fruits"
Text="Apple" runat="server"/>
<asp:RadioButton id="rBtn2" GroupName="Fruits"
Text="Orange" runat="server"/>
<asp:RadioButton id="rBtn3" GroupName="Colors"
Text="Blue" runat="server"/>
<asp:RadioButton id="rBtn4" GroupName="Colors"
Text="Red" runat="server"/>
When Apple is selected, Orange can't be selected. If Orange is selected while Apple is selected, Apple will automatically be un-selected. But when Apple is selected, when you click Blue, Apple won't be un-selected.
Just try to use a javascript
function like
<script language="javascript" type="text/javascript">
function SelectSingleRadiobutton(rdbtnid) {
var rdBtn = document.getElementById(rdbtnid);
var rdBtnList = document.getElementsByTagName("input");
for (i = 0; i < rdBtnList.length; i++) {
if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id)
{
rdBtnList[i].checked = false;
}
}
}
</script>
Grid View
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" DataKeyNames="UserId" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton id="rdbUser" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="Name"/>
<asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
Try to check aspdotnet
Check this asp.net
Hope it works.
<input id="HDDepMood_0" name="DepMood" type="radio" runat="server" />
Same name for the one group.
I think you may proceed like this :-
protected void rbtnSelect_CheckedChanged(object sender, EventArgs e)
{
RadioButton selectButton = (RadioButton)sender;
GridViewRow gvrow = (GridViewRow)selectButton.Parent.Parent;
var radio = (RadioButton)gvRow.FindControl("rdo");// rdo is ID of your radio button:-
if(radio[0].id=selectButton.id)
radio[1].checked=false;
else if(radio[1].id=selectButton.id)
radio[0].checked=false;
}
Just add some script to your page to set the groupname:
$('input[type=radio]').each(function () {
var array = $(this).attr('name').split('$');
$(this).attr('name', array[array.length - 1]);
});
This wil adjust the name rendered to the correct group name.
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript">
function singleRbtnSelect(chb) {
$(chb).closest("table").find("input:radio").prop("checked", false);
$(chb).prop("checked", true);
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.