Figure 4 Web Forms Control Code Used to Generate HTML <DefaultProperty("Text"), ToolboxData("<{0}:SimplestWebControlCtrlVB _
runat=server></{0}:SimplestWebControlCtrlVB>")>
Public Class SimplestWebControlCtrlVB
Inherits System.Web.UI.WebControls.WebControl
' Create a new attribute named "style" in the writer's buffer.
' Set its value to be "color:[value of forecolor property]"
output.AddStyleAttribute("color", Me.ForeColor.ToKnownColor.ToString)
' Create an HTML tag named "span", place the attributes from the
' buffer into it, and write it to the output stream.
output.RenderBeginTag("span")
' Write the control's current text property to the output stream.
output.Write(Text)
' Write the close tag for the last open HTML tag, in this case
' span, to the output stream.
output.RenderEndTag()
End Sub
End Class
Figure 8 HTML Generating Table Control <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwxMDM2ODAwMjQzOzs+" />
<table Border="2">
<tr>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R1C1')">R1C1</td>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R1C2')">R1C2</td>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R1C3')">R1C3</td>
</tr><tr>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R2C1')">R2C1</td>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R2C2')">R2C2</td>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R2C3')">R2C3</td>
</tr><tr>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R3C1')">R3C1</td>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R3C2')">R3C2</td>
<td onClick="__doPostBack('TableWebControlCtrlVB1','R3C3')">R3C3</td>
</tr>
</table>
<span id="Label2" style="Z-INDEX: 103; LEFT: 20px; POSITION: absolute;
TOP: 114px">Your selection will be shown here</span>
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform = document.Form1;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
</form>
</body>
</HTML>
Figure 9 Render Method of Table Control Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
' Fetch selected row and column from control's view state
Dim SelectedRow, SelectedColumn As Integer
SelectedRow = Me.ViewState("SelectedRow")
SelectedColumn = Me.ViewState("SelectedColumn")
' Output beginning of table.
output.AddAttribute("Border", "2")
output.RenderBeginTag("Table")
' loop through each row
Dim i, j As Integer
For i = 1 To m_Rows
output.RenderBeginTag("TR")
' loop through each column in row
For j = 1 To m_Columns
' generate table cell text containing row and column name
Dim cellname As String
cellname = "R" + i.ToString + "C" + j.ToString
' add onClick handler that causes a postback when cell is
' clicked
output.AddAttribute("onClick",
Page.GetPostBackEventReference(Me, cellname))
' If this is the selected row and column,
' generate HTML for a different background color
If (i = SelectedRow And j = SelectedColumn) Then
output.AddStyleAttribute("background-color", "green")
End If
' write table data tag and contents
output.RenderBeginTag("TD")
output.Write(cellname)
output.RenderEndTag()
Next j
output.RenderEndTag()
Next i
output.RenderEndTag()
End Sub
Figure 10 Event-related Code in Sample Table Control Public Class TableWebControlCtrlVB
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackEventHandler
' Handler that gets called by ASP.NET when this control causes a
' postback
Public Sub RaisePostBackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
' Parse row and column number of clicked cell from string
Dim ClickedColumn, ClickedRow, Cindex As Integer
Cindex = eventArgument.IndexOf("C")
ClickedColumn = eventArgument.Substring(Cindex + 1)
ClickedRow = eventArgument.Substring(1, Cindex - 1)
' Signal any controls on this page that might be listening that
' the postback was triggered by a click on this control,
' specifying the row and column number.
RaiseEvent TableCellClicked(ClickedRow, ClickedColumn)
' Place the new row and column in the view state
Me.ViewState("SelectedRow") = ClickedRow
Me.ViewState("SelectedColumn") = ClickedColumn
End Sub
' Declare the event that our control fires when it receives a
' postback due to a click by the user on our table in the browser.
Public Event TableCellClicked(ByVal Row As Integer, ByVal _
Column As Integer)
End Class
Figure 11 Responding to Table Control Event Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents TableWebControlCtrlVB1 As _
TableWebControlCtrlVB.TableWebControlCtrlVB
' Receive notification of click event from table control. Set
' name of clicked control into label control for user to see
Private Sub TableWebControlCtrlVB1_TableCellClicked(ByVal Row As _
Integer, ByVal Column As Integer) Handles _
TableWebControlCtrlVB1.TableCellClicked
Label2.Text = "You clicked row " + Row.ToString + " column " + _
Column.ToString
End Sub
End Class
Figure 13 HTML Produced by Scripting Sample Application <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="WebForm1.aspx"
language="javascript" onsubmit="return __ChooseToSubmit();" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDw0NzUyMzI2NTM7Oz4=" />
<script language="javascript"> function __doAlert() {alert("I've been
clicked");} </script>
<script language="javascript"> function __ChooseToSubmit() {if
(window.prompt ("Enter Y to allow submit") == "Y") return true; return
false; } </script>
<span onClick="__doAlert();" style="color:0;">Browser name = IE6
JavaScript = True VBScript = True EcmaScriptVersion = 1.2
</span><input type="submit" name="Button1" value="Button"
id="Button1" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute;
TOP: 93px" /><script language=JavaScript> alert("startup script");
</script>
</form>
</body>
</HTML>
Figure 14 Web Forms Sample Code <DefaultProperty("Text"), ToolboxData("<{0}:ScriptingControlCtrlVB _
runat=server></{0}:ScriptingControlCtrlVB>")>
Public Class ScriptingControlCtrlVB
Inherits System.Web.UI.WebControls.WebControl
Protected Overrides Sub Render(ByVal output As _
System.Web.UI.HtmlTextWriter)
' Add attribute that calls a script when clicked
output.AddAttribute("onClick", "__doAlert();")
' Create an HTML tag named "span", place the attributes from the
' buffer into it, and write it to the output stream.
output.RenderBeginTag("span")
' Write the browser's scripting capability to the output stream.
output.Write("Browser name = " + Page.Request.Browser.Type + _
" JavaScript = " + Page.Request.Browser.JavaScript.ToString + _
" VBScript = " + Page.Request.Browser.VBScript.ToString + _
" EcmaScriptVersion = " + _
" Page.Request.Browser.EcmaScriptVersion.ToString)
' Write the close tag for the last open HTML tag, in this case
' span, to the output stream.
output.RenderEndTag()
End Sub
Private Sub WebCustomControl1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Add startup script that gets called automatically when page is
' shown
Page.RegisterStartupScript("Script1", "<script language=JavaScript>
alert(""startup script""); </script>")
' Register script block that pops up a box announcing a click. The
' Render method adds a handler that calls it.
Page.RegisterClientScriptBlock("ClientScript1", "<script
language=""javascript""> function __doAlert()
{alert(""I've been clicked"");} </script>")
' Add script that gets called by OnSubmit handler to validate input
' and optinally abort submit process
Page.RegisterClientScriptBlock("MyOnSubmitScript", "<script
language=""javascript""> function __ChooseToSubmit()
{if (window.prompt (""Enter Y to allow submit"") == ""Y"")
return true; return false; } </script>")
' Add OnSubmit handler that calls script added in previous line
Page.RegisterOnSubmitStatement("SubmitScript", "return
__ChooseToSubmit();")
End Sub
End Class
|