asp.net多个网格视图与radiobuttons

时间:2014-09-26 13:15:15

标签: javascript asp.net

我有两个带有无线电按钮的网格视图。但是,我不希望他们彼此依赖,例如。

Gridview 1:

  • 价值1 - RadioButton
  • 价值2 - RadioButton
  • Value 3 - RadioButton

Gridview 2:

  • 价值1 - RadioButton
  • 价值2 - RadioButton
  • Value 3 - RadioButton

如果我在gridview 1中选择值1,并在gridview 3中选择值3,则我的第一个选定的单选按钮将被取消选择。

JavaScript代码:

    <script>
    function SelectOneDep(objId) {
        var grd = document.getElementById("<%=GridView1.ClientID %>");
        var rdoArray = grd.getElementsByTagName("input");
        for (i = 0; i <= rdoArray.length - 1; i++) {
            if (rdoArray[i].type == 'radio') {
                if (rdoArray[i].id != objId) {
                    rdoArray[i].checked = false;
                }
            }
        }
    }

    function SelectOneRet(objId) {
        var grd = document.getElementById("<%=GridView2.ClientID %>");
        var rdoArray = grd.getElementsByTagName("input");
        for (i = 0; i <= rdoArray.length - 1; i++) {
            if (rdoArray[i].type == 'radio') {
                if (rdoArray[i].id != objId) {
                    rdoArray[i].checked = false;
                }
            }
        }
    }
    </script> 

Gridviewone:

<input type="radio" ID="rbDep" name="InpOne" runat="server" onclick="SelectOneDep(this.id);"/>  

Gridviewtwo:

<input type="radio" ID="rbRet" name="InpTwo" runat="server" onclick="SelectOneRet(this.id);"/>

1 个答案:

答案 0 :(得分:1)

您需要将 GroupName attriibute添加到单选按钮以对其进行分组。这样,一组中的选择不会影响另一组的选择

对于GridView1

<asp:RadioButton GroupName="FirstGrid" id="rbDep" runat="server" OnClick="javascript:SelectRadiobutton(this.id)" />

对于GridView2

<asp:RadioButton GroupName="SecondGrid" id="rbDep" runat="server" OnClick="javascript:SelectRadiobutton(this.id)" />