Problems with usercontrol

7 posts, 1 answers
  1. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 11 Jul 2009 Link to this post

    Hi,

    I have a web-usercontrol which works fine - on a normal (ajax enabled) page.
    But when I place the control in a Grid's Edit FormTemplate the emitted script no longer works.
    The control is only used in edit mode - and I looked around a lot here - but I could not get it to work.
    By the way - the script is in the source of the page - but it is somehow ignored.

    My Control renders some script in it's OnPreRender handler.
    The approach is to add an "OnLoad" handler for a contained control.
    Again - it works well as long as it is not in a grid.

    Here is my (I tried what ever I found and could imagine) PreRender function:
    protected override void OnPreRender(EventArgs e) {  
        string strScript="alert('Hello');";  
        base.OnPreRender(e);  
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyBlock" + ClientID, strScript, true);  
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "MyBlock1" + ClientID, strScript, true);  
        RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "MyBlock2" + ClientID, strScript, true);  
        aM.ResponseScripts.Add("<script type=\"text/javascript\">\r\n" + strScript + "\r\n</script>");  
        this.Controls.Add(new LiteralControl("<script type=\"text/javascript\">\r\n" + strScript + "\r\n</script>"));  
            //rtBox.ClientEvents.OnLoad = "OnLoad_" + ClientID;  
    To keep the things simple - I used just an alert to this script - which should alter when the script is seen by the client.
    But it does nothing!!
    Although the alert is there in source code!!

    Being a bit frustrated - I tried the "direct approach" and changed my control to this:
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WSTextBox.ascx.cs" Inherits="VJF09.Classes.WSTextBox" %> 
    <script type="text/javascript">  
        function OnThisloaded() {  
            alert("Tet");  
        }  
    </script> 
    <telerik:RadTextBox runat="server" ID="rtBox" ClientEvents-OnLoad="OnThisloaded">  
    </telerik:RadTextBox> 
     
     
    The result - after I switch to edit I get an error message: "Onthisloaded" not found.

    Regards

    Manfred


  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 15 Jul 2009 Link to this post

    Hi ManniAT,

    When you ajaxify your RadGrid and load client scripts after initial postback (just like what happens when you load a user control with scripts on ajax request), your scripts are not properly registered on page.

    Fixing this is straightforward. Simply put your user control scripts in a RadScriptBlock control.

    All the best,
    Veli
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 15 Jul 2009 Link to this post

    Hi Veli,

    I tried your approach - and it did not help anyhow.

    Again I use the simple approach I did before - now decorating my script with a RadScriptBlock
    It looks like this now:
    string strSCR = "<telerik:RadScriptBlock ID='RadScriptBlock133' runat='server'><script type=\"text/javascript\">alert('hello');\r\n</script>\r\n</telerik:RadScriptBlock>";  
    //this.Controls.Add(new LiteralControl(strSCR));  
    RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "aa" + ClientID, strSCR, true);  
     
    I guess that this CAN not work - since adding server controls via rendering can't work.

    Of course I could add a RadScriptBlock to my usercontrols ASCX - but how can I inject my script in this block.
    As you may have seen from my first post the script is generated at runtime.

    Regards

    Manfred
  5. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 17 Jul 2009 Link to this post

    Hi ManniAT,

    No, you cannot register your scripts by adding "<telerik:RadCodeBlock" type string to the page. You will need to declare your RadCodeBlock in the markup, and place your scripts again in the markup in <script> tags.

    Best wishes,
    Veli
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 17 Jul 2009 Link to this post

    OK, thats what I thougt. (the RadCodeBlock can't be "injected").

    But anyhow - this does not solve my problem.
    The script can't be placed in markup - it contains values only available at runtime.

    Any idea how to "inject" a script block to an existing RadCodeBlock?
    It  has no "InnerText" or an other property like this...
  7. Answer
    Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2284 posts

    Posted 23 Jul 2009 Link to this post

    Hello Manni,

    Please review the code in the attached project. It demonstrates both your scenarios described initially.
    You can also check the following article:
    http://www.telerik.com/help/aspnet-ajax/ajxradscriptblockradcodeblock.html

    All the best,
    Nikolay
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 23 Jul 2009 Link to this post

    Hi Nikolay,

    thanks a lot - your sample let me find my problem.
    My code was:
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyBlock" + ClientID, strScript, true);  
    rtBox.ClientEvents.OnLoad = "OnLoad_" + ClientID;  
     
    I changed it to:
    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyBlock" + ClientID, strScript, true);  
    rtBox.ClientEvents.OnLoad = "OnLoad_" + ClientID;  
     
    and now it works.
    The thing which made troubles - on a normal page the first solution works.
    In an ajaxified thing it fails :)

    By the way -- playing around I was always there (code from above):
    RadScriptManager.RegisterClientScriptBlock(this, this.GetType(), "aa" + ClientID, strSCR, true);     
        
     
    But my mistake - I registered with the control instead of with the page :)

    Kind Regards

    Manfred
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017