How to link a div Click to button click server side for dynamic controls

522 views Asked by At

I've got a requirement where I need to be able to click dynamically generated div's

So,I've generated buttons also dynamically and want to link the ClickEvent of Button to div's Click event.

for (int i = 0; i < DtUsers.Rows.Count; i++)
{
     ASPxButton btnButton = new ASPxButton();
     btnButton.ID = "btnButton" + (i + 1);
     btnButton.Visible = false;
     btnButton.Click += new EventHandler(this.btnButton_Click);
     btntButton.CommandArgument = DtOnlineUsers.Rows[i]["USER_ID"].ToString();        

     var divCustomItem = new HtmlGenericControl("div");

     divUandMeUsers.Controls.Add(divCustomChatItem);

     divCustomItem.Controls.Add(btnButton);
     divCustomItem.Attributes.Add("onclick", "document.getElementById('<%= " + btnButton.ClientID + "  %>').click()");
}

But,it is not working. Can you tell me where I've done wrong?
Is there something I need to do with the UpdatePanel AsyncPostBackTrigger?
When I click the div, in the developer tools, it is saying Uncaught TypeError: Cannot read property 'click' of null

1

There are 1 answers

0
Guy Lowe On

Change

divCustomItem.Attributes.Add("onclick", "document.getElementById('<%= " + btnButton.ClientID + "  %>').click()");

to

divCustomItem.Attributes.Add("onclick", "document.getElementById('" + btnButton.ClientID + "').click()");

You don't need the client side server includes during a server side operation.

Edit: OK so I've gone through your code with a finer toothed comb and I've found these additional problems:

  1. The button's visibility is set to false so it won't even be rendered. If you want it to be clickable then you will need to hide it using css visibility.
  2. What is an ASPxButton? Did you mean Button?
  3. As there is nothing in the div its auto height and width will be set to zero.

Try sorting out those issues and post your new code if this still does not work.