DividesByZero
DividesByZero

Reputation: 83

dynamic link button(Created in dynamic Table cell) event is not firing

my problem is that i am creating dynamic link buttons in a dynamically created cell.its working fine but the click event handler of dynamic link button is not firing.i have searched enough on internet but nothing is working out..im calling a method on page load which in turn is calling the method for creating dynamic cell and link buttons. but still the click event is not firing...even oninit wont work because structure of my project is such.is there any other solution for this.im calling a method on page load which does some work and thn calls this create method.

     protected void create()
      {

        for (int j = 0; j < 8; j++)
       {
        Label lbl;
        LinkButton lnk;
        TableRow tRow = new TableRow();
        for (int i = 0; i < 6; i++)
        {
            TableCell tCellLessonPlan = new TableCell();
            lbl = new Label();

            lnk = new LinkButton();


                lnk.Click += new System.EventHandler(this.LinkButton_Click);


                lbl.Text = "Period";
                lnk.Text = "Details";
                tCellLessonPlan = createCell(tCellLessonPlan.Text);
                tCellLessonPlan.Controls.Add(lbl);
                tCellLessonPlan.Controls.Add(lnk);
               tRow.Cells.Add(tCellLessonPlan);

        }
        Table1.Rows.Add(tRow);
    }
}

     protected TableCell createCell(string cellText)
{
    TableCell tCell = new TableCell();
    Unit height = new Unit(20, UnitType.Pixel);
    tCell.Height = height;
    Unit width = new Unit(190, UnitType.Pixel);
    tCell.Width = width;
    tCell.BorderStyle = BorderStyle.Solid;
    tCell.BorderColor = System.Drawing.Color.Black;
    tCell.BorderWidth = Unit.Point(2);

    tCell.Text = cellText;
    return tCell;

}


protected void LinkButton_Click(object sender, EventArgs e)
{
    LinkButton lnk = (LinkButton)sender;
    //Some logic here.
}

Upvotes: 0

Views: 3080

Answers (1)

Nikola Radosavljević
Nikola Radosavljević

Reputation: 6911

From code you pasted and fact that you say it is called from Page_Load or earlier this should work. You are probably omitting some significant code. To debug this

  1. Open the web page
  2. View source, and find your button and check it's ID
  3. Set a breakpoint in Page_Load
  4. Click the button
  5. View Request.Form and check if button ID is posted from client

It is possible that you are not always creating same controls, and that they get different IDs based on that. You need to make sure that IDs are not different when button is first rendered, and when PostBack occurs. To do this, set IDs of the button and it's parents on server side.


How do ASP.NET events work

Web sites are stateless user interface. This means that server produces output which is displayed to user and no longer cares about them. In order to handle users request for some work, users browser sends information to the server. This is also the case for handling events. For server to know which event needs to be handled, it has to create all controls, then check data which user sent and see which control user targeted with his action. Understandably, it is necessary that controls are created early enough so that server can look for them and find event handlers for them.

Page life-cycle has several stages. Life cycle starts when users browser sends a request to server, and ends when server outputs response to that request. At the time users browser has fully loaded the page, server no longer knows about it. Once a button which sends data back to server is clicked, server again creates the page based and page goes through another life-cycle. Life-cycle stages are:

  1. Page request - Page is not being created at this stage, only request from client has been received.
  2. Start - Basic pre-processing of the page is performed. Check is made to see if request is a post back or not.
  3. Initialization - Controls have been created at this point, but view state and post back data has not been processed yet
  4. Load - Control information is recovered from view state and control state
  5. Postback event handling - Postback .... event handling
  6. Rendering - View and control states are saved, and Render is called for each control down control tree
  7. Unload - Request being discarded

As you may have guessed 3, 4 and 5 are important for you. You should create your control in initialization stage, or latest in load stage, because in next stage postback event handling is done.

How does server know which event handler to invoke? User posts back all information needed to locate control who's event was triggered, and event itself. This data depends on control type.

Take following code for example:

protected void Page_Load(object sender, EventArgs e)
{
    CreateControls();
}

public void CreateControls()
{
    LinkButton linkBtn = new LinkButton();
    linkBtn.ID = "btnTest";
    linkBtn.Text = "test";
    linkBtn.Click += new EventHandler(btn_Click);
    pnl.Controls.Add(linkBtn);

    Button btn = new Button();
    btn.ID = "btnTest2";
    btn.Text = "test";
    btn.Click += new EventHandler(btn_Click);
    pnl.Controls.Add(btn);
}

Here, we are dynamically adding controls and attaching event handlers. Here is how Request data that user sent looks like when page is first rendered: Request data on first run

When user clicks LinkButton, page is handled again, and user sends additional information to the page to see what needs to be done: Request data after LinkButton is clicked Users browser sends control ID in form data, under __EVENTTARGET key. Server checks if __EVENTTARGET is set, and if it is it knows that an event needs to be handled. This happens in stage 5 from above. Server looks for control and triggers it's default event (Click in case of a LinkButton and Button).

Similar thing happens for Button control: Request data after Button is clicked This time, control ID is sent as a key value of form data. In form data, under that key is value of html element which represented the button (in our case, value is test as this is buttons text).


This should be enough to understand the basics of how events are handled in ASP.NET. Now according to this, place a breakpoint in Page_Load, check Request.Form["__EVENTTARGET"] and see if it is set. If it is set, check if your control has been created at that point, if it has that same ID, and if event handler has been attached to it. If event is still not triggered, then you are either removing that control (or it's parent) or detaching event handler before stage 5 of request life-cycle.

Upvotes: 3

Related Questions