change a web control in jscript

J

jthornby

Is there any way to manipulate the content of a web control in client side
jscript that can propogate to the server? Specifically, I'm trying to
increment or decrement the value in a text box when one of a set of check
boxes are checked and I don't want the overhead of going back to the server

Thanks
 
K

Ken Cox [Microsoft MVP]

You can do that nicely with a little Javascript which you output from your
codebehind. I've create a demo sample below that might get you started.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
sb.Append("<script>")
sb.Append("function AddChks()")
sb.Append("{")
sb.Append("var intChkTotal=0;")
sb.Append("if (document.forms[0].CheckBox1.checked==true)")
sb.Append("{")
sb.Append("intChkTotal = intChkTotal + 1")
sb.Append("}")
sb.Append("if (document.forms[0].CheckBox2.checked==true)")
sb.Append("{")
sb.Append(" intChkTotal = intChkTotal + 1")
sb.Append("}")
sb.Append("if (document.forms[0].CheckBox3.checked==true)")
sb.Append("{")
sb.Append(" intChkTotal = intChkTotal + 1")
sb.Append("}")
sb.Append("document.forms[0].TextBox1.value=intChkTotal;")
sb.Append("}")
sb.Append("</script>")
Page.RegisterClientScriptBlock("myscript", sb.ToString)
CheckBox1.Attributes.Add("onclick", "AddChks()")
CheckBox2.Attributes.Add("onclick", "AddChks()")
CheckBox3.Attributes.Add("onclick", "AddChks()")
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub
<form id="Form1" method="post" runat="server">
<p>
<asp:checkbox id="CheckBox1"
runat="server"></asp:checkbox></p>
<p>
<asp:checkbox id="CheckBox2"
runat="server"></asp:checkbox></p>
<p>
<asp:checkbox id="CheckBox3"
runat="server"></asp:checkbox></p>
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:button id="Button1" runat="server"
Text="Button"></asp:button></p>
<p>
<asp:label id="Label1" runat="server">Label</asp:label></p>
</form>
 

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

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top