DataGrid1里面有二个模板列,放二个DropDownList,分别是DropDownList1和DropDownList2
我想选择DropDownList1中的一个值,同时能在DropDownList2中出现DropDownList1对应的值

解决方案 »

  1.   

    DropDownList ddl1=(DropDownList)e.Items.FindControl("ddl1");
    DropDownList ddl2=(DropDownList)e.Items.FindControl("ddl2");
    写ddl1的SelectIndexChange事件让ddl2.SelectIndex等于ddl1.SelectIndex
      

  2.   

    在ItemdataBound事件里面写上:
    (DropDownList)e.Item.FindControl(DropDownList2).SelectValue = (DropDownList)e.Item.FindControl(DropDownList1).SelectValue;具体的你调试一下
      

  3.   

    DropDownList模板列没有事件可以触发,
      

  4.   

    if(e.Item.ItemType==System.Web.UI.WebControls.ListItemType.EditItem)
    { System.Data.SqlClient.SqlDataAdapter mycmd =new System.Data.SqlClient.SqlDataAdapter("select ssx from am_ssx_tab order by ssx",myconn);
    DataSet ds =new DataSet();
    mycmd.Fill(ds,"ssx");
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataTextField="ssx";
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataValueField="ssx";
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataSource=ds.Tables["ssx"];
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataBind();
    //这是第一个下拉框
    System.Data.SqlClient.SqlDataAdapter mycmd1 =new System.Data.SqlClient.SqlDataAdapter("select xian from am_ssx_tab where ssx='"+((DropDownList)e.Item.FindControl("temp_ssx")).SelectedItem.Text+"' order by ssx",myconn);
    DataSet ds1 =new DataSet();
    mycmd1.Fill(ds1,"xian");
    ((DropDownList)e.Item.FindControl("temp_xian")).DataTextField="xian";
    ((DropDownList)e.Item.FindControl("temp_xian")).DataValueField="xian";
    ((DropDownList)e.Item.FindControl("temp_xian")).DataSource=ds1.Tables["xian"];
    ((DropDownList)e.Item.FindControl("temp_xian")).DataBind();

    }
    //这是第二个下拉框,内容是当选择第一个框中内容时,第二个中的内容要跟着变化,
    我这样写只是页面起来时可以出来,也只是第一个框的对应的内容,当选择第一个框中内容时,第二个不会变了。
      

  5.   

    动态给DropDownList1绑定事件
    DropDownList DropDownList1=(DropDownList)e.Items.FindControl("ddl1");
    DropDownList1.AutoPostBack = true;
    DropDownList1.SelectedIndexChanged += new System.EventHandler(SelectedIndexChanged);
    SelectedIndexChanged为根据DropDownList1选项绑定DropDownList2值的函数;类似于
    private void SelectedIndexChanged(object sender, System.EventArgs e)
    {
       ...
    }
      

  6.   

    erwanfan(teddyxiong) :你好,我按你的意思操作了,可还是不行,当改变temp_ssx中内容时,不出运行那个selechange(),是不是temp_ssx和DropDownList1是两个不同的对象?
    ==================
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    { if(e.Item.ItemType==System.Web.UI.WebControls.ListItemType.EditItem)
    { System.Data.SqlClient.SqlDataAdapter mycmd =new System.Data.SqlClient.SqlDataAdapter("select ssx from am_ssx_tab order by ssx",myconn);
    DataSet ds =new DataSet();
    mycmd.Fill(ds,"ssx");
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataTextField="ssx";
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataValueField="ssx";
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataSource=ds.Tables["ssx"];
    ((DropDownList)e.Item.FindControl("temp_ssx")).DataBind();
    DropDownList DropDownList1=(DropDownList)e.Item.FindControl("temp_ssx");
    DropDownList1.AutoPostBack = true; DropDownList1.SelectedIndexChanged += new System.EventHandler(selechanged); }
    }
    private void selechanged(object sender, System.EventArgs e)
    {
    Response.Write("<script language='javascript'>alert('aaa!')</script>"); }
      

  7.   

    DropDownList2绑定DropDownList1为数据源就可以了!
      

  8.   

    绑定DropDownList1的SelectedIndexChanged以后,改变选项时激发了postback了么?如果激发了,则有可能你的DataGrid的数据邦定放在 Not IsPostBack 判定块之外,导致每次点击执行Page_Load的时候,重新邦定了DataGrid,这样会丢的checkbox的选中信息。
    如果没有激发,你调试一下,看看是否绑定了DropDownList1的动作。
      

  9.   

    erwanfan(teddyxiong) :
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack) 
     {
     }
    }
    DropDownList1的postbace激发了,我在页面开始时用的上面的代码,是否可以?
      

  10.   

    不难!   你还可以用 javascrpit 来实现
      

  11.   

    javascrpit 不清楚怎么弄,
    能不能给出些具体实现的代码么,
    我都整了两天了,还没结果。
      

  12.   

    选择DropDownList1的下拉内容,就是不能触发他绑定的事件,
      

  13.   

    我测试了,这样子写肯定是可以触发selechanged的,你试试
    <asp:TemplateColumn HeaderText="dd1">
    <ItemTemplate>
    <asp:DropDownList ID="dd1" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="selechanged">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>