Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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??

Posted

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.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

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.

Posted

Some thoughts...

 

I tried using HTML buttons' date=' but I can't change .Value through code.[/quote']

 

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:

 

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

 

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

 

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

 

And manipulate it (C# example):

 

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:

 

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

 

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

 

//  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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...