【问题标题】:How to Enable a DropDownList in Gridview When Selected the First DropDownList选择第一个 DropDownList 时如何在 Gridview 中启用 DropDownList
【发布时间】:2017-04-19 08:24:23
【问题描述】:

我正在 ASP.Net 中处理我的 GridView 表。 首先,当我单击 GridView 中的编辑链接按钮时,我将能够使用文本框和下拉列表对其进行编辑。

这是我的 GridView 的图像。

因此启用了业务部门,但未启用部门、子部门、分类和子分类。 我的问题是,当我选择业务单位时,下拉列表部门应该被启用,并且能够根据我选择的业务单位选择值等等。因此,下一个下拉列表将取决于您选择的第一个下拉列表。我已经在互联网上搜索过这个并尝试了不同的解决方案,但我仍然感到困惑,不知道它会如何工作。

这是我在 ASP.Net 中的代码

 <asp:GridView ID="gvGroup" runat="server" AllowPaging="false" 
                                AllowSorting="true" AutoGenerateColumns="False" BorderColor="Silver" 
                                BorderWidth="1px" Height="104px" ShowFooter="true" ShowHeader="true" 
                                style="margin-right: 0px" Width="77%">
                                <RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
                                <Columns>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" 
                                        HeaderStyle-ForeColor="White" HeaderStyle-Width="10px" HeaderText="" 
                                        ItemStyle-Width="10px">
                                        <ItemTemplate>
                                            <%# Container.DataItemIndex + 1 %>
                                        </ItemTemplate>
                                        <HeaderStyle BackColor="#666666" ForeColor="White" />
                                        <ItemStyle Width="10px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
                                        <ItemTemplate>
                                            <asp:Label ID="lblMasterID" runat="server" Text='<%#Bind("MASTERID") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSEQID" runat="server" Text='<%#Bind("SEQID") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt" 
                                        HeaderStyle-ForeColor="White" HeaderText="Job Title" ItemStyle-Font-Size="9pt" 
                                        ItemStyle-Width="140px">
                                        <ItemTemplate>
                                            <asp:Label ID="lblJobTitle" runat="server" Text='<%#Bind("JOBTITLE") %>' 
                                                Width="140px"></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtJT" runat="server" CssClass="ehr_textbox" 
                                                Text='<%#BIND("JOBTITLE") %>' Width="140px"></asp:TextBox>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:TextBox ID="txtJobTitleAdd" runat="server" Width="110px">
                                            </asp:TextBox>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
                                        <ItemStyle Font-Size="9pt" Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt" 
                                        HeaderStyle-ForeColor="White" HeaderText="Business Unit" 
                                        ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
                                        <ItemTemplate>
                                            <asp:Label ID="lblBusinessUnit" runat="server" 
                                                Text='<%#Bind("BUSINESS_UNIT") %>' Width="140px"></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" CssClass="ehr_dropdown" 
                                                Width="140px">
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlBusinessUnitAdd" runat="server" Width="110px">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
                                        <ItemStyle Font-Size="9pt" Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt" 
                                        HeaderStyle-ForeColor="White" HeaderText="Division" ItemStyle-Font-Size="9pt" 
                                        ItemStyle-Width="140px">
                                        <ItemTemplate>
                                            <asp:Label ID="lblDivision" runat="server" Text='<%#Bind("DIVISION") %>' 
                                                Width="140px"></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddl_gvDivision" Enabled="false" runat="server" CssClass="ehr_dropdown" 
                                                Width="140px">
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlDivisionAdd" runat="server" Width="110px">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
                                        <ItemStyle Font-Size="9pt" Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt" 
                                        HeaderStyle-ForeColor="White" HeaderText="Sub-Division" 
                                        ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSubDivision" runat="server" Text='<%#Bind("SUB_DIVISION") %>' 
                                                Width="140px"></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddl_gvSubDivision" Enabled="false" runat="server" CssClass="ehr_dropdown" 
                                                Width="140px">
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlSubDivisionAdd" runat="server" Width="110px">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
                                        <ItemStyle Font-Size="9pt" Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt" 
                                        HeaderStyle-ForeColor="White" HeaderText="Classification" 
                                        ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
                                        <ItemTemplate>
                                            <asp:Label ID="lblClassification" runat="server" 
                                                Text='<%#Bind("CLASSIFICATION") %>' Width="140px"></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddl_gvClassification" Enabled="false" runat="server" 
                                                CssClass="ehr_dropdown" Width="140px">
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlClassification" runat="server" Width="110px">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
                                        <ItemStyle Font-Size="9pt" Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt" 
                                        HeaderStyle-ForeColor="White" HeaderText="Sub-Classification" 
                                        ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSubClassification" runat="server" 
                                                Text='<%#Bind("SUB_CLASSIFICATION") %>' Width="140px"></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="ddl_gvSubClassification" Enabled="false" runat="server" 
                                                CssClass="ehr_dropdown" Width="140px">
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:DropDownList ID="ddlSubClassification" runat="server" Width="110px">
                                            </asp:DropDownList>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
                                        <ItemStyle Font-Size="9pt" Width="140px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderStyle-BackColor="#666666" ItemStyle-Font-Size="9pt">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnkRemove" runat="server" CommandName="Delete" 
                                                OnClientClick="return confirm('Do you want to delete?')" Text="Delete"></asp:LinkButton>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:LinkButton ID="btnAdd" runat="server" CommandName="Add" 
                                                OnClick="AddgvGroup" Text="Add"></asp:LinkButton>
                                        </FooterTemplate>
                                        <HeaderStyle BackColor="#666666" />
                                        <ItemStyle Font-Size="9pt" />
                                    </asp:TemplateField>
                                    <asp:CommandField HeaderStyle-BackColor="#666666" HeaderStyle-Width="10px" 
                                        ItemStyle-Font-Size="9pt" ItemStyle-Width="10px" ShowEditButton="True">
                                        <HeaderStyle BackColor="#666666" />
                                        <ItemStyle Font-Size="9pt" />
                                    </asp:CommandField>
                                </Columns>
                                <PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
                                <EmptyDataTemplate>
                                    <div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
                                        No record found.
                                    </div>
                                </EmptyDataTemplate>
                                <HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial" 
                                    Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
                                <AlternatingRowStyle BackColor="Gainsboro" />
                            </asp:GridView>

