ASP.NET:在运行时注入 Javascript(启用 AJAX)
我的 Imagebutton 存在于 ListView
对象和 UpdatePanel
中。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:ImageButton ID="btnAttach" runat="server" OnClientClick="update('Clip','false','inc')" ImageUrl="~/Images/Image.png" CommandName='<%# DataBinder.Eval(Container.DataItem, "ID")'/>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
在 ListView ItemCommand
事件中,我执行检查以查看数据库中是否存在记录。 如果它不存在,我只需添加它。 现在,如果记录存在,我想显示一条 JavascriptMessage,显示该记录已存在。
Private Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
If Not RecordExists() Then
InsertRecord()
Else
Show JavascriptMessage (Record Already Exists!)
End If
End Sub
那么我该怎么做呢?我尝试了几个不同版本的 Show JavascriptMessage
,但没有一个有效!
My Imagebutton exists within a ListView
object and an UpdatePanel
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:ImageButton ID="btnAttach" runat="server" OnClientClick="update('Clip','false','inc')" ImageUrl="~/Images/Image.png" CommandName='<%# DataBinder.Eval(Container.DataItem, "ID")'/>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
At the ListView ItemCommand
event i perform a check to see if a record exists in a database.
If it does not exist, i simply add it.
Now, if the record exists, i would like to show a JavascriptMessage displaying that the record already exists.
Private Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
If Not RecordExists() Then
InsertRecord()
Else
Show JavascriptMessage (Record Already Exists!)
End If
End Sub
So how could i do that? I tried several different versions of the Show JavascriptMessage
, but none worked!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Lav 的代码已经差不多了,但是因为您的元素位于 UpdatePanel 中,所以您需要进行以下更改:
实际上,您可以删除第 1 行和第 3 行,因为我们将告诉它自动添加脚本标记,所以我们可以简单地说:
现在,我们将使用
ScriptManager
,而不是使用ClientScript
。相反,我们将使用 ScriptManager,如下所示:
请注意,我们使用 RegisterStartupScript 方法,因为我们希望部分回发完成后立即运行脚本。
Lav's code is almost there, but because your elements are in an UpdatePanel you need to make the following changes:
Actually, you can get rid of lines 1 and 3 because we're going to tell it to automatically add the script tags, so we can simplify and just say:
Now, instead of using
ClientScript
, we're going to useScriptManager
.Instead, we're going to use ScriptManager, like so:
Note that we use the
RegisterStartupScript
method because we want the script to be run as soon as the partial postback is complete.请参阅这篇文章。
以下是您可以用来按需显示 JavaScript 消息的代码片段
Look at this article.
Here is the code snippet that you can use to show javascript message on demand