1. Original Entry + Comments2. Write a Comment3. Preview Comment
New comments for this entry are disabled.


December 17, 2003  |  Doing client script things in ASP.NET  |  7306 hit(s)

One of the many areas of ASP.NET where I could use to have more expertise is that of injecting client script into pages. I've often done it manually (e.g, by using control.Attributes.Add), but the Page class also has a number of methods for injecting client script in a more structured away. These include:

RegisterClientScriptBlock
RegisterStartupScript
RegisterOnSubmitStatement
GetPostBackClientEvent
GetPostBackHyperlink
GetPostBackEventReference

The problem -- a problem -- is that the documentation for these methods is, mmm, not so great. Moreover, the documentation lacks an overview that pulls all these together and provides a nice chart telling you under what circumstances to use which, um, method (member or approach). A task for the Whidbey docs.[1]

There are some articles on this already available, which are somewhat helpful:Anyway, based on what I read, I thought I'd play around a little. RegisterOnSubmitStatement sounded interesting, so I experimented to see if I could write the minimal-case example of confirming that you want to submit a page. Here's what I came up with:
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
Dim script As String = "return DoConfirm();"
Page.RegisterOnSubmitStatement("", script)
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)
Label1.Text = "Posted @ " & DateTime.Now.ToString()
End Sub
</script>
<html>
<head>
<script language="javascript">
function DoConfirm()
{
return confirm("Do you want to submit?");
}
</script>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
</p>
</form>
</body>
</html>

[1] According to this article, the various Page methods for client script are being moved to a ClientScript object.

[2] He retired. Already.




Colt   18 Dec 03 - 1:30 AM

When you're talking about "Client Script" in ASP.NET, which remind me that there's a chapter dedicate in this topic on "Real World ASP.NET Best Practices"
(I did a review of this book which is available @ http://aspalliance.com/271)

BTW, RegisterOnSubmitStatement is convenient and do what we expect easily, but one of the disadvantage is its "All-or-Nothing" effect applied on all "button" controls... I saw many threads on the forums are talking in this topic...

P.S. Joseph really retire? I attended a session presented by him in the past, and he look very young though! :)


 
Mike   18 Dec 03 - 8:13 AM

Yeah, we were a little surprised when Joe said he was leaving, since he is, as you point out, on the young side. Good for him, I guess. :-) We miss him, I must say -- he was a great explainer and always could make spare time when we had questions.

Thanks for the review link. Obviously, I have not dug very deeply into the various Registerxxxx methods, but it had occurred to me that the example I was working with for RegisterOnSubmitStatement was pretty simple. Joteke had a post on the forums:

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=421561

that got me thinking about these various methods. I always start simple (sometimes I stay there, haha), and the first step is just getting something working. Hopefully then I can figure out what to tell someone who's interested in client script about when to use which method, etc.