这是我在 VB.Net 中的代码

Protected Sub gvGroup_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGroup.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
            Dim ddlBusinessUnit As DropDownList = DirectCast(e.Row.FindControl("ddl_gvBusinessUnit"), DropDownList)
            'bind dropdown-list
            Dim sqlstr As String
            Dim dt As DataTable = New DataTable()
            sqlstr = "Select BUSINESS_UNIT from BUSINESS_UNIT_TBL"
            dt = ehr_utils.DataTable(sqlstr)
            ddlBusinessUnit.DataSource = dt
            ddlBusinessUnit.DataTextField = "BUSINESS_UNIT"
            ddlBusinessUnit.DataValueField = "BUSINESS_UNIT"
            ddlBusinessUnit.DataBind()
            Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
            ddlBusinessUnit.SelectedValue = dr("BUSINESS_UNIT").ToString()
        End If
    End If

End Sub

知道我怎么可能做到这一点吗?我也试过OnSelectedIndexChanged

【问题讨论】:

  • 如果我理解,您想要一个级联组合框?
  • @KyloRen 是的,我想要一个级联组合框。
  • 其实我想在编辑时在 Gridview 中有一个级联的 DropDownList
  • 填充下拉列表的数据在哪里?我以为这是 C# 代码,但你的是 VB。

标签: asp.net vb.net gridview drop-down-menu


【解决方案1】:

首先您需要将OnSelectedIndexChanged 添加到ddl_gvBusinessUnit 并将AutoPostBack 设置为true

<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_gvBusinessUnit_SelectedIndexChanged">

然后在后面的代码中

protected void ddl_gvBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
    //cast the sender back to a dropdownlist
    DropDownList dropDownList1 = sender as DropDownList;

    //get the selectedvalue
    string value = dropDownList1.SelectedValue;

    //find the other dropdownlist in the correct row by using the editindex
    DropDownList dropDownList2 = gvBusinessUnit.Rows[gvBusinessUnit.EditIndex].FindControl("DropDownList2") as DropDownList;

    //do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
    dropDownList2.BackColor = Color.Red;
}

VB

Protected Sub ddl_gvBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    'cast the sender back to a dropdownlist
    Dim dropDownList1 As DropDownList = CType(sender,DropDownList)

    'get the selectedvalue
    Dim value As String = dropDownList1.SelectedValue

    'find the other dropdownlist in the correct row by using the editindex
    Dim dropDownList2 As DropDownList = CType(gvBusinessUnit.Rows(gvBusinessUnit.EditIndex).FindControl("DropDownList2"),DropDownList)

    'do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
    dropDownList2.BackColor = Color.Red
End Sub

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 2014-10-22
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多