How to populate an href with code behind text? - c#

I have a requirement to have a link on a webpage that users can click on a link that will open a Microsoft Outlook window that has the To, Subject, and Body fields pre-populated with information pertinent to the request.
The Subject and Body Fields are covered, but I need to populate the To field with data that is generated in my C#. The easiest way that I know of to do this, would be able to create an asp hiddenField, and populate it with the e-mail addresses that I need separated by semicolons. The problem is that I don't know how to get the actual text of the field to show up in my href. The code below describes it:
Email
If there is a better way to accomplish this than through an asp hiddenField, please let me know. I'm new to web design programming, so my knowledge is limited. Thanks!

I'd use the Hyperlink-control if i want to access it on serverside.
<asp:HyperLink id="hyperlinkEmail" Text="Email" runat="server"/>
and in codebehind:
string href = string.Format("mailto:{0}?Subject={1}&Body={2}"
emailAddress, subject, body);
this.hyperlinkEmail.NavigateUrl = href;

Related

Change text in HTML that I'm grabbing from a text file

What I'm doing is grabbing the HTML code for my header from a text file.
But once a User logs in I want it to say Welcome "Username" at the top, which is a dropdown to account settings, cart, etc...
So since I'm inserting the HTML into a DIV on page load, I don't actually have access to any of the elements inside in C#.
How would I go about doing this? Is there any way to access something like a (p id="name")'s inner text, after its loaded in from the text file?
Would like to do this with C# not JS please.
Edit: I have a work around for now, but I am still interested in better answers.
headerText = headerText.Replace("::Username::", Session["Username"] as string);
Here is my code for grabbing the HTML and pasting it in.
string headerText = File.ReadAllText(Server.MapPath("~/global/header.html"));
string footerText = File.ReadAllText(Server.MapPath("~/global/footer.html"));
headerText = headerText.Replace("::Username::", Session["Username"] as string);
divHeader.InnerHtml = headerText;
divFooter.InnerHtml = footerText;
To be more clear, is there anyway to access something like
<asp:Panel ID="panelAccount" runat="server">
which is stored in another HTML file.
I have done similar work in C# earlier, I use HTMLAgilityPack for doing this kind of works. Later I start using Anglesharp since it has a very good CSS selector based support.
Try anglesharp and you can modify the HTML tag like you do in jQuery.
As much as I liked Adrians response, I found out a better way to do what I needed to do, I could do with a MasterPage. I definitely be using Adrians answer for a load of other things though so it still holds valid.
Then in the C# file associated with the master page you create a property like this.
public Panel PanelAccount
{
get { return panelAccount; }
set { panelAccount = value; }
}
Then from the regular WebForm, you can call "PanelAccount" to access that property.
Here is a tutorial for how to do that.
Thanks for everyones downvotes with no inputs, you guys are stars!

how to remove tags in text while displaying on a web page

I am using TFS API and I try am trying to access a property "Description" of an object "WorkItem"
I want to display this property on a web page.
When I display this on the web page , this is what I see :
<p>This task is created for our SSRS team Sesame project.</p>
Firstly , I wanted to know if this is a html tag or does it mean something else in TFS .
And secondly , is there a way I can display this in plain text ?
This task is created for our SSRS team Sesame project.
Please let me know.
If you just want to remove tags, see this simple solution (it uses Regular Expressions):
Remove HTML tags in String
It could be even better if you create it as a extension method.
This is HTML as the System.Description field is an HTML one.
If you just write the field out to a tag it should render as text without the tags. The browser hosting the webpage will render them like any other HTML tags.

Can I add a Javascript function in C# to a gmail e-mail?

I am using C# and ASP.net to write an e-mail and send using via Gmail.
Basically, the e-mail will contain a list of checkboxes that the person can check. After the check boxes will be some wort of 'Submit' button or link that will use Javascript to check which check boxes have been checked. They will then be redirected to a new page.
So far, I can successfully create the check boxes doing this:
message += #"<input type=""checkbox"" name=""vehicle"" value="" + i + ">Text</input>";
Where i is part of a for loop so each input has it's own unique number value.
I have been trying to do this:
message += #"<a onclick=""myFunction();"" href=""http://www.mysite.com"">Submit</a> ";
However 'myFunction()' is never called. In fact the 'onclick' portion of this HTML doesn't show up in my e-mail when I 'inspect element'. What I get is this:
Submit
My goal here is to have a link that would be modified by a javascript function to make it do something like this:
<a href="http://www.mysite.com&CheckedItems=123" >Submit</a>
Where '123' could be parsed later on by 'mysite'.
For security reasons, email clients will strip all script and most CSS from emails before displaying them.
You can't do that.
This is unreliable. Just find a better way to get people to come to your site through the email to fill out the form.
Reference: Is JavaScript supported in an email message?

AjaxControlToolKit HTMLEditorExtender adding anchor tag as a normal text when a link is added

I am unable to write a nice title to this topic because my problem is a little weird. I am using AjaxControlToolkit HTMLEditorExtender in my website to send HTML formatted emails. Every other feature like bold, italic, underline etc. are working fine but when I add a link it shows the HTML code of it as follows:
As you can see BOLD is working but the anchor tag is appearing in HTML code format.
Code for extender and the textbox:
<asp:TextBox ID="TextBox2" runat="server" Height="376px"
TextMode="MultiLine" Width="795px"></asp:TextBox>
<asp2:HtmlEditorExtender ID="TextBox2_HtmlEditorExtender"
runat="server" Enabled="True" TargetControlID="TextBox2">
</asp2:HtmlEditorExtender>
Can any one please tell me why this is happening? Is this some bug with the extender?
Considering I do not have enough reputation for commenting on the post, I will ask a followup question here. Is there any way we could see the text you are getting on your C# backend? This is a possible source for the issue if the string has some weird formatting.
Plus email clients are not meant to be browsers and there is a possibility that the email client will not render the html correctly.
Is that image a screen shot of the editor itself? I created my own test project using your same code.
Also, how did you create the link? I typed some text highlighted the text and clicked on the 'create link' icon and from there I typed in the URL. It created the link as expected.
The only difference is that I didn't bother implementing a sanitizer, which it appears you did. I would try disabling the sanitizer (just for testing purposes) and see if that's where your problem lies.
Try this it should solve your issue-
txtEmialMsg.Text=Server.HtmlDecode(ActualStringFromExtender.ToString());
Or if you are getting (A href) text then you need to use following when sending emails
Server.UrlDecode(link)

asp.net formatting an email

I am working on an application that involves the user clicking on a hyperlink to popup an outlook email template. I have the SendTo values, and the subject values.
How do I enter text into the body of the email?
In the hyperlink you can add the ?body= variable after the email address. Formatting the body will be a challenge because for symbols like spaces you have to use hex codes (space = %20, newline = %0D). I don't know what your design criteria are, but most of the time I prefer to create a form with the desired input fields and let .NET handle the sending of the e-mail message on the submit.
Are you searching for this?
...

Categories

Resources