Custom Web Form Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a web application for my company (VB.NET). I am adding a custom
web form class to prevent all the code reuse as you can see below...

Public Class UIWebForm
Inherits System.Web.UI.Page

Here I have a Public Sub that will use a ASP Literal on the page to display
a javascript alert message...

Protected WithEvents ltlAlert As System.Web.UI.WebControls.Literal

Public Sub ShowAlertMessage(ByVal Message As String)
' Format string properly
Message = FormatJavaScriptMessage(Message)
' Display as JavaScript alert
ltlAlert.Text = "alert('" & Message & "')"
End Sub

Public Function FormatJavaScriptMessage(ByVal Message As String) As
String
Message = Message.Replace("'", "\'")
Message = Message.Replace(Convert.ToChar(10), "\n")
Message = Message.Replace(Convert.ToChar(13), "")
Return Message
End Function

I need to drop the ASP Literal control onto my webform as follows to get
this to work...

<script>
<asp:Literal id="ltlAlert" runat="server"
EnableViewState="False"></asp:Literal>
</script>

Is there a way that I can have the above code also enter on my page
automatically so that I do not have to enter it on every page that create.
Can I add when the controls render or anything? I cannot think of the
specific funcitons I would need to call or implement
 
Rather than use a hardcoded label as an alert placeholder, why don't you
just use Page.RegisterStartupScript? and register the alert statement
through there?

bill
 
Hi Welcome to ASPNET newsgroup.

As for the displaying alert message in asp.net page you mentioned, I think
William's suggestion on using
Page.RegisterStartupScript function is good. This is a member fucntion of
Page class, however it is not automatically displayed in VB.NET IDE , we
can manually type it in our code. Also, it's better to encapsualte it into
a Page fucntion (in a base page class) so that we can resue it in other
concrete pages, for example:


Public Sub MsgBox(ByVal msg As String)
Page.RegisterStartupScript("msg_script", _
String.Format("<script
language='javascript'>alert('{0}');</script>", msg) _
)

End Sub

Then, in our postback event, we can use the following code to output the
scriptcode which displaying an alert msgbox

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click
MsgBox("hello aspnet!")

End Sub

HTH. Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top