在asp.net中验证RadioButton [英] Validating RadioButton in asp.net

查看:101
本文介绍了在asp.net中验证RadioButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 3.5使用VB

经过数小时的研究和反复试验,我发现自己仍然坚持尝试验证单个单选按钮(不是单选按钮列表).在这个问题的结尾,我将解释为什么我不认为我可以使用单选按钮列表(可能只是我不想使用!).

简而言之,我有一个相当大的问题矩阵需要回答.答案是通过单选按钮选项而不是下拉菜单提供的(客户需要单选按钮).

我需要验证每个问题在表单提交时都选择了一个单选按钮.我希望验证发生在客户端.

我创建了JavaScript以返回验证,但是我继续遇到各种障碍,试图显示错误消息.

第一个障碍是不使用单选按钮列表,我很难获得所需的单选按钮以通过组名进行验证.我发现了各种各样的例子,这些例子似乎可以奏效,但从未奏效.我结束了下面发布的功能.漂亮的不是,但是它可以工作.可悲的是,我将需要为每行使用这些功能之一,但目前我不在乎.

第二个障碍是CustomValidator需要一些输入元素作为其ControlToValidate.我没有这样的控件,因为RadioButton不是有效的输入元素(奇数?).因此,要解决此问题,我插入了一个文本框以作为控件进行验证.显然,我不希望显示此文本框,因此我将样式设置为显示:无.好吧,这没有用,因为CustomValidator似乎继承了ControlToValidate的display属性(再次,是奇数?);因此,当TextBox隐藏时,来自验证器的错误消息也将隐藏.我已经确认了验证器已被触发,并且如果display属性设置为none,验证器将起作用.

好的,很抱歉,冗长的介绍,但我知道如果我不回答这些问题,无论如何都会要求全部解决.

因此,在多汁的东西上,这是我现在要编写的代码.

CSS:

.NET 3.5 using VB

After hours of research and trial-and-error, I find myself still stuck on trying to validate individual radio buttons (NOT radiobuttonlist). At the end of this question, I''ll explain why I don''t think I can use a radiobuttonlist (which may just be that I don''t want to!).

In short, I have a rather large matrix of questions that need answered. Answers are provided by radiobutton option, not dropdown (client demands radiobuttons).

I need to validate that each question has a radiobutton selected at form submission. I want the validation to occur client side.

I created javascript to return the validation, but I continue to run into various roadblocks trying to get the error message to display.

First roadblock was by not using radiobuttonlist, I was having difficulty obtaining the desired radio buttons to validate by group name. I found various examples that seemed like it would have worked, but never did. I ended up with the function I have posted below. Pretty it is not, but it works. Sadly, I will need one of these functions for each row, but at this point, I don''t care.

Second roadblock was that the CustomValidator requires some input element as its ControlToValidate. I have no such control since RadioButton is not a valid input element (odd?). So to circumnavigate this problem, I inserted a text box to pass as the control to validate. Obviously I would not want this text box to show, so I set a style to display: none. Well, this didn''t work because it seems the CustomValidator inherits the display property of the ControlToValidate (again, odd?); so when the TextBox is hidden, the error message from the validator is also hidden. I have confirmed the validator is fired, and the validator works if the display property is set to something other than none.

Okay, sorry for the long intro, but I know if I don''t answer that stuff it will all be asked anyway.

So onto the juicy stuff, here''s the code I have to this point.

CSS:

.DoNotShow
{
    display: none;
}
.Show
{
    display: block;
}



javascript:



javascript:

function validateEvaluationTableRow1(oSrc, args)
{
   var rbtnValue = null;
   var valueCol1 = document.getElementsByName('<%= rdoEvalSatRow1.ClientID %>')[0].checked;
   var valueCol2 = document.getElementsByName('<%= rdoEvalNeedsImprovRow1.ClientID %>')[0].checked;
   var valueCol3 = document.getElementsByName('<%= rdoEvalUnsatRow1.ClientID %>')[0].checked;
   var valueCol4 = document.getElementsByName('<%= rdoEvalUnableRow1.ClientID %>')[0].checked;
   var valueCol5 = document.getElementsByName('<%= rdoEvalNARow1.ClientID %>')[0].checked;
    
    alert(valueCol1 || valueCol2 || valueCol3 || valueCol4 || valueCol5);
    
    args.IsValid = (valueCol1 || valueCol2 || valueCol3 || valueCol4 || valueCol5);
 }



ASP.NET代码:



ASP.NET Code:

