[英]Bind a Dropdown in Gridview When Selected Index Changes on Dropdown in the same Gridview
我有一个包含由下拉列表和文本框组成的模板字段的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();
//}
}
我想要的是,当我在第一个下拉列表中进行选择时,将使用第一个下拉列表中的选定值再次对其他下拉列表进行数据绑定。 请参考下图。
就像我在评论中所说,您需要找到您的DropDownList包含在其中的GridViewRow。 在该行中,您可以找到所有其他DropDownLists。 通过找到其他所有DropDownList,您将走上正确的道路,但您找不到的位置正确。
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...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.