当选定的索引在同一Gridview中的下拉列表中更改时,在Gridview中绑定下拉列表

时间:2015-06-02 08:28:40

标签: c# asp.net gridview drop-down-menu

我有一个gridview,其中的模板字段包含下拉列表和文本框。 1下拉列表将是一个所谓的“主”下拉列表,当所选索引发生变化时,同一行中的其他下拉列表将使用所选主下拉列表的数据进行数据记录。

我的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();
        //}
    }

我想要的是,当我在第一个下拉列表中选择时,其他下拉列表将使用第一个下拉列表中的选定值再次进行数据绑定。请参考下图。 Sample

1 个答案:

答案 0 :(得分:3)

就像我在评论中所说,你需要找到你的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...
}