views:

558

answers:

4

Hi,

I have a search button on my page that runs a query on a DB, pulls out and displays some entries in a table, and for each entry I create a button. It looks something like this:

List<Friend> friends = SearchFriend(searchStr);
foreach (Friend f in friends)
{
    TableCell addCell = new TableCell(), nameCell = new TableCell();

    addCell.Text = "";
    if (!f.IsMyFriend)
    {
            LinkButton addFriendBtn = new LinkButton();
            addFriendBtn.Text = "Add as Friend";
            addFriendBtn.Click += new EventHandler(addFriendBtn_Click);
            addFriendBtn.ID = "add_" + f.ID.ToString();

            addCell.Controls.Add(addFriendBtn);
    }
    nameCell.Text = f.Name;

    TableRow row = new TableRow();
    row.Cells.Add(addCell);
    row.Cells.Add(nameCell);

    SearchFriendTable.Rows.Add(row);
}

Problem is that the LinkButton event does not fire when it is pressed (changing LinkButton to a simple Button does not fix this either).

This is the html that I get in this portion:

<td><a id="ctl00_contentPH_add_2" href="javascript:__doPostBack('ctl00$contentPH$add_2','')">Add as Friend</a></td>

Also - when I put a breakpoint on Page_Load I do see the __EVENTTARGET with this control's id in it - however the event never starts running.

Any clues? Thanks.

A: 

Where and when did you created that button?

If you dynamically create Buttons and want to listen to a event you have to create that button in the PageInit event. Always! So do not use if(!IsPostback)

Arthur
You're right - I'm not creating the button inside the Page\_Load but rather inside of another event. How can I fix it then? I did not understand the "if(!IsPostBack)" part of your answer...
emk
You have to create the buttons in the Page_Init event. With !IsPostback I mean that you have to recreate the button every request. Usually you initializes a Grid or similar only once - with the !IsPostback check
Arthur
+1  A: 

Try with this.

<td><a id="ctl00_contentPH_add_2" href="javascript:__doPostBack('<%=ct100_contentPH_add_2.ClientId %>','')">Add as Friend</a></td>
solairaja
A: 

Add event handler for the link buttons and handle those events.

 if (!f.IsMyFriend)
    {
            LinkButton addFriendBtn = new LinkButton();
            addFriendBtn.Text = "Add as Friend";
            addFriendBtn.Click += new EventHandler(addFriendBtn_Click);
            addFriendBtn.ID = "add_" + f.ID.ToString();
            addFriendBtn.Click += new EventHandler(addFriendBtn_Click);
            addCell.Controls.Add(addFriendBtn);
    }

the event:

  protected void addFriendBtn_Click(object sender, EventArgs e) 
        {
            LinkButton lnk = (LinkButton)sender;
            // do your coding
    }
Himadri
I have an event handler, of course :). Problem is that the code inside of it never starts running.
emk
A: 

Adding to Himadri's answer:

Dynamically added controls need to be rewired in page init. Then the event will fire. I had a very similar issue http://stackoverflow.com/questions/1315102/dynamically-loaded-controls-in-a-wizard

Darthg8r