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>