How to launching email client on LinkButton click event? - c#

How can I launch an Outlook email window (similar to what mailto: does in a hyperlink) ?
This needs to be done in a LinkButton click event.

Consider that the mailto functionality is a function that needs to happen client side. You are going to need javascript to do it. Depending on when you want the mailto to happen you have two choices.
If you want it to happen as soon as the LinkButton is clicked then just add to the LinkButton's OnClientClick event:
<asp:LinkButton runat="server" ID="btnEmail" Text="Send Email"
OnClientClick="window.open('mailto:someone#somewhere.com','email');">
</asp:LinkButton>
If you want it to happen AFTER the server side code has run your are going to have wire up the javascript event to run when the new page starts up:
// At the end of your LinkButton server side OnClick event add the following code:
ClientScript.RegisterStartupScript(this.GetType(), "FormLoading",
"window.open('mailto:someone#somewhere.com','email');", true);
Hope that helps.

I've accomplished this using the OnClientClick event of the LinkButton.
You can use:
<asp:LinkButton runat="server" ID="btnEmail" Text="Send Email"
OnClientClick="window.location.href = 'mailto:someone#something.com?subject=Email Subject';">
</asp:LinkButton>
You can also do this in code, in case you need to load an email address from a database or something:
btnEmail.OnClientClick = "window.location.href = 'mailto:someone#something.com?subject=Email Subject';";

Related

How to prevent page reload on asp.net LinkButton OnClick function

I'm using the <asp:LinkButton />’s OnClick function on the server side to fetch data. I'm using OnClientClick to open a popup and populate data. But the page refreshes when I click the button. Please see the attached code and help me to fix the issue.
<asp:LinkButton ID="lnkEdit_Bill" runat="server" CssClass="lnkAddressButton" OnClientClick="javascript:ShowDialog_Both_New('Invoice','edit');" OnClick="lnkEdit_Bill_Click_new" >Edit</asp:LinkButton>
Google event.preventDefault(), I believe i've used that previously to prevent a postback. Also remove the OnClick, if that's not what you want, and just use OnClientClick.
Bro, if you don't want the postback, just fire the javascript. and you definitely don't need the server control. which you can present your code as below:
Edit
Let's says if you still insist want to user ASP.NET server control of LinkButton, then you can do something like this:
<asp:LinkButton ID="lnkEdit_Bill" runat="server" CssClass="lnkAddressButton"
OnClientClick="ShowDialog_Both_New('Invoice','edit'); return false;" >
Edit
</asp:LinkButton>

How would one disable an ASP.NET WebForms button when its clicked, and have it be enabled again after postback completes?

What I would like to do is be able to disable a button as soon as it pressed, perform an operation, and re-enable the button. Seemed simple enough, but my button doesn't update when I need it to. I tried an update panel, but I'm not sure if I'm using it correctly. Am I doing something wrong? Or is there a better way to achieve this?
HTML
<asp:UpdatePanel runat="server" ID="updateSubmitButton">
<ContentTemplate>
<asp:Button ID="btnSearch" CssClass="CrossoverButton" runat="server" Text="Search" OnClick="btnSearch_Click" />
</ContentTemplate>
</asp:UpdatePanel>
C#
protected void btnSearch_Click(object sender, EventArgs e)
{
btnSearch.Enabled = false;
updateContent();
// Do a lengthy operation here.
btnSearch.Enabled = true;
}
You will need to use a client side technology to disable the button. This is because when you click the button, the page does a post back to itself, which tells the server to execute btnSearch_Click. Based on your code this will
Disable the button
Update the content
Re-enable the button
all of this happens on the server. Only after the method has finished executing, does it return context to the page.
In order to disable the button on click, you'll want to try something this:
<asp:Button ID="btnSearch" CssClass="CrossoverButton" runat="server" Text="Search"
OnClientClick="this.disabled = true;"
UseSubmitBehavior="false" />
OnClientClick="this.disabled = true;"
This line registers the client click handler, to disable itself.
UseSubmitBehavior="false"
This line tells ASP.NET to include the disabled element in the form post, so the server knows which control registered the postback.
See also -- https://stackoverflow.com/a/11832053/86860
All the actions in btnSearch_Click will be performed on server. The page will not be rendered until full page lifecycle ends.
If you want to disable the button you should use javascript to do this, and then only after disabling the button make a postback to the page.

