1

我有 asp.net 网站,其中包括一些按钮计算。当我按下按钮时,我的计算工作正常,但首先页面刷新,然后计算显示在标签上。我想直接在标签上进行计算。无需提神。我给出一些代码。

ps 还有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("."))
{
  ...
  ...
  ...
4

4 回答 4

1

使用 AJAX。如果不是,您将始终调用回发事件。或者另一方面,使用 JavaScript 进行一些客户端编程。

于 2012-01-26T01:24:38.743 回答
0

您可以使用更新面板。ajaxify。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>

在你的代码后面。// 虽然您需要验证所有输入,以避免 Format 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();
        }
于 2012-01-26T02:44:17.770 回答
0

在后面的代码中编写一个 WebMethod 并从 jQuery 的 click 函数中调用 WebMethod this。

$(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.
  }
}
于 2012-01-26T01:52:24.803 回答
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");
        });
于 2013-05-12T17:25:47.523 回答