Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Custom server control is posting back twice

Custom server control is posting back twice

Scheduled Pinned Locked Moved ASP.NET
htmldesignsysadminquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chubbysilk
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • C chubbysilk

      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

      C Offline
      C Offline
      chubbysilk
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups