Callback With Master Pages [Help REALLY Appreciated]

K

kfc1976

Hello All and Advanced Thanks For Replying,
OS: XP Pro
IDE: Visual Studio 2005
..net Framework: 2.0.....

Problem: Getting An Error When trying to implement the following
CallBack on a content page that uses a master page (Code From
Professional asp.net 2.0 Book)

Error:
/***********************************************************************************/
Line: 163
Char: 13

Error:'document.forms.0.TextBox1' is null or not an object

Code: 0
URL: http://localhost:1325/WebSite4/Experiment_WthMaster.aspx
/***********************************************************************************/


Code Behind
/*************************************************************************************/
Partial Class Experiment_WthMaster
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler

Dim _callbackResult As String = Nothing

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles Me.Load

Dim cbReference As String =
Page.ClientScript.GetCallbackEventReference(Me, "arg",
"GetRandomNumberFromServer", "context")
Dim cbScript As String = "function UseCallback(arg, context) {"
& cbReference & ";" & "}"

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"UseCallback", cbScript, True)
End Sub

Public Sub RaiseCallbackEvent(ByVal eventArgument As String)
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
_callbackResult = Rnd().ToString()
End Sub

Public Function GetCallbackResult() As String Implements
System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return _callbackResult
End Function
End Class
/*************************************************************************************/

Code ASPX
/*************************************************************************************/
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false" CodeFile="Experiment_WthMaster.aspx.vb"
Inherits="Experiment_WthMaster" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">


<script type="text/javascript">
function GetNumber(){
UseCallback();
}

function GetRandomNumberFromServer(TextBox1, context){
document.forms[0].TextBox1.Value = TextBox1;
}
</script>


<div>
<input id="Button1" type="button" value="Get Random Number"
onclick="GetNumber()" />
<br />
<br />
<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
</div>

</asp:Content>
/*************************************************************************/
Help REALLY Appreciated
Thanks
Farooq
 
A

Alan Silver

document.forms[0].TextBox1.Value = TextBox1;

This is your problem. When you use master pages (or anything else that
wraps a container around controls), the framework changes the names of
the controls to ensure uniqueness.

What you need to do is change this so that the server side code adds the
ClientID of the control. Something like...

document.forms[0].<%#TextBox1.ClientID%>.Value = TextBox1;

There have been a couple of threads about this in the past couple of
weeks. If this doesn't make sense, look back through the archives to see
more discussion.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top