2

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!

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, could you take a look at stackoverflow.com/questions/5612167/…
1

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

4 Comments

I have already tried that but, it does not work. I guess it has something to do with partial postbacks.
@Chocol8, this code should work. If you're doing a partial postback you might have to use ScriptManager.RegisterClientScriptBlock instead of ClientScript.
@Chocol8 Have a look here: --> stackoverflow.com/questions/545290/…
@Chocol8 - Try RegisterStartupScript instead? Also, if the 'AddScriptTags' or whatever is set to true, then make sure you DON'T include script tags in your StringBuilder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.