server tag not well formed error

I know there are a lot of threads about this error, but I've looked through all of them and haven't been able to make it work. It's probably something very small that I am overlooking.
This is the code I have now, in an asp:Button component
<asp:Button ID="btn_profile" runat="server" Text="Bekijk de wenslijst"
CssClass="wenslijst_preview_btn"
OnClick="btn_goToChild_Click(<%# Eval("pk_child_id") %>)"/>
I have tried multiple things, for example the following:
OnClick='btn_goToChild_Click(<%# Eval("pk_child_id") %>)'
But then I get the following error:
preprocessor directives must appear as the first non-whitespace character on a line
If you need to see more code, let me know!
OnClick is a server-side event, so should just be the name of a server-side event handler. OnClientClick (where available) is a client-side event handler, so would take a JavaScript expression to evaluate.
Try
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick='btn_goToChild_Click(<%# Eval("pk_child_id") %>)' />
<asp:Button ID="btn_profile" runat="server" Text="Bekijk de wenslijst"
CssClass="wenslijst_preview_btn" CommandArgument='<%# Eval("pk_child_id") %>'
OnClick="btn_goToChild_Click"/>
protected void btn_goToChild_Click(object sender, EventArgs e)
{
Button btn= (Button) sender;
if (btn==null) return;
var id =btn.CommandArgument;
// Or int id=(int)btn.CommandArgument;
}
If you want to use Javascript expression, use OnClientClick not OnClick. These are different side events.
OnClick is used for server side methods only, for client side calls you should use "OnClientClick" attribute.

JavaScript in button onclick not working

I have a webpage (ASP.NET C#) that has a button:
<asp:Button ID="btnHide" runat="server" OnClick="HidePanel" Text="Hide"/>
and I'm doing a JavaScript alert like:
public void HidePanel(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Hello","<script type=text/javascript> alert('Hello') </script>");
}
If I modify the function to not have object sender and EventArgs e then I can call it from Page_Load, and it works fine, I get the alert.
As written above I expect to see the alert when I click the button, but it's not happening. I'm sure it's something obvious, but I don't see it.
Use OnClientClick instead of OnClick. And add a return false to avoid a postback on the page.
<asp:Button ID="btnHide" runat="server" OnClientClick="alert('Hello'); return false;" Text="Hide"/>
You could try using,
ClientScript.RegisterClientScriptBlock(GetType(), "nameOfScript", scriptString);
I'ved used that before in a click event.
edit:
I can't edit posts yet, but your code works fine for me as well.
And depending on the situation he might want to do some server side stuff as well on the click event, or build up the script dynamically.
Edit:
If you already registered a script with the same name, it won't run, eg.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> var i = 0;</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> alert('other hello') </script>");
You can remove the code to register the JavaScript code and instead do this:
<asp:Button ID="btnHide" runat="server"
OnClick="HidePanel" Text="Hide" OnClientClick="alert('Hello');"
UseSubmitBehavior="false" />
This:
UseSubmitBehavior="false"
will cause the button to fire the client-side script, and it will run the server-side code (post-back).

Click Event of Hyperlink

How to find Whether a hyperlink is clicked or not in ASP.net C# in runtime?
I want to write code on like that
Response.Redirect("Default.aspx");
If you want to execute server code upon a click in a link, then you should use the ASP.NET control <asp:LinkButton>
This is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.
You would attach either the event in the code behind, or in the ASPX / ASCX of your link in question like so:
<asp:LinkButton ID="linkGoSomewhere" runat="server" Click="linkGoSomewhere_Click" />
OR
linkGoSomewhere.Click += (linkGoSomewhere_Click);
With an event handler looking like so in your code:
public void linkGoSomewhere_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
HOWEVER
In this situation, you don't need a server side control to just send the user somewhere else. You just need a simple hyperlink:
Go somewhere else
if this HyperLink you can do it using javascript but if it is LinkButton you can do it inside onclick event
<asp:LinkButton ID="MyLnkButton" runat="server" onClick="MyLnkButton_Click" Text="Click Me!">
protected void MyLnkButton_Click(Object sender,EventArgs e)
{
Response.Redirect("Default.aspx");
}
The onclick server side handler can be added to achieve this.
<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>
You can determine this with the Click event of the LinkButton

Categories