 |
|
 |
I use this inside lists or grids. You can have multiple lists or grids on single page and grouping will work properly on each list or grid.
<asp:RadioButton runat="server"
ID="DefaultRadioButton"
GroupName="DefaultItems"
OnClick="DefaultRadioButton_OnClick(this, {});"
/>
<script type="text/javascript" language="javascript">
function DefaultRadioButton_OnClick(sender, e) {
<span class="style1"> var idPath = sender.id.split('_');
var buttonId = idPath[idPath.length - 1];
<span class="style1"> var namePath = sender.name.split('$');
var buttonName = namePath[namePath.length - 1];
var buttonRegex = new RegExp("\\u0024" + buttonName + "$");
<span class="style1"> var regex = new RegExp("_ctrl([0-9]+)_" + buttonId + "$");
var listId = sender.id.replace(regex, "");
<span class="style1"> var items = document.getElementsByTagName("input");
for (var i = 0; i < items.length; i++) {
var item = items[i];
<span class="style1"> <span class="style1"> if (item.type == "radio"
&& item.checked
&& item.id.indexOf(listId) == 0
&& item.name.match(buttonRegex)
&& item != sender
) item.checked = false;
}
}
</script>
|
|
|
|
 |
|
 |
hi this is very useful,i am try this same in javascript,but it is come good,so anybody have the code for javascript,plz send in mail,
by
hajaworld@yahoo.co.in
haja
|
|
|
|
 |
|
 |
how to using:
[asp:RadioButton id="check" onclick="onCheckedChangedRadioButton(this.name, 'check');" runat="server"][/asp:RadioButton]
ibizq
|
|
|
|
 |
|
 |
<script language="javascript">
function onCheckedChangedRadioButton(selectName, aspRadioID)
{
regular = new RegExp(':' + aspRadioID + '$');
for(i = 0; i < document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i];
if(elm.type == 'radio') {
if(regular.test(elm.name) && elm.name != selectName) {
elm.checked = false;
}
}
}
}
</script>
how to use:
<asp:RadioButton id="check" onclick="onCheckedChangedRadioButton(this.name, 'check');" runat="server">
result: not refresh page when select items.
ibizq
|
|
|
|
 |
|
|
 |
|
 |
This approach is okay sometimes, but as a general rule in user interface design, you don't want to give the user a bunch of radio buttons and then instantly react to their click. What if they accidentally clicked the wrong button?
Ideally you want them to be able to click until they are happy with their selection, and perhaps fill out other controls on the form, then click a submit button.
The problem that happens is if you set the radio button's group property in order to get the radio buttons to behave as a group, the datagrid bind process changes not only their ID property, but also their group property (which translates to "name" on the rendered input control), so none of the radio buttons are declared as part of the same group.
I'm going to try to find a way to work around that.
|
|
|
|
 |
|
 |
yup, i've stumbled on the same prob, which is what led me to this page in the first place... have you had any luck with restricting user selections to one radio button? otherwise, this solution is no different from using check boxes, which is not always what you want
pete
|
|
|
|
 |
|
 |
Pete, I did finally get a solution to work (depending on what you mean by "work"), but it's pretty ugly, actually. It involves putting a label control in the row and writing out the HTML for the radio button in the ItemDataBound event. It winds up that you can't really use the ASP.NET model for this...meaning you have no radio button control that you can get/set properties on. So, I advise you to take another approach if possible. If I haven't talked you out of it, maybe the following code snippet from my event handler will give you some idea how to go about it: <code> if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==ListItemType.Item) { Label lbl = (Label)(e.Item.FindControl("lblPlaceHolder")); // if we have returned to this page after the user has already made // a selection, we need to re-select their original choice. string sCheckedClause = ""; string sPK = GetCurrentlySelectedPrimaryKey(); if (sPK != "" && e.Item.Cells[1].Text == sPK) { sCheckedClause = " CHECKED "; } lbl.Text = "<input type='radio' name='rgMyGroup' id='rbSelRow'" + sCheckedClause + "value='" + e.Item.Cells[1].Text + "' />" ; } </code> Sorry for the ugly formatting, I'm in a rush. --JV
|
|
|
|
 |
|
 |
ok, i've found another approach, pretty simple:
http://www.codeproject.com/aspnet/RadioButtonList_Demo.asp
i added the two event handlers this bloke outlines, and it does the trick:
private void dgAllData_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Find the RadioButton control
RadioButton oRb = ((RadioButton)e.Item.FindControl("rb"));
if(oRb != null)
{
//Wire the RadioButton's event
oRb.CheckedChanged += new
System.EventHandler(this.rb_CheckedChanged);
}
}
private void rb_CheckedChanged(object sender, System.EventArgs e)
{
//Find the current selected RadioButton
RadioButton oRb1 = (RadioButton)sender;
foreach(DataGridItem oItem in dgAllData.Items)
{
//Find the previous selected RadioButton
RadioButton oRb2 = (RadioButton)oItem.FindControl("rb");
if (! oRb2.Equals(oRb1))
oRb2.Checked = false;
}
}
|
|
|
|
 |
|
 |
Could you convert this code into VB for me? Thank you very much!
|
|
|
|
 |
|
 |
Did you get a conversion to vb.net for Mustafa's code of adding radio button to data grid control ?
Can u post it if u did ?
|
|
|
|
 |
|
 |
insert into your datagrid: <asp:TemplateColumn> <ItemTemplate> <asp:RadioButton id="rbSelection" runat="server" OnCheckedChanged="rbChanged" AutoPostBack="true"> </asp:RadioButton> </ItemTemplate> </asp:TemplateColumn>
insert into your code behind page: Public Sub rbChanged(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = sender.ClientID Dim i As DataGridItem Dim rb As RadioButton For Each i In dtgNew.Items rb = i.FindControl("rbSelection") If Not rb.ClientID = sender.ClientID Then rb.Checked = False Else dtgNew.SelectedIndex = i.ItemIndex End If Next End Sub
I had to make a list of radio buttons that would function like a radiobutton list and select the record too. Thus, my code isn't an exact conversion of his but it is close and maintains the same basic functionality.
|
|
|
|
 |
 | oops  |  | Specificity | 7:49 22 Jun '05 |
|
 |
take out the line that reads:
Label1.Text = sender.ClientID
and it should work.
|
|
|
|
 |