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");
}
9
  • 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. Commented Apr 15, 2014 at 19:40
  • 1
    What's not working? Can you see the button on the page? Commented Apr 15, 2014 at 19:44
  • I can see the button. But it is not firing at all. Commented Apr 15, 2014 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. Commented Apr 15, 2014 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 +=. Commented Apr 15, 2014 at 19:49

2 Answers 2

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

Use asp:Button instead

sb.Append("<asp:Button OnClick=\"Button1_Click\" runat=\"server\" ID=\"Btn1\">Click Me</asp:Button>");

7 Comments

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.
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
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.