我在webform上有一个按钮,在Ajax表容器中的更新面板中,它将文本框中的数据保存到数据库中。我有五条if语句,用于验证页面上的日期范围,并在范围无效时发出警告(例如):
If <condition> Then
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Actual end date cannot be earlier than Sign date.');", True)
updateFlag = False
End If这个代码很好用。设置了用于更新数据的标志。问题是,如果代码通过if进入更新部分:在更新完成后,我放在那里的“保存数据”的alert不会弹出。我使用了以下每一种方法:
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
Me.btnSave.Attributes.Add("onclick", "alert('Job saved.');")
ClientScript.RegisterStartupScript() 'don't have the rest of this line他们都没有工作过。数据按其应有的方式保存;就像代码跳过alert行一样。如果有人能帮我,我将非常感激。
编辑:澄清我将Me.btnSave.Attributes.Add("onclick", "alert('Job saved.');")放在Page_Load中以及update语句之后,但在实际保存数据之前触发,因此在“保存”数据之后接收错误消息。点错了,不能用。
编辑2:张贴更多的代码。代码背后:
Protected Sub SaveAll()
If <condition> Then
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Actual end date cannot be earlier than Sign date.');", True)
updateFlag = False
End If
...
If updateFlag Then
'add values to SqlDataAdapter
...
con.Open()
da.SelectCommand.ExecuteNonQuery()
con.Close()
da.Dispose()
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alertMessage", "alert('Job saved.');", True)
'Me.btnSave.Attributes.Add("onclick", "alert('Job saved.');")
Response.Redirect("Edit.aspx?ID=" & txtID.Text)
End If
End Subaspx:
<ajaxToolkit:TabContainer ID="TabContainer1"
runat="server" TabStripPlacement="Top">
<ajaxToolkit:TabPanel runat="server" ID="JobPanel" HeaderText="Job Info">
<ContentTemplate>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<asp:Button ID="btnSave" OnClick="SaveAll"
runat="server" Text="Save" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>如果你需要更多代码,请告诉我。
发布于 2015-02-18 14:19:17
在这个特定的场景中,您不需要UpdatePanel,甚至AJAX。
Protected Sub SaveAll()
If <condition> Then
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alertMessage", "alert('Actual end date cannot be earlier than Sign date.');", True)
updateFlag = False
End If
...
If updateFlag Then
'add values to SqlDataAdapter
...
con.Open()
da.SelectCommand.ExecuteNonQuery()
con.Close()
da.Dispose()
Response.Redirect("Edit.aspx?ID=" & txtID.Text & "&message=Job%20Saved")
End If
End Sub您试图运行某个客户端脚本,但随后重定向,从而结束响应。因此,解决方案是将消息传递给下一页,并让下一页显示确认消息。Edit.aspx页面应该检查页面加载上的查询字符串,如果有消息,则显示它。但不要使用alert()。那些太烂了。它们吸引了用户的注意力,不能被设计成样式。找到一个不错的客户端通知库。我真的很喜欢诺蒂。
我不是VB程序员,但您需要将数据库连接封装在using语句中,这样它们才能正确地释放(或任何与VB等价的东西)。
https://stackoverflow.com/questions/28571217
复制相似问题