English 中文(简体)
How can I get value from radio-button inserted into innerHtml
原标题:

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

最佳回答

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.

问题回答

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

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

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签