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

$find not working on programmatically created grids

2 Answers 25 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 05 Aug 2014, 09:20 AM
Hi Telerik,

I've created a page which I populate all grids on the page programmatically in c# and following this demo to print the grids; however as my grids are created on pageload, when typing the javascript code in front end (aspx), the script cannot detect the grids and returning a "The Name 'RadGrid1' does not exist in current content" error. I've tried calling it by string name in the $find function and also tried document.FindElementById, but no luck. Could anyone give me some advice on this issue?

Many thanks.

Attached the code below:
function Print() {
    var previewWindow = window.open('about:blank', '', '', false);
    var styleSheet = '<%= Telerik.Web.SkinRegistrar.GetWebResourceUrl(this, RadGrid1.GetType(), String.Format("Telerik.Web.UI.Skins.{0}.Grid.{0}.css", RadGrid1.Skin)) %>';
    var baseStyleSheet = '<%= Telerik.Web.SkinRegistrar.GetWebResourceUrl(this, RadGrid1.GetType(), "Telerik.Web.UI.Skins.Grid.css") %>';
    var htmlContent = "<html><head><link href = '" + styleSheet + "' rel='stylesheet' type='text/css'></link>";
    htmlContent += "<link href = '" + baseStyleSheet + "' rel='stylesheet' type='text/css'></link></head><body>";
 
    for (i = 1; i < 13; i++) {
        var CYGrid = "RadGrid_CY_Strategy_" + String(i);
        var CYradGrid = $find('<%=CYGrid.ClientID %>'); //This is not working well, hence below
 
        htmlContent += getOuterHTML(CYradGrid.get_element());
        if (LYradGrid.hidden == false) {
            htmlContent += getOuterHTML(LYradGrid.get_element());
        }
    }
    htmlContent += "</body></html>";
    previewWindow.document.open();
    previewWindow.document.write(htmlContent);
    previewWindow.document.close();
    previewWindow.print();
 
    if (!$telerik.isChrome) {
        previewWindow.close();
}
 
    function getOuterHTML(obj) {
    if (typeof (obj.outerHTML) == "undefined") {
        var divWrapper = document.createElement("div");
        var copyOb = obj.cloneNode(true);
        divWrapper.appendChild(copyOb);
        return divWrapper.innerHTML
    }
    else {
        return obj.outerHTML;
    }
}


2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 05 Aug 2014, 09:43 AM
Hi Tim,

Since you are creating the grid from code behind it will not be directly accessible in clientside. You can attach the button's client click event in pageload from code behind and pass the RadGrid ClientID with it.

C#:
btnPrint.Attributes.Add("OnClick", "Print('" + RadGrid1.ClientID + "');return false;");

JS:
<script type="text/javascript">
 function Print(gridId) {      
     var grid = $find(gridId);
     var MasterTable = grid.get_masterTableView();
     //Your code     
 }
</script>

Thanks,
Princy
0
Daniel
Telerik team
answered on 07 Aug 2014, 12:46 PM
Hello Tim,

Here is what you can try:
var CYradGrid = $telerik.findControl(document, CYGrid);
...

Telerik Static Client Library

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or