首先要在aspx页面中的gridview中绑定两个模板列:
<asp:TemplateField HeaderText="品牌">
<ItemTemplate>
<asp:DropDownList ID="ddlPinPai" runat="server" CssClass="txtInput" AutoPostBack="true" OnSelectedIndexChanged="ddlPinPai_SelectedIndexChanged"
Width="95%" >
</asp:DropDownList>
<asp:HiddenField ID="txtPinPai" runat="server" Value='<%# Eval("PinPai")%>' />
</ItemTemplate>
<ItemStyle Width="10%" />
<HeaderStyle Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="车辆型号">
<ItemTemplate>
<asp:DropDownList ID="ddlCarType" runat="server" Width="100%"
CssClass="txtInput">
</asp:DropDownList>
<asp:HiddenField ID="txtCarType" runat="server" Value='<%# Eval("CarType")%>' />
</ItemTemplate>
<ItemStyle Width="15%" />
<HeaderStyle Width="15%" />
</asp:TemplateField>
-------------------------------------------------------
让后在这个gridview中的OnRowDataBound事件中进行数据源初始绑定:
#region
DropDownList ddlPinPai = e.Row.FindControl("ddlPinPai") as DropDownList;
DropDownList ddlCarType = e.Row.FindControl("ddlCarType") as DropDownList;
//绑定品牌
DataTable dt = new SqlSupplier().GetPinPai();
ddlPinPai.DataTextField = "PinPaiName";
ddlPinPai.DataValueField = "PkId";
ddlPinPai.DataSource = dt;
ddlPinPai.DataBind();
ddlPinPai.Items.Insert(0, new ListItem("", ""));
ddlPinPai.SelectedValue = ((HiddenField)e.Row.FindControl("txtPinPai")).Value;
////绑定车型号
DataTable dtCarType = sql.GetCarTypeBind();
ddlCarType.DataTextField = "CarTypeName";
ddlCarType.DataValueField = "PinPaiId";
ddlCarType.DataSource = dtCarType;
ddlCarType.DataBind();
ddlCarType.Items.Insert(0, new ListItem("", ""));
#endregion
*----------------------------------------------------------------------------
protected void ddlPinPai_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlPinPai = (DropDownList)sender;
System.Web.UI.WebControls.GridViewRow dvr = (System.Web.UI.WebControls.GridViewRow)ddlPinPai.NamingContainer;
DropDownList ddlCarType = (DropDownList)dvr.FindControl("ddlCarType");
DataTable dtCarType = sql.GetCarTypeBind(ddlPinPai.SelectedValue.ToString());
ddlCarType.DataTextField = "CarTypeName";
ddlCarType.DataValueField = "PinPaiId";
ddlCarType.DataSource = dtCarType;
ddlCarType.DataBind();
ddlCarType.Items.Insert(0, new ListItem("", ""));
}
这应该就能看懂了吧,如果还不行,请联系博主