Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix RegisterClientScriptBlock is Obsolete warning in Visual Studio

I have the following Sub Routine in my vb.net project which runs fine, but I constantly get build warnings:

'Public Overridable Sub RegisterClientScriptBlock(key As String, script As String)' is obsolete: 'The recommended alternative is ClientScript.RegisterClientScriptBlock(Type type, string key, string script). visit http://go.microsoft.com/fwlink/?linkid=14202

I think it would be best if I used the correct method. I have followed the instructions here - http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx but it didn't make a great amount of sense. Any help would be appreciated.

Protected Sub DBC_MEMBER_IsActive_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DBC_MEMBER_IsActive.CheckedChanged
        Dim showmessage As String = "Warning!\r\n \r\n Unticking this box will delete this Member from the Databse. \r\n \r\n If you are unsure, make sure the box remains ticked"
        Dim alertScript As String = "<script language=JavaScript> "
        alertScript += "alert('" & showmessage & "');"
        alertScript += "</script" & "> "

        If Not ClientScript.IsClientScriptBlockRegistered("alert") Then
            Me.RegisterClientScriptBlock("alert", alertScript)
        End If
    End Sub
like image 528
James Avatar asked Jan 29 '11 20:01

James


2 Answers

Try this:

If Not ClientScript.IsClientScriptBlockRegistered(GetType(YourPageClass), "alert") Then
    ClientScript.RegisterClientScriptBlock(GetType(YourPageClass), "alert", alertScript)
End If     

(Replace YourPageClass by the name of your page's class).

The type and the key parameters are used to prevent registering the same script in the same page. The type parameter probably does not make sense when you register your script directly in the page (like you do). However, it allows for example to avoid potential conflicts between different custom controls which register a script with the same key parameter.

like image 142
Maxim Gueivandov Avatar answered Nov 10 '22 11:11

Maxim Gueivandov


change Me.RegisterClientScriptBlock("alert", alertScript) to ClientScript.RegisterClientScriptBlock(TypeOf Me, "alert", alertScript)

I'm not sure if this is the right syntax in vb.net for "TypeOf Me". In C# it would look like this: ClientScript.RegisterClientScriptBlock(typeof(this), "alert", alertScript);

like image 40
Mark At Ramp51 Avatar answered Nov 10 '22 09:11

Mark At Ramp51