Set label's text clientside and use it to set button's text server side.
-
When I click Button1, Button2 gets enabled and the label's text is set to "bye". This is done with a bit of javascript. When Button2 is clicked, it's text should be set to "bye" - the new text value of the label, but is set to "hello" instead - the original text value of the label. How can I fix this? Aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript">
function Hello() {
document.getElementById('Button2').disabled = false;
document.getElementById('Label1').innerText = "bye";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Button1" onclick="Hello()" />
<br />
<asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2"
onclick="Button2_Click" />
<br />
<asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
</div>
</form>
</body>
</html>Cs:
protected void Button2\_Click(object sender, EventArgs e) { this.Button2.Text = this.Label1.Text; }
-
When I click Button1, Button2 gets enabled and the label's text is set to "bye". This is done with a bit of javascript. When Button2 is clicked, it's text should be set to "bye" - the new text value of the label, but is set to "hello" instead - the original text value of the label. How can I fix this? Aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript">
function Hello() {
document.getElementById('Button2').disabled = false;
document.getElementById('Label1').innerText = "bye";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Button1" onclick="Hello()" />
<br />
<asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2"
onclick="Button2_Click" />
<br />
<asp:Label runat="server" ID="Label1" Text="hello"></asp:Label>
</div>
</form>
</body>
</html>Cs:
protected void Button2\_Click(object sender, EventArgs e) { this.Button2.Text = this.Label1.Text; }
Markup - HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript">
function Hello()
{
document.getElementById('Button2').disabled = false;
document.getElementById('Label1').innerText = "bye";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Button1" onclick="Hello()" /><br /><asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" /><br /> <asp:Label runat="server" ID="Label1" Text="hello" /> </div> </form>
</body>
</html>Code-behind
void Page_Init(object sender, EventArgs e)
{
Button2.Click += new EventHandler(Button2_Click);
}protected void Page_Load(object sender, EventArgs e)
{}
void Button2_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Text = "Something new";
} -
Markup - HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript">
function Hello()
{
document.getElementById('Button2').disabled = false;
document.getElementById('Label1').innerText = "bye";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Button1" onclick="Hello()" /><br /><asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" /><br /> <asp:Label runat="server" ID="Label1" Text="hello" /> </div> </form>
</body>
</html>Code-behind
void Page_Init(object sender, EventArgs e)
{
Button2.Click += new EventHandler(Button2_Click);
}protected void Page_Load(object sender, EventArgs e)
{}
void Button2_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Text = "Something new";
}I need to use the label's new text value in the code behind for further processing. Using 'this.label1.text' doesn't work as it gives the original text value not the new one.
-
I need to use the label's new text value in the code behind for further processing. Using 'this.label1.text' doesn't work as it gives the original text value not the new one.
Oops sorry. Can you not simply use the code-behind to solve this issue? Because your clicking on a server control the page is reloaded back to its orginal state. If you want to change the text of the button after changing the label control using JavaScript then you will need to change the second button using JavaScript also.
-
Oops sorry. Can you not simply use the code-behind to solve this issue? Because your clicking on a server control the page is reloaded back to its orginal state. If you want to change the text of the button after changing the label control using JavaScript then you will need to change the second button using JavaScript also.
No. I am working on an application that utilizes a scheduling control. This control has a client-side click event that I must use. When I click on the control it must set a property wich in turn must be used when a button on the page is clicked. My solution to this was to set a label's text property but that doesn't seem to work, as you have seen in my code. Maybe you can help me out. Thanks anyways.
-
No. I am working on an application that utilizes a scheduling control. This control has a client-side click event that I must use. When I click on the control it must set a property wich in turn must be used when a button on the page is clicked. My solution to this was to set a label's text property but that doesn't seem to work, as you have seen in my code. Maybe you can help me out. Thanks anyways.
Only other way I can think of is to use a hidden field.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript">
function Hello() {
var value = "bye";document.getElementById("<%=Button2.ClientID %>").disabled = false; document.getElementById("<%=Label1.ClientID %>").innerText = value; document.getElementById("<%=hdnValue.ClientID %>").value = value; } </script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Button1" onclick="Hello()" /><br /><asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" /> <br /> <asp:Label runat="server" ID="Label1" Text="hello" /> <asp:HiddenField ID="hdnValue" runat="server" /> </div> </form>
</body>
</html>void Page_Init(object sender, EventArgs e)
{
Button2.Click += new EventHandler(Button2_Click);
}protected void Page_Load(object sender, EventArgs e)
{
}void Button2_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Text = hdnValue.Value;
} -
Only other way I can think of is to use a hidden field.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript">
function Hello() {
var value = "bye";document.getElementById("<%=Button2.ClientID %>").disabled = false; document.getElementById("<%=Label1.ClientID %>").innerText = value; document.getElementById("<%=hdnValue.ClientID %>").value = value; } </script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Button1" onclick="Hello()" /><br /><asp:Button ID="Button2" runat="server" Enabled="False" Text="Button2" /> <br /> <asp:Label runat="server" ID="Label1" Text="hello" /> <asp:HiddenField ID="hdnValue" runat="server" /> </div> </form>
</body>
</html>void Page_Init(object sender, EventArgs e)
{
Button2.Click += new EventHandler(Button2_Click);
}protected void Page_Load(object sender, EventArgs e)
{
}void Button2_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Text = hdnValue.Value;
}Thank you very much! It works!