我有一个我在这个网站上发布的sql语句,我在我的网站上申请工作,并在用户按下提交按钮后立即将用户名和信息插入数据库。
但是,当我设置提交的按钮PostBackUrl属性时,这不会发生。当我这样做时,没有插入任何数据,并且似乎跳过了保存insert语句执行的整个函数(因为我试图故意犯错,并且没有抛出任何异常)。
如何使postBackUrl正常工作,以便数据有时间插入?
工作sqlStatment:
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" />
答案 0 :(得分: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");
}