2015-06-02 91 views
1

我有一個gridview,其中包含下拉菜單和文本框。 1下拉菜單將成爲所謂的「主」下拉菜單,當選定的索引更改時,同一行中的其他下拉列表將使用主下拉列表中的選定值進行數據綁定。在GridView中綁定下拉菜單當選擇在同一Gridview中的下拉菜單上的索引更改

我有這樣的代碼爲我的ASPX

<asp:GridView ID="gvEducations" runat="server" AutoGenerateColumns="false" 
     DataKeyNames="file2_id" onrowcancelingedit="gvEducations_RowCancelingEdit" 
     onrowdatabound="gvEducations_RowDataBound" 
     onrowdeleting="gvEducations_RowDeleting" onrowediting="gvEducations_RowEditing" 
     onrowupdating="gvEducations_RowUpdating"> 
     <Columns> 
      <asp:BoundField HeaderText="file2_id" DataField="file2_id" ReadOnly="true" Visible="False"/> 
      <asp:TemplateField HeaderText="Education Establishment"> 
       <ItemTemplate> 
        <%# Eval("education_establishment_code") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="ddlEditEducationEstablishment" runat="server" 
         OnSelectedIndexChanged="ddlEditEducationEstablishment_SelectedIndexChanged"> 
        </asp:DropDownList> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="School"> 
       <ItemTemplate> 
        <%# Eval("school_code") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="ddlEditSchool" runat="server"></asp:DropDownList> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="School Others" Visible="false"> 
       <ItemTemplate> 
        <%# Eval("school_others") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox ID="txtEditSchoolOthers" runat="server" MaxLength="81" 
         Text='<%# Eval("school_others") %>'></asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Start Date"> 
       <ItemTemplate> 
        <%# Convert.ToDateTime(Eval("start_date")).ToString("d")%> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox ID="txtEditStartDate" runat="server" type="date" 
         Text='<%# Bind("start_date", "{0:yyyy-MM-dd}") %>'></asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="End Date"> 
       <ItemTemplate> 
        <%# Convert.ToDateTime(Eval("end_date")).ToString("d")%> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox ID="txtEditEndDate" runat="server" type="date" 
         Text='<%# Bind("end_date", "{0:yyyy-MM-dd}") %>'></asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Branch Of Study 1"> 
       <ItemTemplate> 
        <%# Eval("branch_of_study_1_code") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="ddlEditBranchOfStudy1" runat="server"></asp:DropDownList> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Certificate"> 
       <ItemTemplate> 
        <%# Eval("certificate_code") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="ddlEditCertificate" runat="server"></asp:DropDownList> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Course Appraisal"> 
       <ItemTemplate> 
        <%# Eval("course_appraisal") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox ID="txtEditCourseAppraisal" runat="server" MaxLength="30" 
         Text='<%# Eval("course_appraisal") %>'></asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Branch Of Study 2"> 
       <ItemTemplate> 
        <%# Eval("branch_of_study_2_code") %> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="ddlEditBranchOfStudy2" runat="server"></asp:DropDownList> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Edit"> 
       <ItemTemplate> 
        <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" UseSubmitBehavior="false"/> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Update" UseSubmitBehavior="false"/> 
        <asp:Button ID="btnCanel" runat="server" Text="Cancel" CommandName="Cancel" UseSubmitBehavior="false"/> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Delete"> 
       <ItemTemplate> 
        <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" UseSubmitBehavior="false"/> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

代碼背後(的RowDataBound GridView的事件)

protected void gvEducations_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow && gvEducations.EditIndex == e.Row.RowIndex) 
     { 
      int file2Id = int.Parse(gvEducations.DataKeys[e.Row.RowIndex].Value.ToString()); 
      DataTable dt = new DataTable(); 
      dt = file2BLO.SelectSpecificFile2(file2Id); 
      DropDownList ddlEditEducationEstablishment = 
       (DropDownList)e.Row.FindControl("ddlEditEducationEstablishment"); 
      ddlEditEducationEstablishment.DataSource = 
       educationEstablishmentBLO.SelectAllEducationEstablishment(); 
      ddlEditEducationEstablishment.DataValueField = "education_establishment_code"; 
      ddlEditEducationEstablishment.DataTextField = "education_establishment"; 
      ddlEditEducationEstablishment.DataBind(); 
      ddlEditEducationEstablishment.SelectedValue = 
       dt.Rows[0]["education_establishment_code"].ToString(); 
      string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue; 
      DropDownList ddlEditSchool = (DropDownList)e.Row.FindControl("ddlEditSchool"); 
      ddlEditSchool.DataSource = 
       schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode); 
      ddlEditSchool.DataValueField = "school_code"; 
      ddlEditSchool.DataTextField = "school"; 
      ddlEditSchool.DataBind(); 
      ddlEditSchool.SelectedValue = dt.Rows[0]["school_code"].ToString(); 
      DropDownList ddlEditBranchOfStudy1 = (DropDownList)e.Row.FindControl 
       ("ddlEditBranchOfStudy1"); 
      ddlEditBranchOfStudy1.DataSource = branchOfStudyBLO. 
       SelectBranchOfEstablishment(educationEstablishmentCode); 
      ddlEditBranchOfStudy1.DataValueField = "branch_of_study_code"; 
      ddlEditBranchOfStudy1.DataTextField = "branch_of_study"; 
      ddlEditBranchOfStudy1.DataBind(); 
      ddlEditBranchOfStudy1.SelectedValue = dt.Rows[0]["branch_of_study_1_code"].ToString(); 
      DropDownList ddlEditCertificate = (DropDownList)e.Row.FindControl 
       ("ddlEditCertificate"); 
      ddlEditCertificate.DataSource = certificateBLO. 
       SelectCertificateOfEstablishment(educationEstablishmentCode); 
      ddlEditCertificate.DataValueField = "certificate_code"; 
      ddlEditCertificate.DataTextField = "certificate"; 
      ddlEditCertificate.DataBind(); 
      ddlEditCertificate.SelectedValue = dt.Rows[0]["certificate_code"].ToString(); 
      DropDownList ddlEditBranchOfStudy2 = (DropDownList)e.Row. 
       FindControl("ddlEditBranchOfStudy2"); 
      ddlEditBranchOfStudy2.DataSource = branchOfStudyBLO. 
       SelectBranchOfEstablishment(educationEstablishmentCode); 
      ddlEditBranchOfStudy2.DataValueField = "branch_of_study_code"; 
      ddlEditBranchOfStudy2.DataTextField = "branch_of_study"; 
      ddlEditBranchOfStudy2.DataBind(); 
      ddlEditBranchOfStudy2.SelectedValue = dt.Rows[0]["branch_of_study_1_code"].ToString(); 
     } 
    } 

