Want a button that doesn't postback but text can be changed through code

MultiUser

Newcomer
Joined
Aug 30, 2004
Messages
2
Hi there,

My first post here. I'm a little stuck.

I want to create a button where I can change it's .Text value through code, but I DO NOT want the button to postback when it is clicked (I'm using a client side 'onClick' event). It's for a help button basically.

I tried using the (standard) web forms button, but I can't stop it from doing postbacks (the runat="client" won't work).

I tried using HTML buttons, but I can't change .Value through code.

Any suggestions??
 
Derek is right. Javascript is the only way without post back. And be sure not to use a WebForm button. Use an HTML button. (It's in the ToolBox). A WebForm button will always do a postback (if I'm right).

Then you write your Javascript and you link it to your HTML button.
 
Hmmm... that's too bad. I guess my users will have to live with a postback occuring when they click the help button. You see I use resource files to dynamically change the text of the buttons depending on the user's language settings... and I can't access the resource file (as far as I know), through javascript.

Thanks for everyone's response.
 
Some thoughts...

MultiUser said:
I tried using HTML buttons, but I can't change .Value through code.

Sure you can, if you set the html button to be runat=server and declare it in your code behind.

For ex, in your aspx you'd have:

Code:
<p><input id="btnTest" type="button" value="Blah" runat="server" /></p>

And in codebehind you'd declare it as (C# example):

Code:
protected System.Web.UI.HtmlControls.HtmlInputButton btnTest;

And manipulate it (C# example):

Code:
btnTest.Value = DateTime.Now.ToString();

Voila! A button with properties you can completely control from codebehind that doesn't cause a postback when rendered.

That works and is easy - but as a worst case scenario you can always generate whatever you need from codebehind by more explicit means. For example - you could write out the html for the button into a div or something:

Code:
<!--  Setup a div to manipulate via code behind in the aspx  -->
<div id="divButton" runat="server"></div>

Code:
//  Declare the div in code behind.
protected System.Web.UI.HtmlControls.HtmlGenericControl divButton;

Code:
//  Set the button text.
string sButtonText = "I am the button!";
//  "write" the button to the div tag.
divButton.InnerHtml = "<input type=\"button\" value=\"" + sButtonText + "\">";

The second method here is a little more bulky and unnecessary since the HtmlInputButton works just fine - but it's another way to tackle the problem.

Paul
 
Back
Top