By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Javascript listbox item manipulation, and codebehind

P: n/a
Hello,

I have a page with:

* two side-by-side asp:listboxes and two arrow asp:buttons allowing
users to add/remove email addresses between them -- very much like
Outlook, where you have the address book on the left, and you build
the message recipients on the right.
* an asp:button that, when clicked, fires off some code to create and
send the mail message

The moving of entries from side to side is accomplished with some
fairly straightforward javascript code. It's all processed on the
client-- no postbacks.

THE PROBLEM IS, after I've set up the lists, when I click the button
and loop thru the recipients list box to get the list of recipients to
set to MailMessage.To, the loop doesn't recognize that the lists have
changed. It still thinks they're in the state they were when the page
loaded. How can I get the codebehind to recognize the changes that
have taken place on the client side? Can I do it without postbacks?
Thanks.

html:

<%@ Page language="c#" Codebehind="EmailDraft.aspx.cs"
AutoEventWireup="false" Inherits="xx.EmailDraft" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Email Response Draft</title>
<script language="javascript">
function MoveRecipients( lboFrom, lboTo )
{
for ( var i=0; i < lboFrom.options.length; i++ )
{
if ( ( lboFrom.options[i].selected == true ) )
{
strItemToAdd = lboFrom.options[i].text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options[i] = null;
i--;
}
}
}
</script>
</HEAD>
<body>
<form id="frmEmailDraft" method="post" runat="server">
<table width="600">
<tr>
<td class="appField">
Address List:<br>
<asp:ListBox id=lboEmailAddresses runat="server" Width="250px"
Height="96px" DataSource="<%# arrEmailAddresses %>"
CssClass="appTextBox" SelectionMode="Multiple">
</asp:ListBox>
</td>
<td>
<input type="button" id="btnMoveTo" value=">" class="buttonstyle"
onclick="MoveRecipients( frmEmailDraft.lboEmailAddresses,
frmEmailDraft.lboRecipients)"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON><br>
<input type="button" id="btnMoveFrom" value="<" class="buttonstyle"
onclick="MoveRecipients(frmEmailDraft.lboRecipient s,
frmEmailDraft.lboEmailAddresses )"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON>
</td>
<td class="appField">
Send To:<br>
<asp:ListBox id="lboRecipients" runat="server" Width="250px"
Height="96px" CssClass="appTextBox" SelectionMode="Multiple"
EnableViewState="False"></asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3" class="appLabel" align="right">
<asp:Button id="btnSendEmail" runat="server" Width="75px"
CssClass="buttonstyle" Text="Send E-mail"></asp:Button>&nbsp;
<input class="buttonstyle" id="btnCancel" style="WIDTH: 75px"
onclick="window.close();" type="button" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</HTML>
c# :

public class EmailDraft : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox lboEmailAddresses;
protected System.Web.UI.WebControls.ListBox lboRecipients;
protected System.Web.UI.WebControls.TextBox txtDraft;
protected System.Web.UI.WebControls.TextBox txtAnnotation;
protected System.Web.UI.WebControls.Button btnSendEmail;
protected string[] arrEmailAddresses = { "a@a.com",
"b@b.com","c@c.com","d@d.com" } ;

private void Page_Load(object sender, System.EventArgs e)
{
lboEmailAddresses.DataBind();
}

#region Web Form Designer generated code
...snip...
#endregion

private void btnSendEmail_Click(object sender, System.EventArgs e)
{
MailMessage mm = new MailMessage();
mm.From = "me@x.com";
for ( int iRcpts = 0; iRcpts < lboRecipients.Items.Count; iRcpts++)
{
mm.To += lboRecipients.Items[iRcpts].Text;
if (iRcpts != lboRecipients.Items.Count - 1) mm.To += ";";
}
mm.Subject = "something";
mm.Body = "something else";
SmtpMail.Send(mm);
}
}
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
a listbox only postbacks its value. your javascript nned to send back the
nw item list data also. the best way is in a hidden field. in c# on the
postback, read the hidden field, and load the list from it.

-- bruce (sqlwork.com)
"Bob P." <qu****@yahoo.com> wrote in message
news:fb**************************@posting.google.c om...
Hello,

