动态创建的下拉列表的OnSelectedIndexChanged事件在将控制权附加到stringbuilder时不会被触发

时间:2015-03-11 08:28:03

标签: c# asp.net

动态创建的下拉列表OnSelectedIndexChanged事件未被触发。使用stringbuilder创建和查看DropDown控件。使用HtmlTextWriter为stringbuilder添加了控件。任何人都可以帮我解决如何将一个onselectedindexchanged事件发送到stringbuilder?我已经复制了我已经完成的代码..

    int k = 1;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (Convert.ToString(dr["SubCatPropCtrl"]) == "3")
                    {
                        DropDownList drp = new DropDownList();
                        drp.ID = "drp_" + k.ToString();
                        drp.AutoPostBack = true;
                        drp.Width = new Unit("210px");
                        drp.EnableViewState = true;
                        drp.CssClass = "drplist";


                        drp.Items.Add(new ListItem("--All--", "0"));
                        //drp.Items.Add(new ListItem(Convert.ToString(ds1.Tables[0].Rows[0]))
                        ds1 = cn.with2parameter("fill_dynmicControls", subcatcode, Convert.ToString(dr["SubCatPropCode"]));
                        foreach (DataRow drs in ds1.Tables[0].Rows)
                        {
                            drp.Items.Add(new ListItem(Convert.ToString(drs["SubCatPropVal"]), Convert.ToString(drs["id"])));
                        }
                        buildpanel.Append("<div style=font-size:20px;color:#000;width:98%;float:none;position:relative;top:10px;font-family:ArialNarrow;margin-left:5%;>" + dr["SubCatPropName"].ToString() + "</div><div style=font-size:14px;font-weight:bold;color:#000;width:94%;float:none;position:relative;font-family:ArialNarrow;top:5px;margin-left:15px;margin-top:5px;>");
                        drp.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
                        using (StringWriter sw = new StringWriter(buildpanel))
                        {
                            using (HtmlTextWriter tx = new HtmlTextWriter(sw))
                            {
                                drp.RenderControl(tx);
                            }
                        }
                        buildpanel.Append("</div>");
                        k++;
                    }

                }
  void drp_SelectedIndexChanged(object sender, EventArgs e)
    {
        \\code
    }

1 个答案:

答案 0 :(得分:0)

您需要将事件附加到已创建的控件。

DropDownList drp = new DropDownList();
drp.ID = "drp_" + k.ToString();
drp.AutoPostBack = true;
drp.Width = new Unit("210px");
drp.EnableViewState = true;
drp.CssClass = "drplist";
drp.SelectedIndexChanged += 
        new System.EventHandler(drp_SelectedIndexChanged);

祝你好运。