How to use server control in JavaScript
Hello everybody,
I need to use my server controls in javascript for client-side validation etc.
I have a listbox control.
<asp:listbox id=groupsListBox runat="server" Width="256px" OnSelectedIndexChanged="javascript:SetTextBox();"></asp:listbox>
As user selects a value, I need to display this value in the TextBox just below the listbox. I wrote the code in SetTextBox() but this function does not execute.
Thanks.
Re: How to use server control in JavaScript
Well i've never used OnSelectedIndexChanged before, but what happends if you use the onChange event? This is the html of an example I made that works:
VB Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<script type="text/javascript">
function change(e)
{
var yak = document.getElementById('TextBox3');
yak.value = e;
}
</script>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" style="Z-INDEX: 103; LEFT: 232px; POSITION: absolute; TOP: 72px" runat="server" onChange="change(this.value);"></asp:ListBox>
<asp:TextBox id="TextBox3" style="Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 208px"
runat="server"></asp:TextBox>
</form>
</body>
</HTML>
Good Luck;
Re: How to use server control in JavaScript
Thanks. It resolved my problem. I have two more questions.
My listbox is databound, and I have used DataTextField and DataValueField properties. When I get the value of ListBox in JavaScript function, it gives me DataValueField. I need to get the SelectedText of the listbox control.
Code:
thisList.DataSource = dtTemporary; // Bind with DataTable
thisList.DataTextField = "Name";
thisList.DataValueField = "ID";
thisList.DataBind();
Secondly, I have an ImageButton control for Delete and I have written code for Delete in aspx.cs file in the ImageButton Click event. Now I need to write some client-side validation code on the click of the button. When I call the function on click of ImageButton, application gives exception.
Code:
<asp:ImageButton id="deleteImageBox" runat="server" ImageUrl="images\btn_delete_off.gif" BorderStyle="None" OnClick='javascript:btnDelete_Click();'></asp:ImageButton>