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!

OrElse
  • 9,709
  • 39
  • 140
  • 253

2 Answers2

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.

Pandincus
  • 9,506
  • 9
  • 43
  • 61
  • Hello, could you take a look at http://stackoverflow.com/questions/5612167/value-cannot-be-null-parameter-name-control – OrElse Apr 10 '11 at 15:01
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
Lav
  • 1,850
  • 15
  • 17
  • I have already tried that but, it does not work. I guess it has something to do with partial postbacks. – OrElse Mar 13 '11 at 23:06
  • @Chocol8, this code should work. If you're doing a partial postback you might have to use ScriptManager.RegisterClientScriptBlock instead of ClientScript. – Pandincus Mar 13 '11 at 23:07
  • @Chocol8 Have a look here: --> http://stackoverflow.com/questions/545290/does-the-clientscriptmanager-work-when-called-in-a-partial-postback – Pandincus Mar 13 '11 at 23:09
  • @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. – Pandincus Mar 13 '11 at 23:17