I have a page with:

* two side-by-side asp:listboxes and two arrow asp:buttons allowing
users to add/remove email addresses between them -- very much like
Outlook, where you have the address book on the left, and you build
the message recipients on the right.
* an asp:button that, when clicked, fires off some code to create and
send the mail message

The moving of entries from side to side is accomplished with some
fairly straightforward javascript code. It's all processed on the
client-- no postbacks.

THE PROBLEM IS, after I've set up the lists, when I click the button
and loop thru the recipients list box to get the list of recipients to
set to MailMessage.To, the loop doesn't recognize that the lists have
changed. It still thinks they're in the state they were when the page
loaded. How can I get the codebehind to recognize the changes that
have taken place on the client side? Can I do it without postbacks?
Thanks.

html:

<%@ Page language="c#" Codebehind="EmailDraft.aspx.cs"
AutoEventWireup="false" Inherits="xx.EmailDraft" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Email Response Draft</title>
<script language="javascript">
function MoveRecipients( lboFrom, lboTo )
{
for ( var i=0; i < lboFrom.options.length; i++ )
{
if ( ( lboFrom.options[i].selected == true ) )
{
strItemToAdd = lboFrom.options[i].text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options[i] = null;
i--;
}
}
}
</script>
</HEAD>
<body>
<form id="frmEmailDraft" method="post" runat="server">
<table width="600">
<tr>
<td class="appField">
Address List:<br>
<asp:ListBox id=lboEmailAddresses runat="server" Width="250px"
Height="96px" DataSource="<%# arrEmailAddresses %>"
CssClass="appTextBox" SelectionMode="Multiple">
</asp:ListBox>
</td>
<td>
<input type="button" id="btnMoveTo" value=">" class="buttonstyle"
onclick="MoveRecipients( frmEmailDraft.lboEmailAddresses,
frmEmailDraft.lboRecipients)"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON><br>
<input type="button" id="btnMoveFrom" value="<" class="buttonstyle"
onclick="MoveRecipients(frmEmailDraft.lboRecipient s,
frmEmailDraft.lboEmailAddresses )"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON>
</td>
<td class="appField">
Send To:<br>
<asp:ListBox id="lboRecipients" runat="server" Width="250px"
Height="96px" CssClass="appTextBox" SelectionMode="Multiple"
EnableViewState="False"></asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3" class="appLabel" align="right">
<asp:Button id="btnSendEmail" runat="server" Width="75px"
CssClass="buttonstyle" Text="Send E-mail"></asp:Button>&nbsp;
<input class="buttonstyle" id="btnCancel" style="WIDTH: 75px"
onclick="window.close();" type="button" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</HTML>
c# :

public class EmailDraft : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox lboEmailAddresses;
protected System.Web.UI.WebControls.ListBox lboRecipients;
protected System.Web.UI.WebControls.TextBox txtDraft;
protected System.Web.UI.WebControls.TextBox txtAnnotation;
protected System.Web.UI.WebControls.Button btnSendEmail;
protected string[] arrEmailAddresses = { "a@a.com",
"b@b.com","c@c.com","d@d.com" } ;

private void Page_Load(object sender, System.EventArgs e)
{
lboEmailAddresses.DataBind();
}

#region Web Form Designer generated code
...snip...
#endregion

private void btnSendEmail_Click(object sender, System.EventArgs e)
{
MailMessage mm = new MailMessage();
mm.From = "me@x.com";
for ( int iRcpts = 0; iRcpts < lboRecipients.Items.Count; iRcpts++)
{
mm.To += lboRecipients.Items[iRcpts].Text;
if (iRcpts != lboRecipients.Items.Count - 1) mm.To += ";";
}
mm.Subject = "something";
mm.Body = "something else";
SmtpMail.Send(mm);
}
}

Nov 18 '05 #2

P: n/a
Hello Bob P.,

Look at this control... It does all this for you...

http://www.metabuilders.com/Tools/DualList.aspx

--
Matt Berther
http://www.mattberther.com
Hello,

I have a page with:

