0

我有一个网页,其中包含在运行时使用 GridView 中的模板字段创建的多个下拉列表。我已在selectedindexchange事件上将事件处理程序分配给 DDL 。但是这个事件处理函数只被调用一次,而我的 gridview 只被更新一次。

如何在每次选择索引更改后调用我的事件处理函数。

这是我的代码

foreach (DataColumn coloumn in dt.Columns)
        {

            if (!coloumn.ColumnName.Equals("empName"))
            {


                var linkF = new TemplateField();
                linkF.HeaderText = coloumn.ColumnName;
                linkF.HeaderTemplate = new LinkColumn(ListItemType.Header, coloumn.ColumnName,folder);
                linkF.ItemTemplate = new LinkColumn(ListItemType.Item, coloumn.ColumnName,folder);


                GridView1.Columns.Insert(y, linkF);
                y++;   
            }
            else if (coloumn.ColumnName.Equals("empName"))
            {
                //Response.Write("Came");
                BoundField bfield = new BoundField();

                ////Initalize the DataField value.
                bfield.DataField = coloumn.ColumnName;

                ////Initialize the HeaderText field value.
                bfield.HeaderText = coloumn.ColumnName;
                GridView1.Columns.Insert(y, bfield);
                y++;



            }


        }

        GridView1.DataSource = dt;
        GridView1.DataBind();


class LinkColumn : DetailView, ITemplate
{
    int id;
    ListItemType _item;
    String colN = null;
    String fold;
    public LinkColumn(ListItemType item, String colNa,String f)
    {
        _item = item;
        colN = colNa;
        fold = f;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {

        switch (_item)
        {
            case ListItemType.Header:
                DetailView.dp = new DropDownList();
                Label lb = new Label();
                MySqlCommand cm = new MySqlCommand("select distinct " + colN + " from " + fold + "",conn);

                MySqlDataAdapter ad = new MySqlDataAdapter();
                DataTable d = new DataTable();
                ad.SelectCommand = cm;
                ad.Fill(d);
                DetailView.dp.DataTextField = colN;
                DetailView.dp.DataValueField = colN;
                DetailView.dp.DataSource = d;
                DetailView.dp.DataBind();
                lb.Text = colN.ToUpperInvariant();
                dp.AutoPostBack = true;
                container.Controls.Add(lb);
                container.Controls.Add(DetailView.dp);
                DetailView.dp.SelectedIndexChanged += new EventHandler(dp_Selected);


                break;
            case ListItemType.Item:
                TextBox tb1 = new TextBox();
                tb1.Enabled = false;
                tb1.DataBinding += new EventHandler(tb1_Data);
                tb1.Columns = 30;
                container.Controls.Add(tb1);
                break;
        }

    }

    void tb1_Data(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        GridViewRow cont = (GridViewRow)txt.NamingContainer;
        object dataV = DataBinder.Eval(cont.DataItem, colN);
        if (dataV != DBNull.Value)
        {
            txt.Text = dataV.ToString();
        }
    }

    void dp_Selected(object sender, EventArgs e)
    {


        DropDownList list = (DropDownList)sender;
        String name = list.SelectedValue;
        Session["cols"] = name;
        DetailView.colName = Session["cols"].ToString();
        DetailView.flag = true;


    }

Mu 事件处理函数 dp_selected 仅调用一次。我在 Page_Load() 中调用了我的 GridBind() 函数。

任何帮助将不胜感激。提前致谢

4

0 回答 0