Asp.net Button Object - Runat Server Problem

I have a little problem with creating a server side button. I am using stringbuilder to build a table in html and I want to create a new button object and pass it to my stringbuilder object. I have a problem - my page does not compile when I am debugging. I want to give the button object the runat = "server" property, but I do not know how to do this.

Button add_img_popup = new Button(); add_img_popup.Text = "Add new Image"; // text add_img_popup.Click += new EventHandler(addImgPopup_Click); // click event handler add_img_popup.Enabled = true; add_img_popup.ID = "btn_add_img_popup"; // programmatic id Page.Controls.Add(add_img_popup); // add to page controls so it renders st.Append("<tr>"); st.Append("<td>"); st.Append(add_img_popup); // pass to string builder st.Append("</tbody>"); st.Append("</table>"); 

And the message that the server gives me.


Server error in application "/".

The btn_add_img_popup control of type 'Button' must be placed in the form tag using runat = server. Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for more information about the error and its occurrence in the code.

Exception Details: System.Web.HttpException: The control 'btn_add_img_popup' of type 'Button' must be placed inside the form tag using runat = server.

Source Error:

An unhandled exception was thrown during the execution of the current web request. Information about the origin and location of the exception can be identified using the trace of the exception stack below.

Stack trace:

[HttpException (0x80004005): the btn_add_img_popup control of type 'Button' must be placed in the form tag using runat = server.] System.Web.UI.Page.VerifyRenderingInServerForm (Control control) +8702387 System.Web.UI.WebControls .Button.AddAttributesToRender (HtmlTextWriter writer) +54 System.Web.UI.WebControls.WebControl.RenderBeginTag (HtmlTextWriter writer) +20 System.Web.UI.WebControls.WebControl.Render (HtmlTextWritereb.U + 20. Control.RenderControlInternal (HtmlTextWriter writer, adapter adapter adapter) +27 System.Web.UI.Control.RenderControl (HtmlTextWriter writer, adapter adapter adapter) +99 System.Web.UI.Control.RenderControl (HtmlTextWriter writer) +25 System.Web .UI.Control.RenderChildrenInternal (HtmlTextWriter writer, children ICollection) +134 System.Web.UI.Control.RenderChildren (HtmlTextWriter writer) +19 System.Web.UI.Page.Render (HtmlTex tWriter writer) +29 System.Web.UI.Control.RenderControlInternal (HtmlTextWriter writer, adapter adapter adapter) +27 System.Web.UI.Control.RenderControl (HtmlTextWriter writer, adapter adapter adapter) +99 System.Web.UI.Control .RenderControl (HtmlTextWriter writer) +25 System.Web.UI.Page.ProcessRequestMain (Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

+4
source share
4 answers

do not do that:

 Page.Controls.Add(add_img_popup); 

have a div with runat=server on your page inside the Form tag, and if the div is called myDiv , follow these steps:

 myDiv.Controls.Add(add_img_popup); 
+3
source

It looks like you might have a couple of problems:

Missing form tag

Make sure your page has a form tag with the specified "runat = server".

 <form id="form1" runat="server"> <asp:PlaceHolder ID="ph1" runat="server"/> </form> 

Code for:

 // Create the button control Button add_img_popup = new Button(); add_img_popup.Text = "Add new Image"; // Add it the placeholder // (which is inside the form) ph1.Controls.Add(add_img_popup); 

Button added to row builder

Adding a control to the row builder will not render it. First you need to visualize the control in a string using something like this:

 public string RenderControl(Control ctrl) { StringBuilder sb = new StringBuilder(); StringWriter tw = new StringWriter(sb); HtmlTextWriter hw = new HtmlTextWriter(tw); ctrl.RenderControl(hw); return sb.ToString(); } 

You can use the RenderControl method as follows:

 st.Append("<tr>"); st.Append("<td>"); st.Append(RenderControl(add_img_popup)); st.Append("</tbody>"); st.Append("</table>"); 

Although this will lead to control, you may have problems handling the postback event (addImgPopup_Click), since the control must be recreated every time the page loads.

Why do you need to add a control to StringBuilder instead of just entering the control directly on the page? There are usually better ways to achieve the goal using existing ASP.NET controls, rather than first displaying something in a string.

+4
source

There is another way to do this ...

Place the <asp:button on the page, create a click event for it, and then mark it as hidden. Then, when you create the table, you just need to encode a regular html button with an OnClick event to trigger the button callback ...

0
source

As the error says, you should put your button in a tag. therefore, easily add it to your stream.

0
source

Source: https://habr.com/ru/post/1379458/


All Articles