* two side-by-side asp:listboxes and two arrow asp:buttons allowing
users to add/remove email addresses between them -- very much like
Outlook, where you have the address book on the left, and you build
the message recipients on the right.
* an asp:button that, when clicked, fires off some code to create and
send the mail message
The moving of entries from side to side is accomplished with some
fairly straightforward javascript code. It's all processed on the
client-- no postbacks.

THE PROBLEM IS, after I've set up the lists, when I click the button
and loop thru the recipients list box to get the list of recipients to
set to MailMessage.To, the loop doesn't recognize that the lists have
changed. It still thinks they're in the state they were when the page
loaded. How can I get the codebehind to recognize the changes that
have taken place on the client side? Can I do it without postbacks?
Thanks.

html:

<%@ Page language="c#" Codebehind="EmailDraft.aspx.cs"
AutoEventWireup="false" Inherits="xx.EmailDraft" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Email Response Draft</title>
<script language="javascript">
function MoveRecipients( lboFrom, lboTo )
{
for ( var i=0; i < lboFrom.options.length; i++ )
{
if ( ( lboFrom.options[i].selected == true ) )
{
strItemToAdd = lboFrom.options[i].text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options[i] = null;
i--;
}
}
}
</script>
</HEAD>
<body>
<form id="frmEmailDraft" method="post" runat="server">
<table width="600">
<tr>
<td class="appField">
Address List:<br>
<asp:ListBox id=lboEmailAddresses runat="server" Width="250px"
Height="96px" DataSource="<%# arrEmailAddresses %>"
CssClass="appTextBox" SelectionMode="Multiple">
</asp:ListBox>
</td>
<td>
<input type="button" id="btnMoveTo" value=">" class="buttonstyle"
onclick="MoveRecipients( frmEmailDraft.lboEmailAddresses,
frmEmailDraft.lboRecipients)"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON><br>
<input type="button" id="btnMoveFrom" value="<" class="buttonstyle"
onclick="MoveRecipients(frmEmailDraft.lboRecipient s,
frmEmailDraft.lboEmailAddresses )"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON>
</td>
<td class="appField">
Send To:<br>
<asp:ListBox id="lboRecipients" runat="server" Width="250px"
Height="96px" CssClass="appTextBox" SelectionMode="Multiple"
EnableViewState="False"></asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3" class="appLabel" align="right">
<asp:Button id="btnSendEmail" runat="server" Width="75px"
CssClass="buttonstyle" Text="Send E-mail"></asp:Button>&nbsp;
<input class="buttonstyle" id="btnCancel" style="WIDTH: 75px"
onclick="window.close();" type="button" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</HTML>
c# :

public class EmailDraft : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox lboEmailAddresses;
protected System.Web.UI.WebControls.ListBox lboRecipients;
protected System.Web.UI.WebControls.TextBox txtDraft;
protected System.Web.UI.WebControls.TextBox txtAnnotation;
protected System.Web.UI.WebControls.Button btnSendEmail;
protected string[] arrEmailAddresses = { "a@a.com",
"b@b.com","c@c.com","d@d.com" } ;
private void Page_Load(object sender, System.EventArgs e)
{
lboEmailAddresses.DataBind();
}
#region Web Form Designer generated code
...snip...
#endregion
private void btnSendEmail_Click(object sender, System.EventArgs e)
{
MailMessage mm = new MailMessage();
mm.From = "me@x.com";
for ( int iRcpts = 0; iRcpts < lboRecipients.Items.Count; iRcpts++)
{
mm.To += lboRecipients.Items[iRcpts].Text;
if (iRcpts != lboRecipients.Items.Count - 1) mm.To += ";";
}
mm.Subject = "something";
mm.Body = "something else";
SmtpMail.Send(mm);
}
}


Nov 18 '05 #3

P: n/a
Hi

Did you select all in the listbox before posting... else it wont be sent

for(i=0;i<lbox.options.length;i++)
{
lbox.options[i].selected = true;
}
And use multiple

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )
2B||!2B=?
No matter where you go there you are
==============================
"Bob P." <qu****@yahoo.com> wrote in message
news:fb**************************@posting.google.c om...
Hello,

I have a page with:

* two side-by-side asp:listboxes and two arrow asp:buttons allowing
users to add/remove email addresses between them -- very much like
Outlook, where you have the address book on the left, and you build
the message recipients on the right.
* an asp:button that, when clicked, fires off some code to create and
send the mail message

The moving of entries from side to side is accomplished with some
fairly straightforward javascript code. It's all processed on the
client-- no postbacks.

THE PROBLEM IS, after I've set up the lists, when I click the button
and loop thru the recipients list box to get the list of recipients to
set to MailMessage.To, the loop doesn't recognize that the lists have
changed. It still thinks they're in the state they were when the page
loaded. How can I get the codebehind to recognize the changes that
have taken place on the client side? Can I do it without postbacks?
Thanks.

html:

<%@ Page language="c#" Codebehind="EmailDraft.aspx.cs"
AutoEventWireup="false" Inherits="xx.EmailDraft" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Email Response Draft</title>
<script language="javascript">
function MoveRecipients( lboFrom, lboTo )
{
for ( var i=0; i < lboFrom.options.length; i++ )
{
if ( ( lboFrom.options[i].selected == true ) )
{
strItemToAdd = lboFrom.options[i].text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options[i] = null;
i--;
}
}
}
</script>
</HEAD>
<body>
<form id="frmEmailDraft" method="post" runat="server">
<table width="600">
<tr>
<td class="appField">
Address List:<br>
<asp:ListBox id=lboEmailAddresses runat="server" Width="250px"
Height="96px" DataSource="<%# arrEmailAddresses %>"
CssClass="appTextBox" SelectionMode="Multiple">
</asp:ListBox>
</td>
<td>
<input type="button" id="btnMoveTo" value=">" class="buttonstyle"
onclick="MoveRecipients( frmEmailDraft.lboEmailAddresses,
frmEmailDraft.lboRecipients)"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON><br>
<input type="button" id="btnMoveFrom" value="<" class="buttonstyle"
onclick="MoveRecipients(frmEmailDraft.lboRecipient s,
frmEmailDraft.lboEmailAddresses )"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON>
</td>
<td class="appField">
Send To:<br>
<asp:ListBox id="lboRecipients" runat="server" Width="250px"
Height="96px" CssClass="appTextBox" SelectionMode="Multiple"
EnableViewState="False"></asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3" class="appLabel" align="right">
<asp:Button id="btnSendEmail" runat="server" Width="75px"
CssClass="buttonstyle" Text="Send E-mail"></asp:Button>&nbsp;
<input class="buttonstyle" id="btnCancel" style="WIDTH: 75px"
onclick="window.close();" type="button" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</HTML>
c# :

public class EmailDraft : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox lboEmailAddresses;
protected System.Web.UI.WebControls.ListBox lboRecipients;
protected System.Web.UI.WebControls.TextBox txtDraft;
protected System.Web.UI.WebControls.TextBox txtAnnotation;
protected System.Web.UI.WebControls.Button btnSendEmail;
protected string[] arrEmailAddresses = { "a@a.com",
"b@b.com","c@c.com","d@d.com" } ;

private void Page_Load(object sender, System.EventArgs e)
{
lboEmailAddresses.DataBind();
}

#region Web Form Designer generated code
...snip...
#endregion

private void btnSendEmail_Click(object sender, System.EventArgs e)
{
MailMessage mm = new MailMessage();
mm.From = "me@x.com";
for ( int iRcpts = 0; iRcpts < lboRecipients.Items.Count; iRcpts++)
{
mm.To += lboRecipients.Items[iRcpts].Text;
if (iRcpts != lboRecipients.Items.Count - 1) mm.To += ";";
}
mm.Subject = "something";
mm.Body = "something else";
SmtpMail.Send(mm);
}
}

Nov 18 '05 #4

P: n/a
Thanks for all your help. I tried having all the options selected
before posting but it didn't seem to do the trick.

I wound up populating a hidden field, using Javascript, by iterating
through the contents of the 2nd listbox. That worked nicely.

Thanks again.
"Vidar Petursson" <th**************************@icysoft.com> wrote in message news:<eJ**************@TK2MSFTNGP10.phx.gbl>...
Hi

Did you select all in the listbox before posting... else it wont be sent

