ASP.Net (not Core) Button Controls to stop postback

30 views Asked by At

Unfortunately I have searched everywhere and no solutions have worked thus far. The company I work for still uses the old ASP.Net and wont pay to upgrade all our code to ASP.Net Core. The version of ASP.Net I am using is 4.7.2. Here is the issue. I have initiated a button in the code behind that is dynamically created and coded depending on the type of button needed. I need it to ONLY execute the javascript on the client side. It keeps trying to do a postback regardless of what I try.

What I have tried so far is the following (some of them in combination with others):

btn.UseSubmitBehavior = false
btn.OnClientClick = "function(); return false;"
btn.CausesValidation = false
btn.Attributes["onclick"] = "function(); return false;"

I have tried this with the Button and LinkButton Controls, and cannot get either to work.

Here is the code that creates the button in the code behind: Note: I gave the function a specific name, and the text and ID, for example only, as these are actually variables that are dynamical in value

Button btn = new Button();
btn.Attributes["style"] = "scale: 78%; box-shadow: 0 4px 4px 0 rgba(0,0,0,1);";
btn.CssClass = "stdBtn";
btn.Text = "End Event";
btn.UseSubmitBehavior = false;
btn.OnClientClick = "endEventModal();return false;";
btn.CausesValidation = false;
btn.ClientIDMode = ClientIDMode.Static;
btn.ID = "btn_endEvent";
pnl.Controls.Add(btn);

I have included a snipet of what the website has after the code is run (Note that even with everything saying dont do post back, it somehow gets included in the code anyway. The Javascript also has "return false" in it): snipet of code using Dev Inspection Tool in Chrome](https://i.stack.imgur.com/9tr8i.png)

0

There are 0 answers