im creating a button from c# side like this:
content += "<button class='btn btn-info' runat='server' style='margin-left:4%' id='Like" + dr[0] + "' onclick='likeClick'>";
And the event is really simple i just want to see if its working:
protected void likeClick(object sender, EventArgs e)
{
content = sender.ToString();
}
But when i click the button it doesn't trigger the event, I put a break point in the event and I debugged but it didn't start the fucntion. I opened Inspect Element in the browser and clicked the button. It showed me this:
What am I doing wrong here?
I understand that you want to create a Button
server control dynamically. The way, you are creating is wrong. You have to instantiate a Button
object, declare the Click
event and add it to your page. Also, since it is a dynamically created control, it will required to be re-created on every postback. Here is a sample code on similar scenario:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder runat="server" ID="phControl" />
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
Button btnButton = new Button();
btnButton.Style.Add("margin-left", "4%");
btnButton.ID = "like"+ dr[0];
btnButton.Text = "Like";
btnButton.Click += BtnButton_Click;
phControl.Controls.Add(btnButton);
}
private void BtnButton_Click(object sender, EventArgs e)
{
string content = sender.ToString();
Response.Write(content);
}
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.