如何从插入到innerHtml中的单选按钮获取值

时间:2009-11-23 16:20:13

标签: asp.net radio-button innerhtml

我有一个带有单选按钮列的表。我设法使单选按钮列动态插入单元格(div如果有问题)。但是,在postback上,innerHtml尚未使用“checked”属性进行更新。

您能否告诉我如何(在服务器上)查看是否已选中单选按钮?

更多信息:这是在更新面板内的用户控件上。

This在我的主题上发表的帖子不错,但仍无效

4 个答案:

答案 0 :(得分:0)

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

只有在检查时才会获得非空值。

答案 1 :(得分:0)

确保在回发时正确重建您的页面元素。第一次插入单选按钮的任何绑定过程都必须重新运行才能再次访问它们。

答案 2 :(得分:0)

以下是一个工作示例,首先我通过方法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);
}

然后在代码后面我只使用下面的代码来获取无线电的值:

string value = Request["fldID"];

因此,请确保您尝试在服务器端获取单选按钮的名称。您应该在服务器端使用name属性,而不是id。

答案 3 :(得分:0)

任何原因你不能使用标准的asp:RadioButton并使用javascript来确保它是互斥的。之前我已经通过向radiobutton添加自定义属性然后使用js函数取消选中具有该属性的所有项目然后检查所选项目来完成此操作。这适用于IE问题,这会阻止groupname属性处理不同容器中的放射性物体。

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

并使用以下JS取消选中所有无线电,然后检查所需的无线电。 注意我使用InputAttributes而不是Attributes,因为radiobutton包含在span标记内,因此InputAttributes用于添加到实际输入控件而不是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; 
}

然后,您可以将radioButton.Checked作为CS文件中的属性公开,并将其重新用作控件。

相关问题