0

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:

enter image description here

What am I doing wrong here?

Community
  • 1
  • 1
EldarGoren
  • 107
  • 11
  • Do you want to use html button or ASP button – Venkateswaran R Mar 31 '17 at 10:27
  • I tried both but it didn't work. I think I prefer asp button. – EldarGoren Mar 31 '17 at 11:01
  • Yes, you create asp button, have a look http://stackoverflow.com/questions/10397856/add-aspbutton-from-codebehind and http://stackoverflow.com/questions/188007/creating-an-aspbutton-programatically – Venkateswaran R Mar 31 '17 at 11:02
  • I tried to do it but how am I suppose to add it to the content variable? because i can't to it like this `content += likeButton;` and also can i put html tags in it? like this: `likeButton.Text = " 23 ";` The content variable is in the aspx side `<%=content %>` Ty for helping! – EldarGoren Mar 31 '17 at 11:10
  • Maybe I should do it with html button? – EldarGoren Mar 31 '17 at 11:43

1 Answers1

0

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);
    }
kaushalparik27
  • 794
  • 2
  • 7
  • 19
  • I understand your answer but I need to create the buttons inside div, I have unknown number of divs because im making them dynamically - div for each record on database. Is it possible to it like that? – EldarGoren Mar 31 '17 at 13:08
  • I can't do it like that? `content += "";` – EldarGoren Mar 31 '17 at 13:12
  • No, you cannot create server controls like that. Even if you are to create DIVs then also you can use the same mechanism shown above – kaushalparik27 Apr 01 '17 at 09:47