Results 1 to 3 of 3

Thread: How to use server control in JavaScript

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    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.

  2. #2
    Member
    Join Date
    Mar 2005
    Posts
    56

    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:
    1. <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1"%>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    3. <HTML>
    4.     <HEAD>
    5.         <title>WebForm1</title>
    6.         <script type="text/javascript">
    7.         function change(e)
    8.         {
    9.             var yak = document.getElementById('TextBox3');
    10.             yak.value = e;
    11.         }
    12.         </script>
    13.         <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    14.         <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    15.         <meta name="vs_defaultClientScript" content="JavaScript">
    16.         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    17.     </HEAD>
    18.     <body MS_POSITIONING="GridLayout">
    19.         <form id="Form1" method="post" runat="server">
    20.             <asp:ListBox id="ListBox1" style="Z-INDEX: 103; LEFT: 232px; POSITION: absolute; TOP: 72px" runat="server" onChange="change(this.value);"></asp:ListBox>
    21.             <asp:TextBox id="TextBox3" style="Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 208px"
    22.                 runat="server"></asp:TextBox>
    23.         </form>
    24.     </body>
    25. </HTML>

    Good Luck;

  3. #3

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    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>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width