Click here to Skip to main content
12,997,531 members (58,928 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a user control with some text box and a button(submit).My problem is that
when I load the aspx page I register the ascx control's button click event on the
aspx page.If click the submit button on the ascx control it is working fine
but if I put the page load events under if(!ispostback),then the button click
event not working.
As the page is already loaded so it is ambiguous to reload the control again
to get the ascx controls click event.

Kindly help me to achive that one.I don't want to load the control again.

My aspx code below

protected void Page_Load(object sender, EventArgs e)
            if (Request.QueryString["mode"] == "createTck")
                UserControl_CreateTicket uccreateTicket = LoadControl("~/UserControl/CreateTicket.ascx") as UserControl_CreateTicket;
                uccreateTicket.evnt_btnSubmitClk += new delgbtnSubmit_Ticket(ucCreateTicket_btnSubmitClk);
            else if (Request.QueryString["mode"] == "TcktHistory")
                UserControl_TicketHistory ucTicketHistoey = LoadControl("~/UserControl/TicketHistory.ascx") as UserControl_TicketHistory;
    #region ButtonClick(ContentPages)
    void ucCreateTicket_btnSubmitClk(object sender, EventArgs e)
        UserControl_CreateTicket ucCrateTicketControl = ((UserControl_CreateTicket)(sender));
Posted 10-May-13 2:17am
Updated 10-May-13 2:34am
ryanb31 10-May-13 8:34am
You should load controls in the OnInit event instead of PageLoad. That may fix your issue.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You have to reload the control on every post back, because its a dynamically added control.
You can avoid the loading if you include the control in the XML markup. Then you could add both controls to the XML file and only display the one you want based on the query string.
<my_control id="createTicket">......</my_control>
<my_control2 id="historyTicket">......

if (Request.QueryString["mode"] == "createTck")
    createTicket.Visible = true;
    historyTicket.Visible = false;        
else if (Request.QueryString["mode"] == "TcktHistory")
    createTicket.Visible = false;
    historyTicket.Visible = true;       

This link will explain what happens to the page when its submitted.[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 10 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100