In an asp.net forms website I have a button which has a postpack url:
mailBUtton.PostBackUrl = "mailto:..."
As expected a click on the button opens the local e-mail software, but If an other button on the page is clicked after the mailButton was clicked, the onClick event of the second button is ignored and instead of the onClick event, the PostBackUrl of the mailButton is called.
What is the reason for these behavior?
This is the mailButton:
<asp:Button ID="mailButton" runat="server" Text="Neuer Beitrag" ></asp:Button>
and this the second button:
<asp:Button ID="btnSpeichern" runat="server" Text="Speichern" OnClick="btnSpeichern_Click"/>
and this the event in the code behind:
protected void btnSpeichern_Click(object sender, EventArgs e)
{
speichern(false);
}
According to w3schools the PostBackUrl of a button is
A string specifying the URL of the page to post to. Default is an empty string, this causes the page to post back to itself
So when you click the mailButton you're setting the pages PostBackUrl to mailto:.... By clicking on the second button a postback is performed which now calls the mailto: url instead of the original one, where also is no btnSpeichern_Click method.
Instead of using a button and PostBackUrl you could do the following:
<asp:HyperLink runat="server" NavigateUrl="mailto:abc#abc.com" Text="abc#abc.com"></asp:HyperLink>
If you want your link to appear like a button, you can do that in your css.
Here are some more approaches of using mailto.
Related
In my ASP.Net Web Form App, I have a button, and that button is suppose to launch an ajaxToolKit: ModalPopupExtender modal.
<asp:Button ID="uxTicketHistoryButton" runat="server" Text="Show Ticket History" style="color: blue;" OnClick="uxTicketHistoryButton_Click"/>
<ajaxToolkit:ModalPopupExtender ID="uxTicketHistoryModal" runat="server" PopupControlID="Panel1" TargetControlID="uxTicketHistoryButton"CancelControlID="btnClose" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" style = "display:none">
....
</asp:Panel ID>
I had asked a previous question on how to insert C# variable values into the modal, but it seems my problem there is that the modal seems to be interfering with my button click event function; essentially keeping it from firing. The function is suppose to get the data I need to plug into the modal once the button is clicked and the modal launched.
protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
{
DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
string rName = ticketHist.Rows[0]["RequestorName"].ToString();
string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
....
}
I have noticed several odd things while debugging: 1) I put break points on the string variables (to see what the values were being returned) while debugging. After clicking the button, it goes right over the breakpoints (I assume that means its not "firing" that code.2) I can remove all of the code from inside the _Click function and the modal still launches when I click the button 3) If I comment out the modal code and click the button, the _Click event code fires fine and I can see the values for the string variables. So, I am assuming that the way I have the modal set (I suspect its related to using TargetControlID="uxTicketHistoryButton") is the problem. How do I get around my modal interfering with button click event? What am I doing wrong here?
Looks like you found one of those ajaxToolkit quirks. Try this:
Create a hidden button:
<asp:button id="hiddenButton" runat="server" style="display:none;" />
Change your Extender to target said button:
TargetControlID="hiddenButton"
Then in your _Click event, show the modal:
protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
{
DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
string rName = ticketHist.Rows[0]["RequestorName"].ToString();
string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
....
uxTicketHistoryModal.Show();
}
how stop postback on any button click. My page is reloading as soon as i click on reset button on the registration page, i want to reset the form without reloading the page itself.
You have two possibilities:
Simply set the attribute AutoPostBack="false" on your button or whatever control.
As an alternative you could also add the following javascript to the click event of the button :
onclick="return false"
This prevents the button from submitting.
Try following:
<asp:button runat="server".... OnClientClick="return false;" />
First you have to know about Sever Control and normal HTML control.
If you used Server Button Control then your Page reload on each click.
If you wan to stop it then you have to use AutoPostBack="false", using this your server side method calling is stop.
Otherwise use Normal HTML Button Control and use JavaScript to reset your form.
I am working on a project for school, and this is an extra credit part. I have a project started in VS 2010 using master pages, and what I'm trying to do is get a "Submit" button to redirect people to the "MyAccounts.aspx" page. My current code for the ASP part for the button looks like this:
<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>
I have tried adding in the OnClick option, as well as the OnClientClick option. I have also added this code to the Site.Master.cs file as well as the Transfer.aspx.cs file:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect(Page.ResolveClientUrl("/MyAccounts.aspx"));
}
When I run this and view the project in my browser, the whole thing runs fine, but when I click on the "Submit" button, it just refreshes the current page and does not properly redirect to the MyAccounts page. Anyone have any ideas for me?
You are doing it almost correctly, you just haven't put the correct pieces together. On Transfer.aspx, your button should be:
<asp:Button ID="btnTransfer" OnClick="btnTransfer_Click" runat="server" Text="Submit"/>
and your code behind should be like what #KendrickLamar said:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect("~/MyAccounts.aspx");
}
The OnClick event tells it what to execute on post-back when the users clicks the button. This is in the code-behind for Transfer.aspx, not the site master.
Every time I test the IsPostBack in PageLoad() false is returned whether or not post data is present. My first reaction was to check to see if the runat="server" tag was missing from the form or submit button. However, they were all added and the WriteEmail.aspx page still always returns false for IsPostBack. I have also tried using IsCrossPagePostBack in place of IsPostBack.
ListInstructors.aspx:
<form runat="server" method="post" action="WriteEmail.aspx">
...
<input type="submit" id="writeEmail" value="Write Email" runat="server" />
</form>
WriteEmail.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Redirect("ListInstructors.aspx");
}
}
Post != Postback. A postback is when you post back to the same page. The action on your form is posting to a new page.
It looks like all you're doing is using the WriteEmail.aspx page to send a message and then going back to where you just were. You're not even displaying a form to collect the text there. It's a very... Classic ASP-ish... way to handle things.
Instead, put the code you use to send a message in class separate class and if needed put the class in the App_Code folder. Also change the submit button to an <asp:button ... /> Then you can just call it the code from the server's Click event for your button and never leave your ListInstructors.aspx page.
In response to your comment: No. From MSDN:
... make a cross-page request by assigning a page URL to the PostBackUrl property of a button control that implements the IButtonControl interface.
The IsPostBack is not true because the form is not being submitted from the WriteEmail.aspx page; submitting a form from the same page is what causes a PostBack. If you submitted the form from the WriteEmail.aspx page, it would be a PostBack; as it is, it's just a Post.
You might find this MSDN reference to be useful:
http://msdn.microsoft.com/en-us/library/ms178141.aspx
This seems to be a common problem but I cannot find a solution.
I type in my username and password which are in a login control I have created.
I then press enter once I've typed in my password and the page just refreshes. It triggers the page load event but not the button on click event.
If I press the submit button then everything works fine.
using your forms default button is correct, but you need to supply it the correct id as it will be rendered to HTML.
so you do as Jon said above:
<form runat="server" DefaultButton="SubmitButton">
But ensure you use the Button name that will be rendered.
You can achieve this my making the Button public in your control, or a method that will return it's ClientId.
Let's say your button is called btnSubmit, and your implementation of your control ucLogin.
Give your form an id
<form runat="server" id="form1">
Then in your page load in your code behind of your page, set the DefaultButton by handing it your button client id.
protected void Page_Load(object sender, EventArgs e)
{
form1.DefaultButton = ucLogin.btnSubmit.ClientID;
}
If you're using ASP.NET 2.0 or higher, you can set a default button attribute for your page's Form:
<form runat="server" DefaultButton="SubmitButton">
Pressing ENTER on a text input executes Form.onsubmit, not Button.onclick.
I suppose this was inspired by the fact that you can have a form without an actual submit button (depending solely on the use of ENTER).