如何在c#中创建动态下拉列表的事件处理程序

时间:2021-08-24 06:30:03

I have created a dynamic grid view using Itemplate .now i have also created a dynamic drop down list in the grid . how to create a event handler for on selectedindexchange .

我使用Itemplate创建了一个动态网格视图,现在我还在网格中创建了一个动态下拉列表。如何为selectedindexchange创建事件处理程序。

i created a slectedindexchange event but it didnt work .the control never passes to the event ?

我创建了一个slectedindexchange事件,但它不工作。

what to do create a event handler

如何创建事件处理程序

public class DynamicGridViewTextTemplate : ITemplate
{
    string _ColName;
    DataControlRowType _rowType;
    int _Count;
    details Details1 = new details();

    public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
    {
        _ColName = ColName;
        _rowType = RowType;
    }

    public DynamicGridViewTextTemplate(DataControlRowType RowType, int ArticleCount)
    {
        _rowType = RowType;
        _Count = ArticleCount;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        switch (_rowType)
        {
            case DataControlRowType.Header:
                Literal lc = new Literal();
                lc.Text = "<b>" + _ColName + "</b>";

                DropDownList ddl = new DropDownList();

                ddl.AutoPostBack = true;
                ddl.SelectedIndexChanged += new EventHandler(this.ddl_SelIndexChanged);

                container.Controls.Add(lc);
                container.Controls.Add(ddl);

                break;

            case DataControlRowType.DataRow:               

                 //Label lbl = new Label();

                 //lbl.DataBinding += new EventHandler(this.lbl_DataBind);
                 LinkButton lb = new LinkButton();
                 lb.DataBinding += new EventHandler(this.lbl_DataBind);
                 lb.OnClientClick +=new EventHandler(this.lb_Click);

                 //lbl.Controls.Add(lb);
                 container.Controls.Add(lb);               

                break;

            case DataControlRowType.Footer:
                Literal flc = new Literal();
                flc.Text = "<b>Total No of Articles:" + _Count + "</b>";
                container.Controls.Add(flc);
                break;

            default:

                break;
        }
    }

    private void lb_Click(Object sender, EventArgs e)
    {
        details1.lbl_Click(sender, e);
    }

    private void lbl_DataBind(Object sender, EventArgs e)
    {
        //Label lbl  = (Label)sender;
        LinkButton lbl = (LinkButton)sender;

        GridViewRow row = (GridViewRow)lbl.NamingContainer;

        lbl.Text =DataBinder.Eval(row.DataItem, _ColName).ToString();
    }

    public void ddl_SelIndexChanged(Object sender, EventArgs e)
    {
        Details1.ddlFilter_SelectedIndexChanged(sender,e);
    }
}

4 个解决方案

#1


3  

you can declare you selectedindexchanged event like this:

您可以声明您选择的tedindexchanged事件如下:

ddlFilter.SelectedIndexChanged += new EventHandler(ddl2_SelectedIndexChanged);
ddlFilter.AutoPostBack = true;

void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
{
    //your code 
}

The reason your event wasn't called is the AutoPostBack=true field. If you don't set it to true your selectedIndexChanged event will never be called.

您的事件不被调用的原因是AutoPostBack=true字段。如果您不将其设置为true,则永远不会调用selectedIndexChanged事件。

#2


0  

