【问题标题】:ASP.net button onclick send querystringASP.net 按钮 onclick 发送查询字符串
【发布时间】:2011-07-10 12:46:24
【问题描述】:

我有按钮:

<asp:Button runat="server" OnClientClick="return ValidateNewMessage();" OnClick="PostComment" ID="AddCommentButton" CssClass="nice-button" Text="Add Your Comment" />

在网址上:

blog/42/gregre-re-greg-er-g/comments-6#comments

当我点击按钮时,我会:

blog/42/gregre-re-greg-er-g/comments-6?ID=42&comments=1&page=6

这让我的脚本工作起来很奇怪,我真的需要按钮来保持它的 URL 格式而不包含查询字符串数据,这很容易吗?这么胖点击它,页面还是:

blog/42/gregre-re-greg-er-g/comments-6#comments

发表评论方式如下:

/// <summary>
/// Post a comment
/// </summary>
public void PostComment(object sender, EventArgs e)
{
    CommentResponse CommentResp = CommentCommon.NewComment(NewComment.Text, this.ThisUser.UserID, this.Anchor);

    if (!this.ThisUser.IsLoggedIn)
        CommentResp.Status = CommentError.NotLoggedIn;

    // Error messages
    if (CommentResp.Status == CommentError.CommentsPostedTooQuick)
    {
        DiscussError.Visible = true;
        DiscussErrorMessage.Text = "You are posting comments too quickly";
    }
    else if (CommentResp.Status == CommentError.ExceededCommentsPer3Mins)
    {
        DiscussError.Visible = true;
        DiscussErrorMessage.Text = " You are posting comments too quickly";
    }
    else if (CommentResp.Status == CommentError.NotEnoughChars)
    {
        DiscussError.Visible = true;
        DiscussErrorMessage.Text = "Comment is not long enough";
    }
    else if (CommentResp.Status == CommentError.NotLoggedIn)
    {
        DiscussError.Visible = true;
        DiscussErrorMessage.Text = "You are not logged in";
    }
    else if (CommentResp.Status == CommentError.UnspecifiedError)
    {
        DiscussError.Visible = true;
        DiscussErrorMessage.Text = "Unspecified error.";
    }

    // Posted ok, redirect to last page
    if (CommentResp.Status == CommentError.Success)
    {
        int TotalComments = CommentCommon.CountComments(this.Anchor);
        int TotalPages = (TotalComments + Settings.CommentsPerPage - 1) / Settings.CommentsPerPage;
        Response.Redirect(this.PageNavURL.Replace("$1", TotalPages.ToString()) + "#comments");
    }

}

当状态不成功时会出现问题,当它重定向时它工作正常。

【问题讨论】:

  • 你的表单定义是什么样的?

标签: asp.net button postback


【解决方案1】:

以下代码执行重定向。

Response.Redirect(PageNavURL.Replace("$1", TotalPages.ToString()) + "#comments");

该 URL 似乎基于 PageNavURL 属性。您需要更改该属性或代码,使其生成不同的 URL。

【讨论】:

  • 这不是我遇到的问题页面调用的。当状态 == 成功时,它工作正常。当状态不等于成功时,它会扭曲页面 URL。这就是问题所在,而不是重定向行。
  • 这不是你原来的问题所说的。这不是你的代码所说的。如果没有重定向,那么它只是发回原始 URL。 PageNavURL 长什么样子?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多