2012-01-26 208 views
1

我有asp.net網站,其中包括一些按鈕上的計算。當我按下按鈕時,我的計算工作正常,但首先頁面正在刷新,然後計算會在標籤上顯示。我想直接在標籤上進行計算。沒有提神。我給一些代碼。單擊按鈕時如何停止重新加載頁面?

p.s.此外Page_Load中具有接收每天的匯率

ASP

<asp:Button ID="Button1" runat="server" BackColor="#990000" 
BorderColor="#333333" ForeColor="White" onclick="Button1_Click" Text="Calculate" 
Width="85px" BorderStyle="Outset" style="margin-left: 20px" 
ValidationGroup="grup1" /> 

按鈕單擊

protected void Button1_Click(object sender, EventArgs e) 
{ 
double sayi1, sayi2, sayi3, hesap, sonuc; 

sayi1 = Convert.ToDouble(Tb1.Text); 
sayi2 = Convert.ToDouble(Tb2.Text); 
sayi3 = Convert.ToDouble(Tb3.Text); 


if (Tb1.Text.Contains(".") || Tb2.Text.Contains(".") || Tb3.Text.Contains(".")) 
{ 
    ... 
    ... 
    ... 

回答

1

使用Ajax的功能。如果不是,您將始終調用回發事件。或者另一方面用JavaScript做一些客戶端編程。

0

在您的代碼中編寫一個WebMethod,並通過jQuery中的click函數調用WebMethod。

$(document).ready(function() { 
    $("<%= Button1.ClientID%>").click(function() { 
    $.ajax({ 
      type: "POST", 
      url: "PageName.aspx/MethodName", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(msg) { 
       // Do something interesting here. 
      } 
      }); 
    }); 
}); 

在後面的代碼編寫的WebMethod,

public partial class _Default : Page 
{ 
    [WebMethod] 
    public static string MethodName() 
    { 
    //Your code for calculation goes over here. 
    } 
} 
0

可以使用的UpdatePanel。去ajaxify。 Update panel in Asp.net

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" /> 
     </Triggers> 
     <ContentTemplate> 
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
      <br /> 
      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
      <br /> 
      <asp:Label ID="lblResult" runat="server" /> 
      <asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="text" /> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

你的代碼背後。 //但你需要驗證所有的輸入,以避免格式Execption

protected void btn_Click(object sender, EventArgs e) 
     { 
      int num1, num2, sum; 
      TextBox t = (TextBox) UpdatePanel1.FindControl("Textbox1"); 
      num1 = Convert.ToInt32(t.Text); 
      t = (TextBox)UpdatePanel1.FindControl("Textbox2"); 
      num2 = Convert.ToInt32(t.Text); 
      sum = num1 + num2; 
      lblResult.Text = sum.ToString(); 
     } 
0
Create new benchmark or Add rules to a benchmark</h3> 
      <asp:ScriptManager ID="ScriptManager1" runat="server"> 
       </asp:ScriptManager> 
      <asp:updatepanel ID="instuctionsUpdate" runat="server" updatemode="Conditional" > 
      <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="opener" EventName="click" /> 
      </Triggers> 
      <ContentTemplate> 
      <asp:button id="opener" runat="server" Text="Click me for instructions" 
      onClick="opener_click" EnableTheming="False" EnableViewState="False" /> 

的JavaScript

$("#<%=dialog.ClientID%>").dialog({ autoOpen: false }); 
     $("#<%=opener.ClientID%>").click(function(){ 
      $("#<%=dialog.ClientID%>").dialog("open"); 
     }); 
相關問題