Code execute twice ?

Results 1 to 4 of 4

Thread: Code execute twice ?

  1. #1
    Join Date
    Dec 1969

    Default Code execute twice ?

    I was under the impression that when I have an &#060;asp:button&#062; that I had to create an onclick attribute. Then go in the code behind and create the handler for that onclick attribute.<BR><BR>So this is what I did:<BR>&#060;asp:button id="btnSudmit" text="Enter" onclick="btnSubmit_Onclick" runat="server"/&#062;<BR><BR>Then in the code behind page, I created the handler like this:<BR>public void btnSubmit_Onclick(object sender, System.EventArgs e)<BR>{<BR> //. . . whatever code . . .<BR>}<BR><BR>Now I place a breakpoint on the handler and start debuggin only to realize that the code is being executed twice!<BR><BR>If I *remove* the onclick attribute inside the &#060;asp:button&#062; then the code only executes once which is what I want.<BR><BR>Is this normal behavior?<BR>Was I wrong to think that I needed an onclick attribute inside my &#060;asp:button&#062; ?<BR><BR>Anyone ?<BR><BR>Thanks in advance<BR>Sincerely<BR><BR>Vlince

  2. #2

    Default RE: Code execute twice ?

    Look in the code that is genrated by VS.Net, you probably have a .Click += new EventHandler(...)<BR><BR>This is the equivalent of specifying in the html, so you have both of them in effect

  3. #3
    Join Date
    Dec 1969

    Default You're right...

    ...there was a:<BR>this.btnSudmit.Click += new System.EventHandler(this.btnSubmit_Onclick);<BR>in side the InitializeComponent() method<BR><BR>How come?<BR>Does it mean that each time you place a control or type one by hand, it will do that?<BR><BR>Gotta love that generated code...:(<BR><BR>Thanks Andrew<BR>Sincerely<BR><BR>Vlince<BR><BR>

  4. #4

    Default RE: You're right...

    He he,<BR><BR>When you double click on the button for the first time, VS Generates the code.<BR><BR>The html attribute wireup of the handler is used mostly when not using code-behind.<BR>It can also be usefull on controls inside templates.<BR><BR>If you put &#060;%=asdf()%&#062; in your aspx page, it will not compile, but the browser should have a link to View Complete source in that code you should see an equivalent wireup based on the html attribute.<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts