2016-12-05 86 views
0

我正在ASP.Net中使用我的GridView表。 首先,當我點擊我的GridView中的編輯鏈接按鈕時,我將能夠使用文本框和下拉列表對其進行編輯。如何在Gridview中啓用DropDownList選擇第一個DropDownList時

這是我的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

+0

如果我明白,你想要一個ca scading combobox? – KyloRen

+0

@KyloRen是的,我想要一個級聯組合框。 – chobowski

+0

其實我想在編輯時在Gridview中級聯DropDownList – chobowski

回答

1

首先,你需要添加一個OnSelectedIndexChangedddl_gvBusinessUnit並設置AutoPostBacktrue

<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