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

本文关键字:提交 表单 方法 asp net 按钮 onclick 使用 | 更新日期: 2023-09-27 17:58:02

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

这就是我迄今为止所尝试的

 <%@ 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>

还有其他方法可以实现我正在做的事情吗?欢迎提出建议。

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

我真的不知道你到底是什么意思。。。。我想你是在问如何通过shout boxaspx中插入评论???。。。大概

以下是从"方法"将您想要键入的内容(注释)插入表单的代码。。。尽管它使用的不仅仅是一种方法。。这是我能想到的最简单的方法。。。

这是您的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();
}
}

这将允许你插入任何你想要的评论。您可以将此代码修改为自己的代码,请。。。。

如果这是你想要的答案,请告诉我。

使用按钮的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
}