Whenever I create a new Control in an ASP web page I follow this boiler plate (note that I added some example controls so it's not a "clean" boiler plate):

每当我在一个ASP web页面中创建一个新的控件时,我都会遵循这个锅炉板(注意,我添加了一些示例控件,所以它不是一个“干净”的锅炉板):

namespace Components {
    [ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
    public class MyControl : WebControl, INamingContainer {

        // todo: add controls that are created dynamically
        private GridView gridView;

        public MyControl () {
            Initialize();
        }

        [Browsable(false)]
        public override ControlCollection Controls {
            get { EnsureChildControls(); return base.Controls; }
        }

        protected override void OnLoad(EventArgs e) {
            // todo: attach event listeners for instance
            base.OnLoad(e);
        }

        protected override void CreateChildControls() {
            Initialize();
        }

        protected override void Render(HtmlTextWriter writer) {
             if (DesignMode) {
                 // If special design mode rendering
                 return;
             }
             base.Render(writer);
        }

        /// This is where the controls are created
        private void Initialize() {
            base.Controls.Clear();
            // todo: Create all controls to add, even those "added later"
            // if something is generated but should not be shown,
            // set its Visible to false until its state is changed
            Label exampleLabel = new Label();
            exampleLabel.Visible = false; // like so
            if (gridView == null) { gridView = new GridView(); }
            base.Controls.Add(exampleLabel);
            base.Controls.Add(gridView);
        }
    }
}

Now, if you create your dynamic drop down in Initialize and add it to your Controls collection every time but only set its Visibility to true when you want it to show, your event should be triggered, since the id's of your controls should be the same between postbacks.

现在,如果您在每次初始化时创建您的动态下拉列表并将其添加到您的控件集合中,但只在您希望它显示时将其可见性设置为true,那么您的事件应该被触发,因为您的控件的id应该在回发之间是相同的。

#3


0  

Dynamic control's event to occure, it is required that it should be created and event assigned in page_load event or during the page_load event occures. Control's event will fire after Page_Load event completes. If control is not recreated in page_load event, event will not bind to the control and will not fire.

动态控件的事件到occure时,需要在page_load事件或page_load事件发生期间创建和分配事件。控件的事件将在Page_Load事件完成后触发。如果在page_load事件中没有重新创建控件,则事件将不会绑定到控件,也不会触发。

#4


0  

I had the same problem and I was creating the dynamic ddl inside (!Page.IsPostBack). When i moved the creation outside the (!Page.IsPostBack) it worked fine.

我遇到了同样的问题,我在(!Page.IsPostBack)中创建了动态ddl。当我将创作移到(!Page.IsPostBack)之外时,效果很好。

You must create your elements outside the (!Page.IsPostBack) like MUG4N said and it should work fine.

您必须在(!Page.IsPostBack)之外创建您的元素,就像MUG4N说的那样,它应该可以正常工作。

#1


3  

you can declare you selectedindexchanged event like this:

您可以声明您选择的tedindexchanged事件如下:

ddlFilter.SelectedIndexChanged += new EventHandler(ddl2_SelectedIndexChanged);
ddlFilter.AutoPostBack = true;

void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
{
    //your code 
}

The reason your event wasn't called is the AutoPostBack=true field. If you don't set it to true your selectedIndexChanged event will never be called.

您的事件不被调用的原因是AutoPostBack=true字段。如果您不将其设置为true,则永远不会调用selectedIndexChanged事件。

#2


0  

Whenever I create a new Control in an ASP web page I follow this boiler plate (note that I added some example controls so it's not a "clean" boiler plate):

每当我在一个ASP web页面中创建一个新的控件时,我都会遵循这个锅炉板(注意,我添加了一些示例控件,所以它不是一个“干净”的锅炉板):

namespace Components {
    [ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
    public class MyControl : WebControl, INamingContainer {

        // todo: add controls that are created dynamically
        private GridView gridView;

        public MyControl () {
            Initialize();
        }

        [Browsable(false)]
        public override ControlCollection Controls {
            get { EnsureChildControls(); return base.Controls; }
        }

        protected override void OnLoad(EventArgs e) {
            // todo: attach event listeners for instance
            base.OnLoad(e);
        }

        protected override void CreateChildControls() {
            Initialize();
        }

        protected override void Render(HtmlTextWriter writer) {
             if (DesignMode) {
                 // If special design mode rendering
                 return;
             }
             base.Render(writer);
        }

        /// This is where the controls are created
        private void Initialize() {
            base.Controls.Clear();
            // todo: Create all controls to add, even those "added later"
            // if something is generated but should not be shown,
            // set its Visible to false until its state is changed
            Label exampleLabel = new Label();
            exampleLabel.Visible = false; // like so
            if (gridView == null) { gridView = new GridView(); }
            base.Controls.Add(exampleLabel);
            base.Controls.Add(gridView);
        }
    }
}

Now, if you create your dynamic drop down in Initialize and add it to your Controls collection every time but only set its Visibility to true when you want it to show, your event should be triggered, since the id's of your controls should be the same between postbacks.

现在,如果您在每次初始化时创建您的动态下拉列表并将其添加到您的控件集合中,但只在您希望它显示时将其可见性设置为true,那么您的事件应该被触发,因为您的控件的id应该在回发之间是相同的。

#3


0  

Dynamic control's event to occure, it is required that it should be created and event assigned in page_load event or during the page_load event occures. Control's event will fire after Page_Load event completes. If control is not recreated in page_load event, event will not bind to the control and will not fire.

动态控件的事件到occure时,需要在page_load事件或page_load事件发生期间创建和分配事件。控件的事件将在Page_Load事件完成后触发。如果在page_load事件中没有重新创建控件,则事件将不会绑定到控件,也不会触发。

#4


0  

I had the same problem and I was creating the dynamic ddl inside (!Page.IsPostBack). When i moved the creation outside the (!Page.IsPostBack) it worked fine.

我遇到了同样的问题,我在(!Page.IsPostBack)中创建了动态ddl。当我将创作移到(!Page.IsPostBack)之外时,效果很好。

You must create your elements outside the (!Page.IsPostBack) like MUG4N said and it should work fine.

您必须在(!Page.IsPostBack)之外创建您的元素,就像MUG4N说的那样,它应该可以正常工作。