Modifying ASP button with javascript variable

S

Sean

I have a situation whereby I need to modify the text string appearing on an
ASP button with some text derived from a Javascript function. But I am
unsure of the correct syntax to do so.

Currently the I am just modifying the text in a SPAN next to the button as
opposed to change the button text itself. But it's not ideal really.

Currently I am have this:

<div id="section2">
<asp:TextBox CssClass="textbox" ID="txtSearch" runat="server"
Width="370px" TabIndex="0" />
<asp:Button ID="cmdDoIt" runat="server" onclick="cmdDoIt" Text="Search"
/>
<script type="text/javascript" language="javascript">
function ChangeOptionName(sText) {
document.getElementById("notify_using_se").innerHTML = "with " +
sText; }
</script>
<span id="notify_using_se"></span>
</div>

The javascript function is called by the on_click event of an option button
but I want to change Text="Seach" of the ASP button to the text returned by
the Function ChangeOptionName(sText)

Thanks in advance for any guide lines :)
 
C

Cowboy \(Gregory A. Beamer\)

There are numerous options.

The first is creating a composite control that outputs the JavaScript to
change the funcationality. Think of this as deriving your own "change
button" from the asp.net button. This is a bit of work, if you are only
using this in one place.

Another is to emit your own script using the button's rendered name. I do
not have an example of this, but it happens server side before you render
the page. Once you have the client side name, you can alter any of the
properties.

You might try just looking at the rendered page and writing JavaScript. I
see this all the time, but I would not do this. Why? If forces you to change
your code every time you alter how the button is rendered. For example, you
find the button would be better in a user control. It is also a potential
maintenance problem. And not wise, esp. since you can get the client side
name from the control server side.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
S

Sean

Mark Rae said:
You should remove the language tag, as that has been deprecated for quite
a while...



document.getElementById("<%=cmdDoIt.ClientID%>").value = "with " + sText;

Perfect thanks for the help!
 
B

bruce barker

pretty trivial to change the text of a button:

document.getElementById('<%=cmdDoIt.ClientID%>').value = "new caption";

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top