tags:

views:

104

answers:

2

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 .

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

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

}

A: 

Hi there,

you can declare you selectedindexchanged event like this:

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.

MUG4N
ddl.AutoPostBack = true; ddl.SelectedIndexChanged += new EventHandler(this.ddl_SelIndexChangedi have also done the same it didnt work
Arunachalam
Did you check if the code gets called? Or maybe it gets lost with a postback did you check that as well?
MUG4N
You have to add the event on each postback
MUG4N
where should the call be made.the control is not coming to the call if selectindex event
Arunachalam
The registering of the event should be in page.load or similar events. You need to register it on every postback. You can then make the event call afterwards in for example a button_click event.
MUG4N
can u give a example or how to register an event plz ?
Arunachalam
You did it already:
MUG4N
ddlFilter.SelectedIndexChanged += new EventHandler(ddl2_SelectedIndexChanged);
MUG4N
A: 

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):

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.

Patrick