Groups | Blog | Home
all groups > asp.net webcontrols > march 2005 >

asp.net webcontrols : change a web control in jscript


jthornby
3/31/2005 11:35:04 AM
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

Ken Cox [Microsoft MVP]
4/1/2005 9:44:40 AM
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>

[quoted text, click to view]
AddThis Social Bookmark Button