Handling client side button click events to interact with other controls

M

Matt Adamson

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;

}

}
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top