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

C# 当选定索引在同一Gridview中的下拉列表中更改时,在Gridview中绑定下拉列表,c#,asp.net,gridview,drop-down-menu,C#,Asp.net,Gridview,Drop Down Menu,我有一个gridview,模板字段由下拉列表和文本框组成。1下拉菜单将是一个所谓的“主”下拉菜单,当选定索引更改时,同一行中的其他下拉菜单将使用主下拉菜单的选定值进行数据索引 我有我的ASPX的代码 <asp:GridView ID="gvEducations" runat="server" AutoGenerateColumns="false" DataKeyNames="file2_id" onrowcancelingedit="gvEducations_RowCan

我有一个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>
这就是我尝试过的:

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();
        //}
    }
我想要的是,当我在第一个下拉列表中选择时,其他下拉列表将使用第一个下拉列表中选择的值再次进行数据索引。请参阅下图。

正如我在评论中所说,您需要找到DropDownList所在的GridViewRow。通过该行,您可以找到所有其他下拉列表。通过查找其他每个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...
}

一定要提到什么是不起作用的。我猜你的情况不太好
e.Row.RowType
将不起作用,因为
e
的类型为
EventArgs
,不包含Row属性。您需要找到按钮所在的GridViewRow。@j.f.如我在问题中所述,当我在主下拉列表中选择一个值时,我无法对其他下拉列表进行数据绑定。我尝试将条件更改为GridViewRowEventArgs,但它抛出了一个运行时异常,表示下拉列表只知道EventArgs条件。这对我很有效。在(DropDownList)sender和NamingContainer中使用sender对我来说是新的。谢谢@j.f。
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...
}