ASP.NET:在运行时注入 Javascript(启用 AJAX)

发布于 2024-10-21 21:44:19 字数 1173 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风轻花落早 2024-10-28 21:44:19

Lav 的代码已经差不多了,但是因为您的元素位于 UpdatePanel 中,所以您需要进行以下更改:

sb.Append("<script language='javascript'>")
sb.Append("alert('TestMessage')")
sb.Append("</script>")

实际上,您可以删除第 1 行和第 3 行,因为我们将告诉它自动添加脚本标记,所以我们可以简单地说:

Dim s as String
s = "alert('TestMessage')"

现在,我们将使用 ScriptManager,而不是使用 ClientScript

If Not ClientScript.IsClientScriptBlockRegistered(t, "PopupScript") Then
    ClientScript.RegisterClientScriptBlock(t, "PopupScript", sb.ToString())
End If

相反,我们将使用 ScriptManager,如下所示:

If Not ScriptManager.IsClientScriptBlockRegistered(Me, "PopupScript") Then
    ScriptManager.RegisterStartupScript(Me, GetType(Page), "PopupScript", s, True)
End If

请注意,我们使用 RegisterStartupScript 方法,因为我们希望部分回发完成后立即运行脚本。

Lav's code is almost there, but because your elements are in an UpdatePanel you need to make the following changes:

sb.Append("<script language='javascript'>")
sb.Append("alert('TestMessage')")
sb.Append("</script>")

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:

Dim s as String
s = "alert('TestMessage')"

Now, instead of using ClientScript, we're going to use ScriptManager.

If Not ClientScript.IsClientScriptBlockRegistered(t, "PopupScript") Then
    ClientScript.RegisterClientScriptBlock(t, "PopupScript", sb.ToString())
End If

Instead, we're going to use ScriptManager, like so:

If Not ScriptManager.IsClientScriptBlockRegistered(Me, "PopupScript") Then
    ScriptManager.RegisterStartupScript(Me, GetType(Page), "PopupScript", s, True)
End If

Note that we use the RegisterStartupScript method because we want the script to be run as soon as the partial postback is complete.

并安 2024-10-28 21:44:19

请参阅这篇文章

以下是您可以用来按需显示 JavaScript 消息的代码片段

Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("Alert('TestMessage')")
sb.Append("/script>")
'register with ClientScript 
'The RegisterStartupScript method is also slightly different 
Dim t As Type = Me.[GetType]()
If Not ClientScript.IsClientScriptBlockRegistered(t, "PopupScript") Then
    ClientScript.RegisterClientScriptBlock(t, "PopupScript", sb.ToString())
End If

Look at this article.

Here is the code snippet that you can use to show javascript message on demand

Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("Alert('TestMessage')")
sb.Append("/script>")
'register with ClientScript 
'The RegisterStartupScript method is also slightly different 
Dim t As Type = Me.[GetType]()
If Not ClientScript.IsClientScriptBlockRegistered(t, "PopupScript") Then
    ClientScript.RegisterClientScriptBlock(t, "PopupScript", sb.ToString())
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文