捕获网格视图内的DropDownList索引更改事件

时间:2011-03-16 21:33:18

标签: asp.net gridview

我正在尝试捕获SelectedIndexChanged事件以获取我放在gridview控件中的下拉列表。它回发正常,但不会进入我的SelectedIndexChanged事件处理程序。这是我的代码

    DropDownList myddl;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.myGridview.RowDataBound += new GridViewRowEventHandler(myGridview_RowDataBound);
        myddl = new DropDownList();
        myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
        if (!Page.IsPostBack)
        {
            List<Team> teams = giveMeTeams();
            this.myGridview.DataSource = teams;
            this.myGridview.AutoGenerateColumns = false;

            BoundField col1 = new BoundField();
            col1.DataField = "Name";
            this.myGridview.Columns.Add(col1);

            BoundField col2 = new BoundField();
            col2.DataField = "Sport";
            this.myGridview.Columns.Add(col2);

            BoundField col3 = new BoundField();
            col3.DataField = "Status";
            this.myGridview.Columns.Add(col3);

            this.myGridview.DataBind();
        }
    }

    void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        myddl = new DropDownList();
        myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
        List<string> items = new List<string>();
        items.Add("good");
        items.Add("bad");
        myddl.DataSource = items;
        myddl.AutoPostBack = true;
        myddl.DataBind();
        e.Row.Cells[2].Controls.Add(myddl);
    }

    void myddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        string temp = "In Here";  //neve hits this code
    }

    private List<Team> giveMeTeams()
    {
        Teams teams = new Teams();
        teams.Add(new Team("RedWings", "Hockey", "good"));
        teams.Add(new Team("Lions", "Football", "bad"));
        teams.Add(new Team("Packers", "Football", "good"));
        return teams;
    }

非常感谢任何帮助。 谢谢,

根据评论编辑

我按照你的建议尝试过...而且我仍然没有抓回帖子。这是我的新代码

        void myGridview_RowCreated(object sender, GridViewRowEventArgs e)
    {
        DropDownList myddl = new DropDownList();
        myddl = new DropDownList();
        myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
        myddl.ID = "MyID" + e.Row.RowIndex.ToString();
        e.Row.Cells[2].Controls.Add(myddl);
    }

    void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DropDownList myddl = e.Row.FindControl("MyID" + e.Row.RowIndex.ToString()) as DropDownList;
        //myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
        List<string> items = new List<string>();
        items.Add("good");
        items.Add("bad");
        myddl.DataSource = items;
        myddl.DataMember = "Status";
        myddl.AutoPostBack = true;
        myddl.DataBind();
        e.Row.Cells[2].Controls.Add(myddl);
    }

它仍然没有进入我的myddl_SelectedIndexChanged()事件处理程序。

1 个答案:

答案 0 :(得分:2)

在Grid的RowCreated中创建Dropdownlist并为其分配ID。通过e.Row.FindControl("MyDropdownlistID")获取RowDataBound中的这些Dropdown,并将它们绑定到Datasource。创建不同的Dropdownlist实例,而不是始终引用相同的