2

我在这个网站上发布了一条 sql 语句,我在我的网站上申请了该网站,该网站可以在用户按下提交按钮后不久将用户名和信息插入数据库。

但是,当我设置提交的按钮 PostBackUrl 属性时,这不会发生。当我这样做时,没有插入任何数据,并且似乎跳过了执行插入语句的整个函数(因为我试图故意犯错误,并且没有引发异常)。

如何使 postBackUrl 工作,以便有时间插入数据?

工作sql语句:

 insertCommand.Append("DECLARE @TopicsId int; INSERT INTO Topics(Theme,Topics,Date)");
    insertCommand.Append("VALUES(@topic,@subTopic,GETDATE())");
    insertCommand.Append("SET @TopicsId = SCOPE_IDENTITY()");

    insertCommand.Append(" INSERT INTO Threads(UsersID,TopicsID,Date,ThreadTitle,ThreadParagraph,ThreadClosed,Views,Replies,PageNumber)");
    insertCommand.Append(" SELECT @uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0,0,0,FLOOR(Count(t.TopicsID)/20)");
    insertCommand.Append(" FROM Threads AS d INNER JOIN Topics AS t ON d.TopicsID=t.TopicsID");

工作时间:

<asp:Button ID="sendButton1" runat="server" Text="שלח" Width="60px" 
            onclick="sendButton1_Click"  />

非工作 sqlStatement 时:

<asp:Button ID="sendButton1" runat="server" Text="שלח" Width="60px" 
            onclick="sendButton1_Click" PostBackUrl="~/AnswerQuestion.aspx" />
4

1 回答 1

1

这会导致 cross-page postback. 单击 Button 控件时,它会将当前页面发布到按钮的 PostbackURL 属性中提供的 URL。在这种情况下,您的按钮 Click 事件不会被触发。

有关详细信息,请查看MSDN

EditL为什么不简单地尝试一下,比如......

protected void Button1_Click(object sender, EventArgs e)
{
///.....Your Code.....
insertCommand.Append("DECLARE @TopicsId int; INSERT INTO Topics(Theme,Topics,Date)");
insertCommand.Append("VALUES(@topic,@subTopic,GETDATE())");
insertCommand.Append("SET @TopicsId = SCOPE_IDENTITY()");

insertCommand.Append(" INSERT INTO Threads(UsersID,TopicsID,Date,ThreadTitle,ThreadParagraph,ThreadClosed,Views,Replies,PageNumber)");
insertCommand.Append(" SELECT @uniqueIdentifier,@TopicsID,GETDATE(),@questionTitle,@questionParagraph,0,0,0,FLOOR(Count(t.TopicsID)/20)");
insertCommand.Append(" FROM Threads AS d INNER JOIN Topics AS t ON d.TopicsID=t.TopicsID");

 ///..At the end add this......
 Response.Redirect("~/AnswerQuestion.aspx");
}
于 2011-06-13T06:44:14.870 回答