MultiUser Posted August 30, 2004 Posted August 30, 2004 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?? Quote
eramgarden Posted August 30, 2004 Posted August 30, 2004 hmm, maybe change the text in ispostback block: if not page.ispostback then 'change the text here end if Quote
*Gurus* Derek Stone Posted August 30, 2004 *Gurus* Posted August 30, 2004 You'll need to use JavaScript and the exposed DOM to change the button's text on the client. Quote Posting Guidelines
Arch4ngel Posted August 30, 2004 Posted August 30, 2004 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. Quote "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
MultiUser Posted August 31, 2004 Author Posted August 31, 2004 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. Quote
*Gurus* Derek Stone Posted September 1, 2004 *Gurus* Posted September 1, 2004 Why would you be dynamically changing the text on a button? A user's language doesn't change from page to page. That should all be done server-side. Quote Posting Guidelines
PWNettle Posted September 1, 2004 Posted September 1, 2004 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.