如果在gridview中选中复选框,则验证下拉列表 [英] validate dropdown if checkbox checked inside gridview

查看:79
本文介绍了如果在gridview中选中复选框,则验证下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   asp:GridView     ID   =  gvAttributes     runat   =  server  

AutoGenerateColumns = false EnableModelValidation = True

< span class =code-attribute> CssClass = GridViewStyle onrowdatabound = gvAttributes_RowDataBound >
< >
< asp:TemplateField HeaderText = 选择 >
< ItemTemplate >
< asp:CheckBox < span class =code-attribute> ID = cbxAttributescheck runat = 服务器 文本 =' '

<%# Eval(AttributeName) > AutoPostBack =trueOnCheckedChanged =cbxAttributescheck_CheckedChanged/>
< asp:HiddenField ID = hdnAttributeValue runat = 服务器 =' '

<%# Eval(AttributeID) > '/>
< asp:标签 ID = lblDelete runat = 服务器 文字 =' '

<%# Eval (AttributeID) > 'Visible =false> < / asp:Label >
< / ItemTemplate >
< / asp:TemplateField >
< asp: TemplateField HeaderText = 数据类型 >
< ItemTemplate >
< asp:DropDownList ID = ddlAttributesDataType runat = 服务器 >
< / asp:DropDownList >
< span class =code-keyword>< asp:RequiredFieldValidator ID = rfvAttributeType runat = server

< span class =code-attribute> < span class =code-attribute> ErrorMessage = * 已启用 = false

< span class =code-attribute> ControlToValidate = ddlAttributesDataType InitialValue = 0 >
< / asp:RequiredFieldValidator >
< / ItemTemplate > ;
< / asp:TemplateField >
< asp:TemplateField HeaderText = 允许空值 >
< ItemTemplate >
< asp:CheckBox ID = cbxAttributesisnull runat = server / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:GridView >



< pre lang =c#> protected void cbxAttributescheck_CheckedChanged( object sender,EventArgs e)
{
CheckBox cbx =(CheckBox)sender;
string CurrentCbxId =((CheckBox)sender).ClientID;
foreach (GridViewRow Row in gvAttributes.Rows)
{
if (((CheckBox)Row.FindControl( cbxAttributescheck ))。ClientID.Equals(CurrentCbxId)&& cbx.Checked)
{
RequiredFieldValidator rfvAttributeType =(RequiredFieldValidator)Row.FindControl( rfvAttributeType);
rfvAttributeType.Enabled = true ;
}
}
}



我按照上面的网格视图。

我想启用选中复选框时,必填字段验证器用于下拉列表。

我想通过JavaScript来做这个服务器端。

我试过但无法解决问题。我试图启用验证器,当选中复选框时,否则它将被禁用。

我需要帮助。

解决方案

验证器需要ValidationGroup属性才能使用一些值,分配一个验证组(比如' 'validate'')复选框,下拉列表以及要激活验证的按钮。试一试并发布您的结果。


<asp:GridView ID="gvAttributes" runat="server"

              AutoGenerateColumns="false" EnableModelValidation="True"

              CssClass="GridViewStyle" onrowdatabound="gvAttributes_RowDataBound">
  <Columns>
    <asp:TemplateField HeaderText="Select">
      <ItemTemplate>
        <asp:CheckBox ID="cbxAttributescheck" runat="server" Text=''

        <%# Eval("AttributeName") %> AutoPostBack="true" OnCheckedChanged="cbxAttributescheck_CheckedChanged"/>
        <asp:HiddenField ID="hdnAttributeValue" runat="server" Value=''

        <%# Eval("AttributeID") %>' />
        <asp:Label ID="lblDelete" runat="server" Text=''

          <%# Eval("AttributeID") %>'   Visible="false"></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Data Type" >
      <ItemTemplate>
        <asp:DropDownList ID="ddlAttributesDataType" runat="server">
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="rfvAttributeType" runat="server"

                                    ErrorMessage="*"  Enabled="false"

                                    ControlToValidate="ddlAttributesDataType" InitialValue="0">
        </asp:RequiredFieldValidator>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Allow Nulls" >
      <ItemTemplate>
        <asp:CheckBox ID="cbxAttributesisnull" runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>


protected void cbxAttributescheck_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cbx = (CheckBox)sender;
    string CurrentCbxId = ((CheckBox)sender).ClientID;
    foreach (GridViewRow Row in gvAttributes.Rows)
    {
        if (((CheckBox)Row.FindControl("cbxAttributescheck")).ClientID.Equals(CurrentCbxId) && cbx.Checked)
        {
            RequiredFieldValidator rfvAttributeType = (RequiredFieldValidator)Row.FindControl("rfvAttributeType");
            rfvAttributeType.Enabled = true;
        }
    }
}


I took grid view as above.
I want to enable the required field validator for drop down, when check box is checked.
I want to do this server side not through JavaScript.
I tried but unable to work out. I tried to enable the validator, when check box checked else it will be disabled.
I need assistance on this.

解决方案

a validator needs ValidationGroup property to have some value to work, assign a Validation Group (say ''validate'') to check box, drop down and the button on which you want to fire validation. Give it a try and post your outcome.


这篇关于如果在gridview中选中复选框,则验证下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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