Client to Server Control interaction - much confussion RRS feed

  • Question

  • User1064158201 posted

    This is all done in .NET 4.0

    I have created a custom ASP.NET server control.  It is based on ScriptControl and implements INamingContainer, ICallbackEventHandler and IPostBackEventHandler.  Currently the RaisePostBackEvent method is empty and is never called.

    I first created the control WITHOUT implementing ICallbackEventHandler.  I placed the control in an UpdatePanel and it appeared that partial page updates were working as expected.  I have both JavaScript and C# script in the web page that hosts the control.  I can call into server control code from either the C# or JavaScript code.   The C# code is invoked by being the target of an OnCLick on an ASP.NET button control.  The JavaScript code is invoked by being the target of an onclick on an HTML button.  The JavaScript code then calls a PageMethods method in the page code behind which finds control and calls the correct method.

    Remember this is BEFORE implmenting ICallbackEventHandler
    Question - Is there any difference in performance/lifecycle etc between the C# call and the JavaScript call.

    After implementing ICallbackEventHandler and supplying all the correct scripts (that was fun), I can post a callback from JavaScript with a JSON object and return a JSON response.  The Page.IsCallback is correctly set within the control. 

    Now, the puzzling part.  My callback complete method on the webpage simply does an alert so that I can see that it worked.  As soon as the alert appears, the server control is re-entered as a PostBack (page.IsPostback is trueand Page.IsCallback is false).  I do NOT know why this happens.  (I still have the control in an UpdatePanel.)  Any sugestions as to why the postback are appreciated.

    My UpdatePanel is defined as
    <asp:UpdatePanel ID="upEmulation" runat="server" ChildrenAsTriggers="True" UpdateMode="Conditional">
    The button that initiates the callback is NOT a child of the UpdatePanel, but my custom control is a child of the UpdatePanel.

    The hosting page is defined as
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="InteractiveDemoPage.aspx.cs" Inherits="InteractiveDemoPage" %>
    The code behind does not do special registrations or anything about Callbacks and Postbacks.

    Thursday, August 25, 2011 2:54 PM

