具体写在哪里呢?
我是ASPX页面和aspx.cs的,页面和代码分离的,在后台编码文件*.aspx.cs中访问不了ropDownList1对象,在*.ASPX中我又不知道怎么输入这类代码

解决方案 »

  1.   

    可以在DataGrid的EditCommand中进行绑定,具体可以这样操作:
    1、对DataGrid绑定;
    2、用FindControl方法找到那个DropDownList;
    3、用DropDownList的ItemCollection的FindByValue或FindByText方法找到需要选定的项,然后把它的Selected属性设为true;
    示例:
    void mydatagrid_OnEditCommand(object src,DataGridCommandEventArgs e){
    mydatagrid.EditItemIndex=e.Item.ItemIndex;
    BindData();//这是对DataGrid进行数据绑定的一个方法,自己写
    DropDownList ddl=(DropDownList)mydatagrid.Items[mydatagrid.EditItemIndex].FindControl("mydropdownList");
    if(ddl!=null)
    ddl.Items.FindByValue("我的选项").Selected=true;
    }
    没用DataGrid时间长了,以前的代码也不在身边,不知道上面临时写的代码能不能运行,但大致操作步骤已经说清楚了,自己慢慢看看。
      

  2.   

    EditItemTemplate>
    <FONT face="宋体">
    <asp:DropDownList id=Dropdownlist1 runat="server" Width="96px" DataSource="<%# ds %>" DataTextField="city" DataValueField="city" DataMember="cities" SelectedIndex='<%# city_index(databinder.eval(container.dataitem,"city")) %>'>
    </asp:DropDownList></FONT>
    </EditItemTemplate>代码:
    Function city_index(ByVal city As String) As Integer
            Dim cn As SqlConnection = New SqlConnection(connection1.connstr)
            Dim cmd As SqlCommand = New SqlCommand("select distinct city from employees", cn)
            Dim cityhash As New Hashtable()
            Dim dr As SqlDataReader, i As Integer = 0
            cn.Open()
            dr = cmd.ExecuteReader()        While dr.Read
                cityhash(dr("city")) = i
                i = i + 1
            End While
            cn.Close()
            Return cityhash(city)
        End Function另外把ds设成页面变量
    private ds as new dataset()再写代码填充
      

  3.   

    //value是DropDownList的value值DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(value));将以上放在datagrid的itemdatabind事件里
      

  4.   

    大家都说了:DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(value));呵呵~~:)不过别忘了在ItemCreat事件里写上你填充DropDownList的代码,否则你会看到一片空白的。:)
      

  5.   

    上面的回复都看过了,也试过了,问题还没解决。你们的代码都好理解,
    可是我搞不清的是,绑定DataGrid1中的下拉列表的选项和默认值时,该放在哪个对象的哪个事件里??
    还有,该事件触发的事件代码是放在mypage.ASPX页面中还是ASPX页面的后台编码文件mypage.ASPX.cs中呢?
      

  6.   

    放在DataGrid的ItemDataBound事件里面
      

  7.   

    还是不行我都试了在ItemDataBound和ItemCreat事件中写下面的代码
    没有一次成功过。
    ................
    DropDownList ddl=(DropDownList)mydatagrid.Items[mydatagrid.EditItemIndex].FindControl("mydropdownList");
    if(ddl!=null)
    {
        ddl.Items.FindByValue("我的选项").Selected=true;
        //***********结果永远也不能执行上面这一句***********
    }
    .............................
    哪位兄弟能给个完整的页面看看吗?我明天要交软件了,头痛啊!