Change the text on an aspx button

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

I have the following button:
<asp:LinkButton id="Btn" runat="server" text="Click">
</asp:LinkButton>

I have added an action listener in the javascript
where I change the text on it:
this.dom.textContent = "Clicked";

The change is effective but after the site has
reloaded the text is back to "Click".

How can I make the change sustain? Should I use
something else than dom.textContent?
 
Hello,

you should change the text on the server as controls are generated from the
server to the client. Therefore you should ignal to the server that it
should change text to what you need in this scenario (post it to the server
for example with a hidden form field)
 
you should change the text on the server as controls aregenerated from
the server to the client. Therefore youshould ignal to the server that
it should change text towhat you need in this scenario (post it to the
server
for example with a hidden form field)

Perhaps i'm doing it a strange way. I'm changing the text
of the LinkButton using JavaScript on the client and as i
post it back, i try to read the text using the following.

protected void Button1_Click(Object sender, EventArgs e) {
this.Button1.Text = this.Button1.Text + "!"; }

I notice that the "!" gets added at every time so the text
on the button gets more and more exlamation marks. Still,
i figure it should be possible to change the original text
of the button to something else and then send it back to
server. For some reason, the server only remembers the
text sent out at previous attempt.

And just to be perfectly clear - i can use a button as such
a field, right? Or are we taking about a specialized, hidden
componenet here?
 
Perhaps i'm doing it a strange way. I'm changing the text
of the LinkButton using JavaScript on the client and as i
post it back, i try to read the text using the following.

protected void Button1_Click(Object sender, EventArgs e) {
this.Button1.Text = this.Button1.Text + "!"; }

I notice that the "!" gets added at every time so the text
on the button gets more and more exlamation marks. Still,
i figure it should be possible to change the original text
of the button to something else and then send it back to
server. For some reason, the server only remembers the
text sent out at previous attempt.

And just to be perfectly clear - i can use a button as such
a field, right? Or are we taking about a specialized, hidden
componenet here?

Hi

on post back of the page simply check your link's text
and re assign your text depanding on your desired value.

you can take a hidden field in your page mark it as runat server.
in javascript when you change the text of the link mark the hidden
input to know that links text is chnage
when page post back .. grap the value of the hidden field and then
depending on value modify your links
text.


Thanks
Munna
www.munna.shatkotha.com
www.shatkotha.com
 
protected void Button1_Click(Object sender, EventArgs e) {
on post back of the page simply check your link's text
and re assign your text depanding on your desired value.

That's exactly what i'm doing (i think). The problem is that
the text i've assigned to the asp:LinkButton in the JS-code
is not there anymore!

this.dom.textContext = "beep";

The above does chenge the text of the button and i can
actually see that on the screen. However, when the page
is posted back and returned to the client, the old text
is there right back...

To be even more clear - the text i see changes as follows.

org -> beep -> org! -> beep -> org!! -> beep -> org!!!

I can't explain how the server knows what the previously
assigned text is but somehow it does. Suggestions?
 
Value of Text property of the Button is kept at the server-side. And then
the control is rendered on the server, markup is generated and sent to the
client browser. So if you just change the markup in client browser, that
isn't signaled anyway to the server so it cannot change the text of what you
have sent from the client, unless you provide a mechanism to carry that
value from client to server (hidden field is one such). Note that you have
HTTP requests and responses going on this picture.

" protected void Button1_Click(Object sender, EventArgs e) {
this.Button1.Text = this.Button1.Text + "!"; }

I notice that the "!" gets added at every time so the text
on the button gets more and more exlamation marks"

This is essentially adding exclamation to the Text property at the server,
and then at the end of processing, it's rendered again to the browser. So it
is creating a illusion that you have state (or you do, but its created by
ASP.NET, HTTP itself is stateless)

With hidden field is meant that you have

<input type="hidden" ID="hidden1" runat="server" />

you set it's value in javascript and read it in Page_Load at the server for
example:

Button1.Text = hidden1.Value;

which should change the Button's text according to what you type to it (or
set with js)


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


K Viltersten said:
on post back of the page simply check your link's text
and re assign your text depanding on your desired value.

That's exactly what i'm doing (i think). The problem is that
the text i've assigned to the asp:LinkButton in the JS-code
is not there anymore!

this.dom.textContext = "beep";

The above does chenge the text of the button and i can
actually see that on the screen. However, when the page
is posted back and returned to the client, the old text
is there right back...

To be even more clear - the text i see changes as follows.

org -> beep -> org! -> beep -> org!! -> beep -> org!!!

I can't explain how the server knows what the previously
assigned text is but somehow it does. Suggestions?
 
Value of Text property of the Button is kept at the
server-side. And then the control is rendered on the
server, markup is generated and sent to the client
browser. So if you just change the markup in client
browser, that isn't signaled anyway to the server so
it cannot change the text of what you have sent from
the client, unless you provide a mechanism to carry
that value from client to server (hidden field is
one such). Note that you have HTTP requests and
responses going on this picture.

With hidden field is meant that you have
<input type="hidden" ID="hidden1" runat="server" />
you set it's value in javascript and read it in
Page_Load at the server for example:

Button1.Text = hidden1.Value;

which should change the Button's text according to
what you type to it (or set with js)

Right now, i could kiss you. (Good thing you're
far away in Finland, hehe.)

Great thanks!
 
Back
Top