捕获的网格视图内的DropDownList指数涨跌事件 [英] Capture DropDownList Index Change event inside of grid View

查看:82
本文介绍了捕获的网格视图内的DropDownList指数涨跌事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图捕捉一个下拉列表中我已经把GridView控件内的SelectedIndexChanged事件。这回发罚款,但不会进入我的SelectedIndexChanged事件处理程序。这里是我的code

I am trying to capture the SelectedIndexChanged event for a drop down list I have put inside of a gridview control. It posts back fine, but does not go into my SelectedIndexChanged event handler. Here is my code

    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;
    }

任何帮助是极大AP preciated。
谢谢,

Any help is greatly appreciated. Thanks,

编辑基于注释

我曾尝试如你所说......而且我还没有捕获后回来。这里是我的新code

I have tried as you suggested...and am still not capturing the post back. here is my new code

        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()事件处理程序。

it is still not going into my myddl_SelectedIndexChanged() eventhandler.

推荐答案

创建DROPDOWNLIST在网格的RowCreated和ID分配给它。通过 e.Row.FindControl(MyDropdownlistID)获取refrence在这些的RowDataBound Dropdowns的,并将其绑定到数据源。创建而不是引用总是相同的不同DROPDOWNLIST实例

Create that Dropdownlist in RowCreated of the Grid and assign an ID to it. Get the refrence to these Dropdowns in RowDataBound via e.Row.FindControl("MyDropdownlistID") and bound them to the Datasource. Create distinct Dropdownlist instances instead of referencing always the same

这篇关于捕获的网格视图内的DropDownList指数涨跌事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