使用asp.net按钮onclick方法而不是提交表单

时间:2014-04-11 21:45:03

标签: c# html asp.net

我有一个asp.net Button控件,我想用它在我的页面中插入注释。当我点击按钮时,我希望它调用方法而不是提交表单。我如何实现这一目标?

这是我到目前为止所尝试的 -

 <%@ Page Language="C#" %>
 <!DOCTYPE html>
  <script runat="server">    
 protected void Button1_Click(object sender, EventArgs e)
 {
      //Do some stuff here   
 }
 </script>
  <head>title and other css links go here</head>
<body>
    <form id="form1" runat="server" onsubmit="false">
        //Some other asp.net controls go here
        <asp:Button ID="Button1" runat="server" Text="Comment" OnClick="Button1_Click"/>
    </form>
</body>
</html>

还有其他办法可以实现我的目标吗?欢迎提出建议。

2 个答案:

答案 0 :(得分:4)

我并不确切地知道你的意思......我想你是在询问如何通过aspx将评论插入shout box ???也许吧?

以下是从&#34;方法&#34; ...中插入您想要输入的任何内容(注释)的代码...虽然它使用的不仅仅是单个方法...这是我能想到的最简单的方法......

这是您的default.aspx(此处通知no master页面)

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>AJAX Example for comment</title>
 <link href="Main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="page">
<div id="main">
  <div id="shoutbox">
  <asp:ScriptManager ID="ScriptManager1" runat="server">
  </asp:ScriptManager>
  <p>Here's what everyone is saying:</p>
  <p>
    <asp:UpdatePanel ID="ShoutBoxPanel1" runat="server">
      <ContentTemplate>
        <asp:Label ID="lblShoutBox" runat="server"></asp:Label>
        <asp:Timer ID="Timer1" runat="server" Interval="5000">
        </asp:Timer>
      </ContentTemplate>
      <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnAddShout"
            EventName="Click" />
      </Triggers>
    </asp:UpdatePanel>
  </p>
  <p>
    <asp:UpdatePanel ID="ShoutBoxPanel2" runat="server"
        UpdateMode="Conditional">
      <ContentTemplate>
        <p class="label">Name:</p>
        <p class="entry">
          <asp:TextBox ID="txtUserName" runat="server"
              MaxLength="15" Width="100px"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
              runat="server" ErrorMessage="Name is required."
              ControlToValidate="txtUserName" Display="Dynamic" 
              CssClass="error">
          </asp:RequiredFieldValidator>
        </p>
        <p class="label">Shout:</p>
        <p class="entry">
          <asp:TextBox ID="txtShout" runat="server"
              MaxLength="255" Width="220px"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" 
              runat="server" ErrorMessage="Shout is required."
              ControlToValidate="txtShout" Display="Dynamic" 
              CssClass="error">
          </asp:RequiredFieldValidator>
        </p>
        <asp:Button ID="btnAddShout" runat="server" Text="Add Shout" 
            onclick="btnAddShout_Click" />
        <asp:UpdateProgress ID="UpdateProgress1" runat="server"
            DynamicLayout="False">
          <ProgressTemplate>
            <img src="Images/spinner.gif" alt="Please Wait" />
             Comment...
          </ProgressTemplate>
        </asp:UpdateProgress>
      </ContentTemplate>
    </asp:UpdatePanel>
  </p>
</div>
</div>
</div>
</form>
</body>
</html>

这是你的C#代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    ShoutItemList shoutBox;
    if (Application["ShoutBox"] == null)
    {
        shoutBox = new ShoutItemList();
        Application.Add("ShoutBox", shoutBox);
    }
    else
    {
        shoutBox = (ShoutItemList)Application["ShoutBox"];
        lblShoutBox.Text = shoutBox.Display();
    }
    if (ScriptManager1.IsInAsyncPostBack != true)
        txtUserName.Focus();
}


protected void btnAddShout_Click(object sender, EventArgs e)
{
    ShoutItem shout = new ShoutItem();
    shout.UserName = txtUserName.Text;
    shout.Comment = txtShout.Text;
    shout.Timestamp = DateTime.Now;

    Application.Lock();
    ShoutItemList shoutBox = (ShoutItemList)Application["ShoutBox"];
    shoutBox.Add(shout);
    Application.UnLock();

    lblShoutBox.Text = shoutBox.Display();
    txtShout.Text = "";
    txtShout.Focus();
}
}
public class ShoutItem
{
    public string UserName { get; set; }
    public DateTime Timestamp { get; set; }
    public string Comment { get; set; }
}
public class ShoutItemList
{
private List<ShoutItem> shoutList = new List<ShoutItem>();

private void Purge()
{
    DateTime purgeTime = DateTime.Now;
    purgeTime = purgeTime.AddMinutes(-3);

    int i = 0;
    while (i < shoutList.Count)
    {
        if (shoutList[i].Timestamp <= purgeTime) shoutList.RemoveAt(i);
        else i += 1;
    }
}

public void Add(ShoutItem shout)
{
    Purge();
    System.Threading.Thread.Sleep(2000);
    shoutList.Insert(0, shout);
}

public string Display()
{
    Purge();
    StringBuilder shoutBoxText = new StringBuilder();
    if (shoutList.Count > 0)
    {
        shoutBoxText.AppendLine("<dl>");
        foreach (ShoutItem shout in shoutList)
        {
            shoutBoxText.Append("<dt>" + shout.UserName + " (");
            shoutBoxText.Append(shout.Timestamp.ToShortTimeString() + ")</dt>");
            shoutBoxText.AppendLine("<dd>" + shout.Comment + "</dd>");
        }
        shoutBoxText.AppendLine("</dl>");
    }
    return shoutBoxText.ToString();
}
}

这将允许您插入所需的任何评论。你可以自己修改这段代码......

请告诉我这是您寻求的答案。

答案 1 :(得分:2)

使用按钮的OnClientClick,如下所示:

<asp:Button ID="Button1" runat="server" Text="Comment" OnClientClick="return javascriptFunction();" OnClick="Button1_Click"/>

然后你的javascript函数看起来像这样

function javascriptFunction() {
  //do something here
  return false; //if you don't want the form to POST to the server, leave this as false, otherwise true will let it continue with the POST

}