Handling client side button click events to interact with other controls

Discussion in 'ASP .Net Web Controls' started by Matt Adamson, Feb 25, 2007.

  1. Matt Adamson

    Matt Adamson Guest

    Guys,



    Having issues with the code below which I would like the button click
    handler on the client side to simply add the text box value to the list box.
    When I press the add button the list box seems to get wider for a split
    second but nothing is added. Any ideas?



    ASPX



    <%@ Control Language="C#" AutoEventWireup="true"
    CodeFile="TransferTextToList.ascx.cs"
    Inherits="Controls_TransferTextBoxToListBoxControl" %>

    <asp:Label ID="textBoxLabel" runat="server" Text="Label"></asp:Label>

    <asp:TextBox ID="sourceTextBox" runat="server"></asp:TextBox>

    <asp:Button ID="addButton" runat="server" CausesValidation="False"
    Text="Add" UseSubmitBehavior="False" />

    <asp:ListBox ID="targetListBox" runat="server"></asp:ListBox>

    <asp:Button ID="removeButton" runat="server" Text="Remove" />

    and code behind

    if
    (!Page.ClientScript.IsClientScriptIncludeRegistered("TransferTextToList"))

    {

    Page.ClientScript.RegisterClientScriptInclude( "TransferTextToList",

    ResolveClientUrl("~/Controls/TransferTextToList.js"));

    }

    if ( ! IsPostBack)

    {

    addButton.OnClientClick = String.Format( "MoveItem('{0}', '{1}');",

    sourceTextBox.ClientID,

    targetListBox.ClientID);

    }



    TransferTextToList

    function MoveItem(sourceTextBoxId, targetListBoxId)

    {

    var sourceElement = document.getElementById(sourceTextBoxId).value;

    var targetElement = document.getElementById(targetListBoxId);

    if (sourceElement != null)

    {

    // Create a new instance of ListItem

    var newTargetOption = new Option();

    newTargetOption.text = sourceElement;

    newTargetOption.value = sourceElement;


    // Append the item into the target list box

    targetElement.options[targetElement.length] = newTargetOption;

    }

    }
     
    Matt Adamson, Feb 25, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mong
    Replies:
    5
    Views:
    4,728
    Kevin Spencer
    May 7, 2004
  2. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    Replies:
    2
    Views:
    6,499
    rick74
    May 27, 2008
  3. Aymer
    Replies:
    1
    Views:
    391
    Alvin Bruney
    Sep 19, 2003
  4. Replies:
    0
    Views:
    194
  5. Replies:
    1
    Views:
    133
Loading...

Share This Page