views:

729

answers:

4

I have sort of a table with a radio-button column. I managed to make radio-button column work dynamically inserting into a cell (div if matter). But, on postback innerHtml hasn't been updated with "checked" attribute.

Could you give me an idea how can I find out (on the server) if radio-button has been checked?

More info: This is on user control inside update panel.

This would be good post on my topic, still doesn't help

A: 

Check Form.Request("radio-name") != null

You only get a non-null value when it's been checked.

Gurdas Nijor
doesn't work :(
Oleg Kalenbet
Oleg, "radio-name" needs to be substituted with the value of the "name" attribute of your checkbox (not the id attribute.)
Gurdas Nijor
Request.Form[rbtnReplaced.ClientID] or Request[rbtnReplaced.ClientID]?Both of them null
Oleg Kalenbet
Neither. Try to find the value of the "name" attribute; not the ID attribute. Attempt a view source on your page, to see what the name of the radio button is.
Gurdas Nijor
A: 

Make sure your page elements are being rebuilt correctly on postback. Any binding process that inserted the radio buttons the first time around will have to be re-run before you can access them the second time.

Mark Holland
I can see innerHtml, but it looks exactly as I inserted it.
Oleg Kalenbet
A: 

Here is a working example, first I add radios to my webform by the method you linked :

function addRadio()
{
   try{  
        rdo = document.createElement('<input type="radio" name="fldID" />');  
    }catch(err){  
     rdo = document.createElement('input');  
    }  
    rdo.setAttribute('type','radio');  
    rdo.setAttribute('name','fldID'); 
    document.getElementById('container').appendChild(rdo);
}

Then at code behind I used only the code below to get the radio's value :

string value = Request["fldID"];

So, be sure you're trying to get the name of the radio buttons at server side. You should use name attribute at server side, not id.

Canavar
protected void Page_Init(object sender, EventArgs e) { div.InnerHtml = "<input type='radio' name='gnReplace' /><input type='radio' name='gnReplace' />"; } protected void Button1_Click(object sender, EventArgs e) { Console.WriteLine(Request["gnReplace"]); }Puzzled with value "on" which is returned regardless to selection. If name is what is call RadioGroup, what kind of value should I get?
Oleg Kalenbet
A: 

Any reason you cannot use a standard asp:RadioButton and use javascript to ensure it is mutually exclusive. I have done this before by adding a custom attribute to the radiobutton and then using a js function to uncheck all items with that attribute and then check the selected one. This works around the IE issue which prevents the groupname attribute from working on radioboxes that are in different containers.

radioButton.InputAttributes.Add("ClientGroupName", "grpRadioList");
radioButton.InputAttributes.Add("onclick",
  string.Format(
     "javascript:radiobuttonToggle('{0}','ClientGroupName','grpRadioList');"  
      ,radioButton.ClientID));

and use the following JS to uncheck all radios and then check the one you want. Note i used InputAttributes instead of Attributes as the radiobutton is wrapped inside a span tag so InputAttributes is for items added to the actual input control rather than the span.

function radiobuttonToggle(selectedRB, attribName, attribValue)
{
    var objRadio = document.getElementById(selectedRB);

    for(i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio')
        {
            if(elm.getAttribute(attribName) == attribValue)
                elm.checked = false;
        }
    }
    objRadio.checked = true; 
}

You can then expose radioButton.Checked as a property in your CS file and reuse this as a control.

Kaius