1 Attachment(s)
[2005] Sending e-mail from a web page
I would like to allow the user to check off "who" to send e-mails to (see attached image) and then open their mail with all those usernames in the To: line. I'm looking to allow them to check more then 1 check box - having all the names appear.
How can I do that.
Re: [2005] Sending e-mail from a web page
When you say "and then open their mail", do you mean their email client?
If yes, then it's a liiiitle bit complicated.
So the user checks a bunch of checkboxes, clicks on a button and you want this to happen. In the button's click event, go through each row where the checkbox is checked. Find the corresponding email address. Then, concatenate a string and your eventual aim is to do this
Alternatively, in the buttons click event (client side), you can get some javascript to do the same thing as above, except it'd be
Re: [2005] Sending e-mail from a web page
Yes - their e-mail client (I thought you told someone today that you could read minds...)
Should I be avoiding javascript?
Isn't javascript what's behind (or should I say in front of :) ) making this asp.net stuff run anyway...
If I did it in js I would need to know how to concatenate the names and such - right?
Which way would you lean?
Re: [2005] Sending e-mail from a web page
Hmm... do you want to avoid javascript? Because I know this is an 'internal' application for your company, you can take certain liberties such as assuming the user has javascript turned on, uses IE/Firefox, compatible browsers, all that business.
Yes, javascript is what enables postbacks but I believe that if you disable javascript, the browser attempts to 'gracefully' degrade to use form submissions with extra information.
The codebehind way, in which you loop through your repeater, get all the email addresses, concatenate it and do the response.redirect, would actually be easier.
If you did it in javascript, you will need to have a hidden field in each row of your repeater that contains the corresponding email address. When the checkbox is checked, you can have each checkbox manipulate a global javascript variable (array), adding or removing its value from that array. The array is obviously important, since you will use that array - loop through it, read each element, concatenate the string.
So, what do you want to do?:D
Re: [2005] Sending e-mail from a web page
I like the vb.net backend coding - that's easier for me.
Done!
Re: [2005] Sending e-mail from a web page
Because you're doing it the codebehind way, I'm currently working on a sample to do it the other way - javascript. :D
Re: [2005] Sending e-mail from a web page
Ready. In my ASPX, I first make the javascript.
Code:
<script type="text/javascript">
Array.prototype.remove=function(str)
{
for(i=0;i<this.length;i++)
{
if(this[i]==str)
{
this.splice(i, 1);
}
}
}
var EmailAddresses = new Array();
function AddRemoveEmailAddress(chk, emailaddress)
{
if(chk.checked == true)
{
EmailAddresses.push(emailaddress);
}
else
{
EmailAddresses.remove(emailaddress);
}
}
function SendEmails()
{
var emailString = "";
for(i=0;i<EmailAddresses.length;i++)
{
emailString += EmailAddresses[i] + ";";
}
document.location.href='mailto:' + emailString;
}
</script>
Then, setup the gridview and button.
Code:
<div>
<asp:GridView ID="Students" Runat="server" OnRowDataBound="Students_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSendMail" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<input type="button" onclick="javascript:SendEmails();" value="Send" id="EmailButton" />
</div>
Then in the codebehind, I first setup a dummy datatable.
vb Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New Data.DataTable()
dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Columns.Add("col3")
Dim dr As Data.DataRow = dt.NewRow()
dr.Item("col1") = "111"
dr.Item("col2") = "222"
Dim dr2 As Data.DataRow = dt.NewRow()
dr2.Item("col1") = "333"
dr2.Item("col2") = "444"
Dim dr3 As Data.DataRow = dt.NewRow()
dr3.Item("col1") = "555"
dr3.Item("col2") = "666"
dt.Rows.Add(dr)
dt.Rows.Add(dr2)
dt.Rows.Add(dr3)
Students.DataSource = dt
Students.DataBind()
End Sub
And the main bit, the RowDataBound event
vb Code:
Protected Sub Students_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Students.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chk As New CheckBox
chk = e.Row.FindControl("chkSendMail")
chk.Attributes.Add("onClick", "AddRemoveEmailAddress(this, '" & CType(e.Row.DataItem, Data.DataRowView).Item("col3").ToString() & "');")
End If
End Sub
Ta-daaaa
Re: [2005] Sending e-mail from a web page
Should this have gone in the codebank? :blush:
Re: [2005] Sending e-mail from a web page
Let's do a joint submission - when I finish up my codebehind logic I'll post that here as well.
Re: [2005] Sending e-mail from a web page
Re: [2005] Sending e-mail from a web page
We are finally getting around to working on this...
How come this code
Code:
Dim mailto As String = "mailto:"
For Each n As String In l
mailto &= n & ";"
Next
Response.Redirect(mailto)
Opens a webpage with the "mailto" list.
Any way to make it stay on the original page?
Re: [2005] Sending e-mail from a web page
A Response.Redirect isn't going to work. The redirect process starts on the server side and thus, you'll end up on a page with just the mailto stuff in it.
The only way to make the client mail application open is via a client side event on the page (a click of a link, if you will).
I don't think there is anyway to do this entirely on the server side. Take the frog's code, put your name on it and use that :)
Edit: Unless I'm missing something and there is a header you can add to your redirect that could make this work...
Edit 2: I wonder if it's possible to modify the headers when the response is being sent with something like this:
Found this info here: http://www.w3.org/Protocols/HTTP/Object_Headers.html
Edit 3: Another option is to register some JavaScript to run onLoad and have it automatically open Url with mailto in the address.
So in your OnInit method, register a JavaScript that you modify to include the appropriate Url. When the page returns on that postback, Bam! E-mail pops open.
Re: [2005] Sending e-mail from a web page
A webpage with the mailto list? :confused:
Perhaps mailto hasn't been registered for you, although it's been years since I've done this I thought mailto would open up the email client on the user's desktop.
Re: [2005] Sending e-mail from a web page
Re: Edit3: He means ClientScript.RegisterStartupScript or ClientScript.RegisterClientScriptBlock.
1 Attachment(s)
Re: [2005] Sending e-mail from a web page
Just to clarify - it's doing what KAS indicated.
See the attached image.
We click on the Email Selected spot and it runs re-direct.
The re-direct sends the browser to an empty page with the MAILTO: in the address bar.
The CLIENT E-MAIL app does open - that's working great.
But the browser has to be back-buttoned to the page we were just on.
We tried make the EMAIL SELECTED be a MAILTO: link - that grows as each person to e-mail is clicked. But the page posts back and there is a noticeable flash - that stinks.
Re: [2005] Sending e-mail from a web page
Tools > Options > Programs : Is there a default email client set?
Perhaps you should consider the Javascript solution then, since you're relying on client-side behavior in either scenario.