Custom server control is posting back twice
-
Hello, I have built a custom server control with a few buttons in it. The first time I push a button in the control, it runs the code twice. Does anybody have any idea why? If I push the same button again, it only runs the code once. Here is the main code for the control:
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace TitleBar { /// /// Summary description for TBar. /// [DefaultProperty("Text"), ToolboxData("<{0}:TBar runat=server>")] public class TBar : System.Web.UI.WebControls.WebControl, IPostBackDataHandler, IPostBackEventHandler { public event EventHandler SaveClick; /// /// Render this control to the output parameter specified. /// /// The HTML writer to write out to protected override void Render(HtmlTextWriter output) { System.String quote = System.Convert.ToChar(34).ToString(); System.Text.StringBuilder SBOut = new System.Text.StringBuilder(); SBOut.Append(" Here is the code in the webpage: `protected TitleBar.TBar TBar1; private void Page_Load(object sender, System.EventArgs e) { TBar1.SaveClick +=new EventHandler(testSaveClick); } public void testSaveClick(object sender, System.EventArgs e) { // some code }` It seems like the RaisePostBackEvent function is being called twice the first time the page posts back. But I can't figure out why this is happening. Thanks, RC
-
Hello, I have built a custom server control with a few buttons in it. The first time I push a button in the control, it runs the code twice. Does anybody have any idea why? If I push the same button again, it only runs the code once. Here is the main code for the control:
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace TitleBar { /// /// Summary description for TBar. /// [DefaultProperty("Text"), ToolboxData("<{0}:TBar runat=server>")] public class TBar : System.Web.UI.WebControls.WebControl, IPostBackDataHandler, IPostBackEventHandler { public event EventHandler SaveClick; /// /// Render this control to the output parameter specified. /// /// The HTML writer to write out to protected override void Render(HtmlTextWriter output) { System.String quote = System.Convert.ToChar(34).ToString(); System.Text.StringBuilder SBOut = new System.Text.StringBuilder(); SBOut.Append(" Here is the code in the webpage: `protected TitleBar.TBar TBar1; private void Page_Load(object sender, System.EventArgs e) { TBar1.SaveClick +=new EventHandler(testSaveClick); } public void testSaveClick(object sender, System.EventArgs e) { // some code }` It seems like the RaisePostBackEvent function is being called twice the first time the page posts back. But I can't figure out why this is happening. Thanks, RC
I figured out what the problem was. My server control was inheriting from the System.Web.UI.WebControls.WebControl, which i assume was causing an AutoPostBack in addition to the PostBack that I was forcing through the javascript written in the Render function. I changed it so that the server control now inherits from the System.Web.UI.WebControls.Control class. It no longer posts back twice when a button is clicked. RC