這是我曾嘗試:

protected void ddlEditEducationEstablishment_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //if (e.Row.RowType == DataControlRowType.DataRow && gvEducations.EditIndex == e.Row.RowIndex) 
     //{ 
     // DropDownList ddlEditEducationEstablishment = 
     //  (DropDownList)e.Row.FindControl("ddlEditEducationEstablishment"); 
     // string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue; 
     // DropDownList ddlEditSchool = (DropDownList)e.Row.FindControl("ddlEditSchool"); 
     // ddlEditSchool.DataSource = 
     //  schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode); 
     // ddlEditSchool.DataValueField = "school_code"; 
     // ddlEditSchool.DataTextField = "school"; 
     // ddlEditSchool.DataBind(); 
     // DropDownList ddlEditBranchOfStudy1 = (DropDownList)e.Row.FindControl 
     //  ("ddlEditBranchOfStudy1"); 
     // ddlEditBranchOfStudy1.DataSource = branchOfStudyBLO. 
     //  SelectBranchOfEstablishment(educationEstablishmentCode); 
     // ddlEditBranchOfStudy1.DataValueField = "branch_of_study_code"; 
     // ddlEditBranchOfStudy1.DataTextField = "branch_of_study"; 
     // ddlEditBranchOfStudy1.DataBind(); 
     // DropDownList ddlEditCertificate = (DropDownList)e.Row.FindControl 
     //  ("ddlEditCertificate"); 
     // ddlEditCertificate.DataSource = certificateBLO. 
     //  SelectCertificateOfEstablishment(educationEstablishmentCode); 
     // ddlEditCertificate.DataValueField = "certificate_code"; 
     // ddlEditCertificate.DataTextField = "certificate"; 
     // ddlEditCertificate.DataBind(); 
     // DropDownList ddlEditBranchOfStudy2 = (DropDownList)e.Row. 
     //  FindControl("ddlEditBranchOfStudy2"); 
     // ddlEditBranchOfStudy2.DataSource = branchOfStudyBLO. 
     //  SelectBranchOfEstablishment(educationEstablishmentCode); 
     // ddlEditBranchOfStudy2.DataValueField = "branch_of_study_code"; 
     // ddlEditBranchOfStudy2.DataTextField = "branch_of_study"; 
     // ddlEditBranchOfStudy2.DataBind(); 
     //} 
    } 

我要的是當我在第一個下拉列表中選擇時,其他下拉列表將再次使用fi中的選定值進行數據綁定首先下拉。參考下面的圖片。 Sample

+0

請務必提及到底什麼不起作用。我的猜測是你的情況不起作用。 'e.Row.RowType'將不起作用,因爲'e'的類型是'EventArgs',它不包含行屬性。你需要找到包含你的按鈕的GridViewRow。 –

+0

@ j.f。正如我在問題中所說的,當我在主下拉列表中選擇一個值時,我無法禁止其他下拉菜單。我嘗試將條件更改爲GridViewRowEventArgs,並拋出一個運行時異常,表明下拉列表只知道EventArgs條件。 –

回答

2

就像我在我的評論中所說的,你需要找到你的DropDownList包含在其中的GridViewRow。用那一行,你可以找到你所有的其他DropDownLists。您找到其他每個DropDownLists都處於正確的軌道,但您並未在正確的位置查找它們。

protected void ddlEditEducationEstablishment_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    // Get the master DropDownList and its value 
    DropDownList ddlEditEducationEstablishment = (DropDownList)sender; 
    string educationEstablishmentCode = ddlEditEducationEstablishment.SelectedValue; 

    // Get the GridViewRow in which this master DropDownList exists 
    GridViewRow row = (GridViewRow)ddlEditEducationEstablishment.NamingContainer; 

    // Find all of the other DropDownLists within the same row and bind them 
    DropDownList ddlEditSchool = (DropDownList)row.FindControl("ddlEditSchool"); 
    ddlEditSchool.DataSource = schoolBLO.SelectSchoolOfEstablishment(educationEstablishmentCode); 
    ddlEditSchool.DataValueField = "school_code"; 
    ddlEditSchool.DataTextField = "school"; 
    ddlEditSchool.DataBind(); 

    // etc... 
} 
+0

這對我有用。在(DropDownList)發件人和NamingContainer中使用發件人對我來說是新的。謝謝@ j.f。 –

+0

很高興能幫到你! –