0

I want to prevent this phenomenon ...

Postback when button is clicked

I want to perform the result when the button is clicked and prevent the screen refresh.

<asp:Button ID="DomainSeachButton" runat="server" Text="search" 
            OnClick="btnDomainSearch_Click" OnClientClick="onMySearch();" Width="69px" /> 
function onMySearch() {
    // __doPostBack("DomainSeachButton", "client");
    var r = confirm("Press a button!")
    if (r==true)
    {
         alert("You pressed OK!")
         return true;
    }
protected void btnDomainSearch_Click(object sender, EventArgs e)
{

    if (_cDBConnect.IsValidDBInfo() == true)
    {
        string sql = string.Format("select * from tb_licensekey_storages where cert_domain_name like '%{0}%';", txtSearchDomain.Text.Trim());

        var da = new SQLiteDataAdapter(sql, _cDBConnect.GetConnectionString());

        DataTable dt = new DataTable();

        da.Fill(dt);
        gridViewDBInfo.DataSource = dt;
        gridViewDBInfo.DataBind();
    }
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
dev_O
  • 153
  • 2
  • 10

3 Answers3

0

Try changing your btnDomainSearch_Click to

$("#btnDomainSearch_Click").click( function(e){
    e.preventDefault();

    //Whatever
})
JamesS
  • 2,167
  • 1
  • 11
  • 29
0

You can use AutoPostBack="false" in the particular button.

if AutoPostBack set false it will not send request to server otherwise it will send request to server.

<asp:Button ID="DomainSeachButton" runat="server" Text="search" AutoPostBack="false" OnClick="btnDomainSearch_Click" OnClientClick="onMySearch();" Width="69px" />

Please read this article for more information More Details

Chandra Shakar
  • 126
  • 2
  • 8
  • Postback occurred when using AutoPostBack = "false". When UpdatePanel and AutoPostBack = "false" were used, postback did not happen. but your comment thx – dev_O Feb 01 '19 at 14:33
0

You can easily achieve this with ASP Control UpdatePanel it will not actually refresh the whole page but the the selected content of the page, for example:

<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="updtpnl" runat="server">
  <ContentTemplate>
    <asp:Button
      ID="DomainSeachButton"
      runat="server"
      Text="search"
      OnClick="btnDomainSearch_Click"
      OnClientClick="onMySearch();"
      Width="69px"
      AutoPostBack="true"
    />
    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="DomainSeachButton" EventName="Click" />
  </Triggers>
</asp:UpdatePanel>
Jawad Anwar
  • 485
  • 4
  • 15
  • Should UpdatePanel and AutoPostBack be used at the same time? Single use of "AutoPostBack = false" Postback happened. ex) `` but Postback did not occur when using UpdatePanel and AutoPostBack = false. – dev_O Feb 01 '19 at 14:26
  • Yeah when you postback set to true it refresh the content area not the whole page, otherwise nothing will change. – Jawad Anwar Feb 01 '19 at 16:19