简体   繁体   中英

Call user control button click event when loaded dynamically

I have a web application that assigns time duration to students in a day kind of a scheduler.

so for this i have a user control as TimePeriod and this control is loaded dynamically on the web page.

My User-control contain two textbox and one button control and i need to find click event of that button.At this button it should retrieve value of two related text-boxes.But i am not able to find click event of this button.

This User-control loaded on another page on event of submit button on that page. But the number of that user control on a page varies so for this i have user a code that dynamically creates a list of user control.

For example purpose i have set j value up to 10, actually it varies Looks like this:

for (int j = 1; j < 10; j++)
{
    TableRow row = new TableRow();
    TableCell cell = new TableCell();

    cell.Text = j.ToString();
    cell.Height = 10;
    cell.Width = 75;
    row.Height = 10;
    cell.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.Gray);
    //cell.BackColor = #FFFFFF;
    row.Cells.Add(cell);

    TimePeriod ib = (TimePeriod)LoadControl("TimePeriod.ascx");
    TableCell cell1 = new TableCell();
    cell1.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.Gray);
        cell1.Controls.Add(ib);
        row.Cells.Add(cell1);

        tbl1.Rows.Add(row);
    cell = null; row = null;
}

You need to ensure that the event is added on each postback of the page. Don't limit it to the first pageload ie !Page.IsPostBack as is common.

Also you need to contruct and load your dynamic controls on Page_Init not Page_Load or anywhere else. It's a common mistake. If you construct on Page_Load or anywhere else then they don't get added to ViewState and cause problems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM