B
Bob P.
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.selected == true ) )
{
strItemToAdd = lboFrom.options.text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options = 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.lboRecipients,
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>
<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 = { "(e-mail address removed)",
"(e-mail address removed)","(e-mail address removed)","(e-mail address removed)" } ;
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 = "(e-mail address removed)";
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);
}
}
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.selected == true ) )
{
strItemToAdd = lboFrom.options.text;
lboTo.options[lboTo.length] = new Option(strItemToAdd);
lboFrom.options = 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.lboRecipients,
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>
<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 = { "(e-mail address removed)",
"(e-mail address removed)","(e-mail address removed)","(e-mail address removed)" } ;
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 = "(e-mail address removed)";
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);
}
}