for(i=0;i<lbox.options.length;i++)
{
lbox.options[i].selected = true;
}
And use multiple

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )
2B||!2B=?
No matter where you go there you are
==============================
"Bob P." <qu****@yahoo.com> wrote in message
news:fb**************************@posting.google.c om...
Hello,

I have a page with:

* two side-by-side asp:listboxes and two arrow asp:buttons allowing
users to add/remove email addresses between them -- very much like
Outlook, where you have the address book on the left, and you build
the message recipients on the right.
* an asp:button that, when clicked, fires off some code to create and
send the mail message

The moving of entries from side to side is accomplished with some
fairly straightforward javascript code. It's all processed on the
client-- no postbacks.

THE PROBLEM IS, after I've set up the lists, when I click the button
and loop thru the recipients list box to get the list of recipients to
set to MailMessage.To, the loop doesn't recognize that the lists have
changed. It still thinks they're in the state they were when the page
loaded. How can I get the codebehind to recognize the changes that
have taken place on the client side? Can I do it without postbacks?
Thanks.

html:

<%@ Page language="c#" Codebehind="EmailDraft.aspx.cs"
AutoEventWireup="false" Inherits="xx.EmailDraft" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Email Response Draft</title>
<script language="javascript">
function MoveRecipients( lboFrom, lboTo )
{
for ( var i=0; i < lboFrom.options.length; i++ )
{
if ( ( lboFrom.options[i].selected == true ) )
{
strItemToAdd = lboFrom.options[i].text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options[i] = null;
i--;
}
}
}
</script>
</HEAD>
<body>
<form id="frmEmailDraft" method="post" runat="server">
<table width="600">
<tr>
<td class="appField">
Address List:<br>
<asp:ListBox id=lboEmailAddresses runat="server" Width="250px"
Height="96px" DataSource="<%# arrEmailAddresses %>"
CssClass="appTextBox" SelectionMode="Multiple">
</asp:ListBox>
</td>
<td>
<input type="button" id="btnMoveTo" value=">" class="buttonstyle"
onclick="MoveRecipients( frmEmailDraft.lboEmailAddresses,
frmEmailDraft.lboRecipients)"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON><br>
<input type="button" id="btnMoveFrom" value="<" class="buttonstyle"
onclick="MoveRecipients(frmEmailDraft.lboRecipient s,
frmEmailDraft.lboEmailAddresses )"
style="WIDTH: 72px; HEIGHT: 19px" size="20"></BUTTON>
</td>
<td class="appField">
Send To:<br>
<asp:ListBox id="lboRecipients" runat="server" Width="250px"
Height="96px" CssClass="appTextBox" SelectionMode="Multiple"
EnableViewState="False"></asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3" class="appLabel" align="right">
<asp:Button id="btnSendEmail" runat="server" Width="75px"
CssClass="buttonstyle" Text="Send E-mail"></asp:Button>&nbsp;
<input class="buttonstyle" id="btnCancel" style="WIDTH: 75px"
onclick="window.close();" type="button" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</HTML>
c# :

public class EmailDraft : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox lboEmailAddresses;
protected System.Web.UI.WebControls.ListBox lboRecipients;
protected System.Web.UI.WebControls.TextBox txtDraft;
protected System.Web.UI.WebControls.TextBox txtAnnotation;
protected System.Web.UI.WebControls.Button btnSendEmail;
protected string[] arrEmailAddresses = { "a@a.com",
"b@b.com","c@c.com","d@d.com" } ;

private void Page_Load(object sender, System.EventArgs e)
{
lboEmailAddresses.DataBind();
}

#region Web Form Designer generated code
...snip...
#endregion

private void btnSendEmail_Click(object sender, System.EventArgs e)
{
MailMessage mm = new MailMessage();
mm.From = "me@x.com";
for ( int iRcpts = 0; iRcpts < lboRecipients.Items.Count; iRcpts++)
{
mm.To += lboRecipients.Items[iRcpts].Text;
if (iRcpts != lboRecipients.Items.Count - 1) mm.To += ";";
}
mm.Subject = "something";
mm.Body = "something else";
SmtpMail.Send(mm);
}
}

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.