First, let me preface this by saying that this question does relate to a class I'm taking, but not to any particular homework problem. I'm currently enrolled in a for-credit online ASP.NET 3.5 course. I come from PHP-land, so I've been trying to do some catch-up with C#.
The book I have has the following example code (apologies for the length):
The .aspx page:
Code:
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="RandomNumber.aspx.cs" Inherits="RandomNumber" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Callback Page</title>
<script type="text/javascript">
function GetNumber()
{
UseCallback();
}
function GetRandomNumberFromServer(TextBox1, context) //[1], [2]
{
document.forms[0].TextBox1.value = TextBox1;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Get Random Number" onclick="GetNumber()" />
<br />
<br />
<asp:TextBox id="TextBox1" Runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
The .cs code-behind file:
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; //
public partial class RandomNumber : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler //
{
private _callbackResult = null;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallBackEventReference(this, "arg", "GetRandomNumberFromServer", "context"); // [3], [4]
string cbScript = "function UseCallback(arg, context)" + "(" + cbReference + ";" + ")"; // [5]
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cbScript, true); // [6]
}
public void RaiseCallbackEvent(string eventArg)
{
Random rnd = new Random();
_callbackResult = rnd.Next().ToString(); // [7]
}
public string GetCallbackResult()
{
return _callbackResult;
}
}
My questions:
[1] - Is it necessary to name the argument passed to the function the same as the input its modifying?
[2] - What is 'context'?
[3] - What is 'arg'?
[4] - Again, what is 'context'?
[5] - Is this placing the body of GetRandomNumberFromServer() into UseCallback()? If not, what exactly is going on here?
[6] - More or less the same as [8]...is the new UseCallback() being written to the page?
[7] - What does invoking Next() give us?
Thanks for your time!