简体   繁体   中英

Javascript in asp.net with radiobuttonlist

This is my Default.aspx page

                <td>Radio </td>
                <td>
                 <asp:RadioButtonList ID="RadioButtonList1" runat="server" >
                    <asp:ListItem Text = "One" Value = "1"></asp:ListItem>
                    <asp:ListItem Text = "Two" Value = "2"></asp:ListItem>
                    <asp:ListItem Text = "Three" Value = "3"></asp:ListItem>
                </asp:RadioButtonList>
                </td>
                <td><div id="Div1" style="color:Red;display:none"></div></td>
            </tr>

            <tr>
                <td>Dropdown </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" >
                         <asp:ListItem>Select</asp:ListItem>
                         <asp:ListItem>Male</asp:ListItem>
                         <asp:ListItem>Female</asp:ListItem>
                    </asp:DropDownList>
                </td>                   
            </tr>
        </table>    
<br />
<center> 
        <asp:Button ID="Button1" runat="server" Text="Edit"/>
        <asp:Button ID="Button2" runat="server" Text="DropDown" OnClientClick="return ValidateDropDown('DropDownList1');" />
        <asp:Button ID="Button3" runat="server" Text="RadioValidate" OnClientClick = "return ValRad(document.getElementById('RadioButtonList1'));" />
</center>

This is my separated javascript file:

function ValRad(CntrlID) {
          alert("Radio");
          var RB1 = CntrlID;
          alert("1");
          var radio = RB1.getElementsByTagName("input");
          alert("2");
          var isChecked = false;
          alert("3");
              for (var i = 0; i < radio.length; i++) {
              if (radio[i].checked) {
              isChecked = true;
              break;
              }
         }
         if (!isChecked) {
         alert("Please select an item");
       }
     return isChecked;    
   }

I can't validate radiobuttonlist as well dropdownlist please help me... (SORRY for improper format and I'm new member ) please help me

For Radio Button List You can also use RequiredFieldValidator .

  <asp:RadioButtonList ID="rblChoose" runat="server" RepeatDirection="Horizontal" 
 RepeatLayout="Flow">
   <asp:ListItem Text="a" Value="a" />
   <asp:ListItem Text="b" Value="b" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rfvChoose" runat="server" 
ControlToValidate="rblChoose" ErrorMessage="Choose option." 
ForeColor="red" SetFocusOnError="true"></asp:RequiredFieldValidator>

For DropDown

<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>

<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="ddl" InitialValue="Please select" ErrorMessage="Please select something" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM