0

I am trying to have a button that I create via a stringbuilder call a C# module. No matter what I am trying I can not seem to get it working. Is there a JavaScript I need to do???

StringBuilder sb = new StringBuilder("");

tried:

sb.Append("<input type=\"button\" id=\"Button1\" value=\"Click Me\" runat=\"server\" onserverclick=\"Button1_Click\" />");

tried:

sb.Append("<button type=\"button\" onserverclick=\"Button1_Click\" runat=\"server\" id=\"Btn1\">Click Me</button>");

C# Code:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("Testing");
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
mrmcg
  • 173
  • 6
  • 17
  • 5
    If you want to add buttons based on data, rather than having them statically defined, you should really use a `GridView` or a `Repeater` to create them. Trying to do so entirely dynamically is a nightmare in ASP. – Servy Apr 15 '14 at 19:40
  • 1
    What's not working? Can you see the button on the page? – Rick S Apr 15 '14 at 19:44
  • I can see the button. But it is not firing at all. – mrmcg Apr 15 '14 at 19:45
  • 1
    Why are you trying to do it this way? Why not create a button object and add that control to the page? I also have to agree with Servy, you won't like having to deal with dynamic controls. Especially on postback. – Rick S Apr 15 '14 at 19:46
  • Yes, working on the page's object model is the right way to go in ASP.NET. And the event will have to wired up by `+=`. – Olivier Jacot-Descombes Apr 15 '14 at 19:49
  • @RickS You say that you agree with me while suggesting doing exactly what I specifically said shouldn't be done (dynamically creating the controls in code)? – Servy Apr 15 '14 at 20:04
  • @OlivierJacot-Descombes Doing that is *extremely* difficult, given the way ASP works. It forces the entire dynamic page to be built up on every single post back, in the appropriate stage in the page life cycle, for those event handlers to run. Doing this manually is extremely error prone and tedious. – Servy Apr 15 '14 at 20:05
  • @servy You're quick to judge everyone's comments but i don't see you posting an answer. – Rick S Apr 15 '14 at 20:07
  • @RickS And yet I explained how the OP can go about finding one. There are thousands of tutorials on the subject out there, the OP just needed to know that he should look one up. It's not an answer as per SO's definition, but it *is* all that the OP really needs to know in order to be able to solve his problem himself. – Servy Apr 15 '14 at 20:08

2 Answers2

1
protected void Page_Load(object sender, EventArgs e)
{
    Button button = new Button { ID = "btn1", Text = "Click Me" };
    button.Click += btn_Click;
    PlaceHolder btnPlaceHolder = new PlaceHolder();
    btnPlaceHolder.Controls.Add(button);
}

private void btn_Click(object sender, EventArgs e)
{
    Response.Write("Testing");
}
Vahi
  • 605
  • 1
  • 8
  • 17
  • The button click event won't fire here, because when the post back happens that button, and it's corresponding event handlers, won't exist. This is why dynamic controls are so annoying to work with. This looks like it will work, but it won't. – Servy Apr 15 '14 at 20:02
0

Use asp:Button instead

sb.Append("<asp:Button OnClick=\"Button1_Click\" runat=\"server\" ID=\"Btn1\">Click Me</asp:Button>");
Nanosoft
  • 119
  • 9
  • Why would someone upvote this when this has no chance of possibly resulting in a valid page? You cannot write ASP markup to the response. – Servy Apr 15 '14 at 20:03
  • The asker wants to build a string for an unknown reason, asp:Button gets executed through the code in my answer. I have a project where I have written asp:LinkButton in stringBuilder. I am damn sure it executes. @Servy – Nanosoft Apr 15 '14 at 20:10
  • This will just result in server side errors saying that invalid HTML is written to the response, or just a page that won't properly render when loaded, but it most certainly will not result in a button being placed on the page that will run the given code when clicked. – Servy Apr 15 '14 at 20:12
  • But you never know what are the circumstances of the asker. He may have handled all other things and only stuck at the problem he has shared (here Button), DownVote is bit harsh, may be. – Nanosoft Apr 15 '14 at 20:16
  • He very clearly said that he was trying to create a button that would execute some C# code when clicked. This doesn't do that. This creates a meaningless string that nothing can really use, and certainly can't be used for what he is asking for. Downvoting an answer that doesn't work is the only correct voting option. *Not* downvoting an answer that doesn't work in the slightest would be wrong. – Servy Apr 15 '14 at 20:19
  • You may be right. Seeing your reputation, I can't prove you wrong, but I can learn correct things from you. I have personally written asp:LinkButton in StringBuilder. The site is working. Now how about that. May be I might have added some other code unknowingly. Pls clarify. I tried above code (from my answer), it also works !! @Servy – Nanosoft Apr 15 '14 at 20:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50726/discussion-between-nanosoft-and-servy) – Nanosoft Apr 15 '14 at 20:29