Net中实现Gridview中两个Dropdownlist的级联

首先要在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("", ""));
           
        }

这应该就能看懂了吧,如果还不行,请联系博主

转载于:https://www.cnblogs.com/objectxhy/p/5911902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值