This is a migrated thread and some comments may be shown as answers.

Problems with usercontrol

6 Answers 225 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ManniAT
Top achievements
Rank 2
ManniAT asked on 11 Jul 2009, 08:18 PM
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


6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 15 Jul 2009, 01:57 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 15 Jul 2009, 02:37 PM
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
0
Veli
Telerik team
answered on 17 Jul 2009, 01:09 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 17 Jul 2009, 01:13 PM
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...
0
Accepted
Nikolay Rusev
Telerik team
answered on 23 Jul 2009, 06:17 AM
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.
0
ManniAT
Top achievements
Rank 2
answered on 23 Jul 2009, 11:34 AM
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
Tags
Grid
Asked by
ManniAT
Top achievements
Rank 2
Answers by
Veli
Telerik team
ManniAT
Top achievements
Rank 2
Nikolay Rusev
Telerik team
Share this question
or