All replies

  • User3866881 posted

    Hello royC:)

    Would you mind showing us your detailled codes of your customized control?

    Thx again

    Saturday, August 27, 2011 1:21 AM
  • User-16411453 posted

    return false, will not fire the server side function

    return true will fire the server side function

    I don't care if the user is not using javascript, so I use the javascript.


    Dim ib_TopProductSquare_GeneralMedia As ImageButton
                    ib_TopProductSquare_GeneralMedia = New ImageButton
                    With ib_TopProductSquare_GeneralMedia
                        .ID = [ID] & "_ib_TopProductSquare_GM_Ajax_" & idx.ToString
                        .ImageAlign = ImageAlign.Middle
                        .ImageUrl = [GeneralMedia_0_ButtonUrl]
                        .AlternateText = "Videos / Movies"
                    End With
                    If GeneralMedia_ID > -1 Then
                        ib_TopProductSquare_GeneralMedia.OnClientClick = "return sm_display_Modal_Video('" & PartNumber & "');"
                        ib_TopProductSquare_GeneralMedia.ImageUrl = [GeneralMedia_1_ButtonUrl]
                    End If
    //Javascript function load_gmVideo_Data(part_Number_value) { $(document).ready(function () { }); return false; }
    Saturday, August 27, 2011 1:23 AM
  • User1064158201 posted

    Are you suggesting that I should return false from the Javascript function that calls the CALLBACK function to prevent the subsequent POSTBACK?

    Monday, August 29, 2011 7:22 AM
  • User1064158201 posted

    Not excatly sure what parts you might want to see.  Belows is my control's OnLoad showing the Javascript functions that are inserted into the output page.  Below that is part of the page form showing the HTML button (Magic Button) that invokes the CALLBACK and the UpdatePanel with my control.

    		protected override void OnLoad (EventArgs e)
    			string		callback_reference;
    			if (! Page.IsCallback)
    #if ImplementingCallbacks
    				ec_callback_response = new CallbackResponse();
    				callback_reference = Page.ClientScript.GetCallbackEventReference(this,
    					"xmitLocation", "ReceiveXmitResult", "context", "OnEmulationControlError", false);
    				Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SendXmitFunction",
    					"function SendXmitFunction(xmitLocation,context)" + "{" + callback_reference + ";}" + Environment.NewLine, true);
    				Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "XmitFromCursor",
    					"function XmitFromCursor(context){SendXmitFunction(Sys.Serialization.JavaScriptSerializer.serialize(new CallbackRequest()),context);}" + Environment.NewLine, true);
    				Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "XmitFromEndOfScreen",
    					"function XmitFromEndOfScreen(context){SendXmitFunction('fromEndOfScreen',context);}" + Environment.NewLine, true);
     *			Setup default styles such that the "only" styles will match the standard style
     * */
    				if (FieldDataOnlyCssClass == null)
    					FieldDataOnlyCssClass = FieldDataCssClass;
    				if (FieldLabelOnlyCssClass == null)
    					FieldLabelOnlyCssClass = FieldLabelCssClass;
    		}		/* method EmulationClient OnLoad */


    	<form id="form1" runat="server">
    		<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    				<asp:ScriptReference Assembly="KMSYSASPNetEmulationControl" Name="KMS.Emulation.Web.Controls.HelperFunctions.js" />
    				<asp:ScriptReference Assembly="KMSYSASPNetEmulationControl" Name="KMS.Emulation.Web.Controls.EmulationClient.js" />
    		<input type="submit" runat="server" id="magicButton" value="Magic Button" onclick="XmitFromCursor()" />
    			<asp:UpdatePanel ID="upEmulation" runat="server" ChildrenAsTriggers="True" UpdateMode="Conditional">
    					<asp:Button ID="btn_Connect" Style="margin: 5px 20px 5px 0px" Text="Connect" OnClick="onConnect" runat="server" />
    					<asp:Button ID="btn_Disconnect" Style="margin: 5px 20px 5px 0px" runat="server" Text="Disconnect" OnClick="onDisconnect" />
    					<asp:Button ID="btn_ClearScreen" Style="margin: 5px 20px 5px 30px" runat="server" Text="ClearScreen" OnClick="onClearScreen" />
    					<asp:Button ID="btn_Home" runat="server" Style="margin: 5px 20px 5px 0px" Text="Cursor to Home" OnClick="onHome" />
    					<input type="button" style="margin: 5px 20px 5px 30px" id="btn_sendscreen" onclick="onSendScreen();" value="Send Screen via client JScript" />
    					<p>Send this text from home position:
    						<asp:TextBox ID="tb_Input" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em"  Columns="80" />
    						<asp:Button ID="btn_Send" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em"  Text="Send" OnClick="onSend"
    							ToolTip="Sends text from home (1,1) position"/></p>
    					<p>Enter a field number:
    					<asp:TextBox ID="tb_FieldNumber" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em" Columns="3" />
    					<asp:Button ID="btn_Get" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em" Text="GetField" OnClick="onGet" />
    					<asp:Button ID="btn_Set" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em" Text="SetField" OnClick="onSet" />
    					<asp:TextBox ID="tb_FieldText" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em"  Columns="80" /></p>
    					<p>Enter text to "type" into the current screen position:
    					<asp:TextBox ID="tb_Keys" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em" />
    					<asp:Button ID="btn_SubmitText" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px .5em" Text="Type & Xmit"
    						OnClick="onTypeAndXmit" ToolTip="Simulates user typig into the screen and hitting the transmit key" />
    					<asp:Button ID="btn_Type" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px 3em" Text="Type" OnClick="onType"
    						ToolTip="Simulates user typing into the screen"/>
    					<asp:Button ID="btn_Xmit" runat="server" Style="vertical-align:bottom; margin:0px 1.5em 0px 2em"
    						Text="Xmit" OnClick="onXmitFromCursor" ToolTip="Transmit from current cursor location"/></p>
    					<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="250">
    							Operation in progress...&nbsp;&nbsp;&nbsp;Please wait
    					<asp:Label ID="lbl_FieldValue" runat="server" Style="margin: 5px 15px 5px 10px" Text=""/>
    					<KMSASPNetEmulation:EmulationClient ID="EmulationClient1" runat="server" EnableViewState="False" ShowDebugScreen="True"
    						AutoGenerateScreen="false" ShowDebugTrace="True" PreXmit="onPreXmit"/>
    Monday, August 29, 2011 7:32 AM