簡體   English   中英

如果在GridView內部選中復選框,則驗證下拉列表

[英]validate dropdown if checkbox checked inside gridview

<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;
        }
    }
}

我已經采用了上面的網格視圖,我想在選中復選框時啟用所需的字段驗證器以進行下拉。 我想不通過JavaScript來實現此服務器端。 我嘗試過但無法解決。 當復選框選中時,我嘗試啟用驗證器,否則它將被禁用。 我需要這方面的幫助。

剛剛修改了復選框更改事件,並向復選框和下拉列表分配了驗證組。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM