(WEB开发)在DataGrid里有两个模板列,每个列里包含一个DropDownList ;假设为:dl1,dl2 ;我想对这两个实现联动。也就是,在dl1触发SelectedIndexChanged事件时,dl2显示的东西要跟着变。我代码如下:
private void dgexcel_ItemDataBound(object sender,   System.Web.UI.WebControls.DataGridItemEventArgs e)
{
     Funct ft=new Funct();
     DataTable dt=null;
     dge=e;
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
     {
DropDownList dlr=((DropDownList)e.Item.FindControl("dlrclass"));
DropDownList dls=((DropDownList)e.Item.FindControl("dlsclass"));
dt=ft.getrclass();
dlr.DataSource=dt;
dlr.DataTextField="fname";
dlr.DataValueField="fid";
dlr.DataBind();
dt.Clear();
dlr.SelectedIndexChanged+=new EventHandler(dlr_SelectedIndexChanged);
dt=ft.getsclass(dlr.SelectedValue.Trim());
dls.DataSource=dt;
dls.DataTextField="fname";
dls.DataValueField="fid";
dls.DataBind();
dt.Clear();绑定事件如下:private void dlr_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dls=((DropDownList)dge.Item.FindControl("dlsclass"));
string fid=((DropDownList)sender).SelectedValue;
Funct ft=new Funct();
DataTable dt=ft.getsclass(fid);
dls.DataSource=dt;
dls.DataTextField="fname";
dls.DataValueField="fid";
dls.DataBind();
}现在有两个问题:一是,事件好象触发不了;二是在dl1触发后怎么去确定跟他同在一行的dl2。

解决方案 »

  1.   

    //****前台
     <asp:DataGrid ID="DGRid" runat="server" AutoGenerateColumns="False" OnItemDataBound="DGRid_ItemDataBound">
                <Columns>
                    <asp:TemplateColumn HeaderText="编号">
                        <ItemStyle Width="40" />
                        <ItemTemplate>
                            <%=RowIndex()%>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="Name" HeaderText="文件名"></asp:BoundColumn>
                    <asp:BoundColumn DataField="CreateDateTime" HeaderText="创建时间"></asp:BoundColumn>
                    <asp:BoundColumn DataField="LastAccessTime" HeaderText="最后写入时间"></asp:BoundColumn>
                    <asp:BoundColumn DataField="FullName" HeaderText="文件路径"></asp:BoundColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                          <asp:DropDownList AutoPostBack="true"  ID="drpState" runat="server" OnSelectedIndexChanged="drpState_SelectedIndexChanged">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:DropDownList ID="drpStateTwo" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="选择">
                        <ItemStyle Width="200" />
                        <ItemTemplate>
                            <%#BuliteControl(DataBinder.Eval(Container.DataItem,"Name").ToString())%>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>你把后台代码改一下就可以了
     protected void drpState_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList drpList = null;
            DropDownList drpStateTwo = null;
            foreach (DataGridItem DGItem in DGRid.Items)
            {
                drpList = (DropDownList)DGItem.FindControl("drpState");
                drpStateTwo = (DropDownList)DGItem.FindControl("drpStateTwo");            drpStateTwo.Items.Clear();            if (drpList.SelectedIndex == 0)
                {
                    drpStateTwo.Items.Add("ddd");
                    drpStateTwo.Items.Add("ccc");
                }
                else if (drpList.SelectedIndex == 1)
                {
                    
                    drpStateTwo.Items.Add("ggg");
                }
                else
                {
                    drpStateTwo.Items.Add("oooo");
                }
            }    }
      

  2.   

    dl1的autopostback设置为true了没有?
      

  3.   

    zhangxiaopin(zxp) 的方法可以触发事件,但怎么取到触发事件DropDownList在DataGrid中的行呢。我不能够每一行都跟着变。
      

  4.   

    this function need use js or ajax to achieve.because dl2 may change according to change dl1 selection.and at the same time other selection of dl1 and dl2 cann't be changed.then you shouldn't refresh page.
      

  5.   

    不知道这样可不可以,
    结合你已有的代码和zxp的代码
    protected void drpState_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList drpList = =((DropDownList)sender)
            DropDownList drpStateTwo = null;
            foreach (DataGridItem DGItem in DGRid.Items)
            {
                drpStateTwo = (DropDownList)DGItem.FindControl("drpState");
               if(drpStateTwo == drpList)
    {
       1得到另一个dropdownlist
       2得到这个dropdownlist的值
       3设置
    }
             }    }