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;
}
}
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;
}
}