-1

I have multiple asp:buttons in my application that reload the page whenever they are clicked. How do I resolve this behavior while still allowing the function of the button to work? Besides looking weird when the page reloads, I have a tab style navigation section that defaults back to the first tab every time a button is clicked. Here's an example of one button and the C# code that goes with it.

HTML/ASPX

<asp:Button ID="uxRequestFEMAResponseBtnT3" runat="server" Text="Generate Interim Response Email" OnClick="uxRequestFEMAResponseBtnT3_Click" />
&nbsp;<asp:TextBox ID="uxFEMARequestDateTxtBox" runat="server" style="color: red; text-align:center;"></asp:TextBox>

C#

protected void uxRequestFEMAResponseBtnT3_Click(object sender, EventArgs e)
    {
        String today = DateTime.Now.ToString("dd/MM/yyyy");
        uxFEMARequestDateTxtBox.Text = today;
    }

What I've tried so far: I have tried multiple fixes to resolve this issue. I initially thought I could resolve it by wrapping the entire body section (which includes all of the buttons) in a <asp:UpdatePanel ID="UpdatePanel1" runat="server">...'', but that did not work. Second, I tried just adding AutoPostBack="False" inside the <asp:Button..> tag, but nothing changed. I then changed all of the asp: buttons to simple html <input type="button"...> with the same onClick event. This stopped the reload, but the functionality (adding DateTime to the textbox in the C#) no longer worked. I've seen several possible jQuery solutions, but I am not sure if that will resolve my issue either. I am pretty new to ASP.NET but this seems like a common problem that anyone using the Web.Controls.UI buttons would run into. Any suggestions as to how to resolve this?

gwydion93
  • 1,681
  • 3
  • 28
  • 59
  • try adding `onClientClick="return false;"` on your button. – vikscool Feb 20 '19 at 03:53
  • 2
    Possible duplicate of [How to avoid page refresh after button click event in asp.net](https://stackoverflow.com/questions/21325211/how-to-avoid-page-refresh-after-button-click-event-in-asp-net) – vikscool Feb 20 '19 at 03:55
  • Adding `onClientClick="return false;"` does not resolve this issue. – gwydion93 Feb 20 '19 at 03:59
  • In regards to my question being a duplicate of "How to avoid page refresh after button click event in asp.net", I would like to note that: 1) That question was never answered (there is no satisfactory answer marked with a green check) and 2) As I mentioned in my question (see what I've tried so far) I have tried many of the suggestions in that question and none of them worked. – gwydion93 Feb 20 '19 at 04:03
  • If you don't want to reload the page, but you want to fire a server-side event, you'll need to [do it with AJAX](https://stackoverflow.com/questions/34852151/asp-net-webforms-calling-a-c-sharp-method-with-ajax) or other special Javascript request. Otherwise there is no means by which data can be transferred to the server to let it know the button was clicked. – John Wu Feb 20 '19 at 04:17
  • @gwydion93 I suppose you have tried everything. And for so 1) the suggested edit is to stop the event from firing any server-side code. 2) I just checked the code mentioned in the suggested duplicate(*i.e. is a combination of `UpdatePanel` and `ContentTemplate` without the `onClientClick` event*) on `Edge`,`Chrome`, `Firefox`, *`even IE`* and it is working as it should. – vikscool Feb 20 '19 at 04:24
  • I have everything wrapped in `` currently and removed `onClientClick` , but the behavior is still occurring. – gwydion93 Feb 21 '19 at 21:31

2 Answers2

0

few questions,

  • Have you added the script manager and configured your button using RegisterAsyncPostBackControl method on page load?
  • AutoPostBack should be always true. Other wise it will not trigger the server click event.

You can use jQuery. It'll definitely cover your requirement. But you have to write a web method instead of the button click event.

steps of Jquery

  1. add jquery files and add a class to the button declaration

  2. register the button click event


    $('.button-class').onClick(function()){
        //call to web method
    });

  1. create a web method in your server side with contain the business logic

  2. call the web method


$.ajax({
        type: 'POST',
        url: 'PageName.aspx/MethodName',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d) {
                //set your values to the text box
                $('.textbox-classname').val(msg.d.returningvalue);
            }
        },
        error: function () {
            alert("Error! Try again...");
        }
    });

0

Use updatepanels for this purpose...they will not refresh page and still exceute your click event

Shubham
  • 443
  • 2
  • 10
  • It's really weird; I have everything wrapped in `` which I used a while back for `DropdownLists` with my `_SelectedIndexChanged` functions in the C#. It worked fine although I had to add `AutoPostBack="False"` in a few places. So why doesn't it work for the buttons?! I'm investigating it now. – gwydion93 Feb 20 '19 at 13:07