<table width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <th>
        </th>
        <th valign="top" align="center">
            Satisfactory
        </th>
        <th valign="top" align="center">
            Needs<br />
            Improvement
        </th>
        <th valign="top" align="center">
            Unsatisfactory
        </th>
        <th valign="top" align="center">
            Unable<br />
            To Evaluate
        </th>
        <th valign="top" align="center">
            N/A
        </th>
    </tr>
    <tr bgcolor="#E1E1EA">
        <td>
            <asp:CustomValidator ID="vldRow1" runat="server" ControlToValidate="TextBox1" Enabled="True"
                Height="18px" ErrorMessage="Selection Required<br>" SetFocusOnError="True" Visible="True" CssClass="Show"
                Display="Dynamic" ValidateEmptyText="true" ClientValidationFunction="validateEvaluationTableRow1" />
            <asp:Label ID="Label1" runat="server" Text="Label">Level of clinical activity</asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" Visible="true" Width="0"  CssClass="DoNotShow" />
        </td>
        <td align="center">
            <asp:RadioButton ID="rdoEvalSatRow1" runat="server" GroupName="Row1" />
        </td>
        <td align="center">
            <asp:RadioButton ID="rdoEvalNeedsImprovRow1" runat="server" GroupName="Row1" />
        </td>
        <td align="center">
            <asp:RadioButton ID="rdoEvalUnsatRow1" runat="server" GroupName="Row1" />
        </td>
        <td align="center">
            <asp:RadioButton ID="rdoEvalUnableRow1" runat="server" GroupName="Row1" />
        </td>
        <td align="center">
            <asp:RadioButton ID="rdoEvalNARow1" runat="server" GroupName="Row1" />
        </td>
    </tr>
    {... lots more rows ...}
</table>



我很想消除愚蠢的文本框;我可以使用javascript函数;我只需要验证器即可工作!

那么,我可以使用单选按钮列表吗?也许可以,但是我不知道一种将矩阵标题与每一列中的单个单选按钮对齐的好方法.这个矩阵很大,我还有其他几种具有相似矩阵的形式,因此以其他方式重做此矩阵也将花费很多工作.可悲的是,验证要求并不是该表格开发时的初始要求的一部分,但是客户认为拥有它会很好.

感谢您抽出宝贵的时间阅读所有这些内容.任何帮助和建议,将不胜感激!请记住,您在此处看到的代码显然是一个较大项目的子集,其中某些代码仅用于故障排除,因此请不要挂在命名或措辞上.



I would love to do away with the silly textbox; I''m okay with the javascript function; I just need the validator to work!

So, can I use radiobuttonlist? Perhaps, but I don''t know of a good way to align the header of the matrix with the individual radiobuttons in each column. And this matrix is pretty large and I have several other forms that have a similar matrix, so to redo this matrix in some other fashion is going to take a pretty lot of work too. Sadly, the validation requirement was not part the initial requirements when this form was developed, but the client decided it would be nice to have it.

Thanks for taking the time to read all this. Any help and suggestions would be greatly appreciated! Keep in mind the code you see here is obviously a subset of a larger project and some of it is done just for troubleshooting, so please don''t get hung-up on naming or wording.

推荐答案

好吧,我不会写您的代码,这只是我如何做的一个提示,但是它使用了jquery,这没什么大不了的,可以转换为纯JavaScript.这只是一个概念,可以给您一个想法.离开群组时,模糊事件应该起作用

Well, I''m not going to write your code a be a brogrammer, this is just a hint of how I would do it, but it uses jquery, which is no big deal, can be converted to just plain javascript. It''s just a concept to give you an idea. The blur event should work, when leaving the group

rb_MovieEditor_MovieDeliveryType = New RadioButtonList
     With rb_MovieEditor_MovieDeliveryType
         .ID = [ID] & "_rb_MovieEditor_MovieDeliveryType"
         .CssClass = [CssStyle_InputControl]
         .Style.Add(HtmlTextWriterStyle.Width, "90%")
         .Style.Add(HtmlTextWriterStyle.Color, "#FFFFFF")
         .Style.Add(HtmlTextWriterStyle.FontSize, "0.8em")
     End With
        td_MovieEditor_Basic_MovieDeliveryType_Field.Controls.Add(rb_MovieEditor_MovieDeliveryType)

''// Load the Video Types in OnInit
Load_Movie_DeliveryType()
rb_MovieEditor_MovieDeliveryType.Items.Add(New ListItem("Local - Your Web Server", "STATIC"))
rb_MovieEditor_MovieDeliveryType.Items.Add(New ListItem("Streaming - Other Web Server", "STREAMING"))



验证器



Validator


(文档).ready(function(){ initialBinding(); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); }); 函数EndRequestHandler(sender,args){ initialBinding(); } 函数initializeBinding(){ //在这里绑定事件 //收听RadioButtonList更改事件
(document).ready(function () { initiateBinding(); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); }); function EndRequestHandler(sender, args) { initiateBinding(); } function initiateBinding() { //Bind events here // Listen for the RadioButtonList Change Event


(''[id * ="__ rb_MovieEditor_MovieDeliveryType"]:input:radio'').blur(function(){ rb_MovieProgramming(); 返回false; }); }); 函数rb_MovieProgramming(){
(''[id*="_rb_MovieEditor_MovieDeliveryType"]:input:radio'').blur(function () { rb_MovieProgramming(); return false; }); }); function rb_MovieProgramming() {


这篇关于在asp.net中验证RadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