Button that doesn't post back, but has onchange javascript function

I want to have a button that looks like a button, but doesn't post back to the server... so I guess I couldn't really call it a button. I have a button control with

      addTransBTN.Attributes.Add("onclick","openNewWin();");

but it still posts back to the server when clicked, causing my page to reload. Do I use a different control, or how does one normally work around this?
RichardFoxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
DotNetLover_BaanConnect With a Mentor Commented:
Hi there ..
get an HTML button as gspronych said. Right click on it. Make it "Run at server". Assign a name to it using Property box. Say "Button1". In the page load add the attribute...

        Button1.Attributes.Add("OnClick", "javascript:openNewWin();")

-Baan
0
 
gspronychConnect With a Mentor Commented:
If you want a button that doesn't do a server postback, you want a input box.
<INPUT type="button" value="Button">

Or click the toolbox, select HTML, and drag the button on the form.

The HTML button does not perform a server side postback
0
 
RichardFoxAuthor Commented:
ok, but how do I work with this in a codebehind file?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
gspronychCommented:
If you want it to run codebehind richardfox it needs to be a server control as Baan said.
BUT a server control will cause a postback.

If the name of the control is Button1, put this code in your page_load
        Button1.AutoPostBack = False


0
 
DotNetLover_BaanCommented:
No.. gspronych  , if it is an HTML control.. it won't.
0
 
RichardFoxAuthor Commented:
I need to create these controls dynamically. I understand the suggestion, but I need to add the HTML button when I create the rest of my controls, so I can't drag it from the toolbox and right click it and specify "Run at server". How can I do this from code?
0
 
RichardFoxAuthor Commented:
I created a dynamic control

HtmlControls mybutton;

and set the onclick function, works great, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.