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

null grid in javascript function

1 Answer 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Elliott
Top achievements
Rank 2
Elliott asked on 06 Jun 2011, 09:33 PM
I can't seem to get to the grid in a function
the function throws a null object error
        <telerik:RadGrid ID="rgEditOrder" OnNeedDataSource="rgEditOrder_NeedDataSource" OnItemCommand="rgEditOrder_ItemCommand" OnItemUpdated="rgEditOrder_UpdateRow" OnItemCreated="rgEditOrder_OnItemCreated" runat="server">
        <GroupingSettings CaseSensitive="false" />
        <MasterTableView DataKeyNames="OrderSeq" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" AllowFilteringByColumn="False" EditMode="InPlace" >
        <Columns>
            <telerik:GridBoundColumn UniqueName="StoreNumber" DataField="StoreNumber" HeaderText="Store #" DataFormatString="{0:#####}" DataType="System.Int32" ReadOnly="True">
                <HeaderStyle Width="40px" />
            </telerik:GridBoundColumn>
a bunch of columns
            <telerik:GridTemplateColumn UniqueName="Qty" HeaderText="Cases" >
                <ItemTemplate>
                    <asp:Label ID="lblQty" Text='<%# Bind("Qty") %>' Width="40px" runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadNumericTextBox ID="rntbQty" Text='<%# Eval("Qty") %>' MinValue='<%# Bind("Qty") %>' Width="40px" runat="server">
                        <NumberFormat DecimalDigits="0" />
                    </telerik:RadNumericTextBox>
                </EditItemTemplate>
                <HeaderStyle Width="40px" />
            </telerik:GridTemplateColumn>
more columns
            <telerik:GridBoundColumn UniqueName="OrderSeq" DataField="OrderSeq" DataType="System.Int64" Visible="false">
            </telerik:GridBoundColumn>
        </Columns>
        </MasterTableView>
        <ClientSettings EnablePostBackOnRowClick="true" >
            <ClientEvents OnRowDblClick="RowDblClick" />
        </ClientSettings>
        </telerik:RadGrid>
<head id="Head1" runat="server">
<title>Edit Order</title>
<script language="javascript" type="text/javascript">
<!--
// this function handles the onbeforeprint event of the body object
function window_onbeforeprint()
{
    // idprint is the id assigned to the div tag of the javascript
    // injected into the response object in the code behind which
    // actually prints the screen
    var divEle = document.getElementById("idPrint");
    // remove the display=none i.e. show the div element
    if (divEle != null)
    {
         divEle.style.display = "";
    }
 
    // hide the div elements
    idShow.style.display="none";
  //  idShow.style.display="none";
  //  idShow1.style.display="none";
    idShow1.style.display="none";
    idHeader.style.display="none";
}
// this function handles the onafterprint event of the body object
function window_onafterprint()
{
    // idprint is the id assigned to the div tag of the javascript
    // injected into the response object in the code behind which
    // actually prints the screen
    var divEle = document.getElementById("idPrint");
    // hide the div element
    if (divEle != null)
    {
         divEle.style.display = "none";
    }
 
    // remove the style.display='none'-show the div element
    idShow.style.display="";
 //   idShow1.style.display="";
    idShow1.style.display="";
    idHeader.style.display="";
}
var idx = 0;
function RowDblClick(sender, eventArgs) {
    idx = eventArgs.get_itemIndexHierarchical();
    sender.get_masterTableView().editItem(idx);
}
 
// this function is called on the click event of the Finalize Order button
// it is wired to the button in the page load event  
function check_input() {
    // both the first and last name need to be entered-if either of them is not
    // popup message box and cancel update
    var strName = document.getElementById('txtFirstName');
    if (trim(strName.value) == "") {
        alert("Please Enter First Name");
        return (false);
 
    }
    strName = document.getElementById('txtLastName');
    if (trim(strName.value) == "") {
        alert("Please Enter Last Name");
        return (false);
    }
    return (true);
}
// use regular expression to format first or last name
// per Derek-remove any imbedded white space
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}
// this functiom was added because the code behind is looking for it 
function PrintPage() {
    return true;
}
-->
</script>
</head>
protected void rgEditOrder_OnItemCreated(object source, GridItemEventArgs e)
{
    StringBuilder sb;
    GridEditableItem geItem;
    TableCell tcQty;
    RadNumericTextBox rntbQty;
    char DoubleQuote;
    string EscapeChar,str;
     
    DoubleQuote = (char) 34;
    sb = new StringBuilder(DoubleQuote);
    EscapeChar = sb.ToString();
    str = DoubleQuote.ToString();
    if (e.Item.IsInEditMode)
    { }
    else
        return;
 
    if (e.Item is GridEditableItem)
    { }
    else
        return;
 
    geItem = (GridEditableItem)(e.Item);
    sb = new StringBuilder("<script language=");
    sb.Append(@"'");
    sb.Append("javascript");
    sb.Append(@"'");
    sb.Append(" ");
    sb.Append(@"'");
    sb.Append("text/javascript");
    sb.Append(@"'");
    sb.Append(">");
    sb.Append("function QtyOnValueChanged(sender, eventArgs) { ");
    sb.Append("alert(");
    sb.Append(@"'");
    sb.Append("gorkomatic");
    sb.Append(@"'");
    sb.Append("); ");
    sb.Append("var grid = $find(");
    sb.Append(@"'");
    sb.Append("<%= ");
    sb.Append("rgEditOrder.ClientID");
    sb.Append(" %>");
    sb.Append(@"'");
    sb.Append(").get_masterTableView(); ");
    sb.Append("grid.updateItem(grid.get_dataItems()[");
    sb.Append(geItem.DataSetIndex.ToString());
    sb.Append("].get_element()); ");
    sb.Append("alert(");
    sb.Append(@"'");
    sb.Append("qty on value changed");
    sb.Append(@"'");
    sb.Append("); }");
    sb.Append("</script>");
    str = sb.ToString();
    rcBlock.Controls.Add(new LiteralControl(str));
 //   ScriptManager.RegisterStartupScript(this,this.GetType(), "Update", str, false);
     
    tcQty = geItem.Cells[1];
    rntbQty = (RadNumericTextBox)tcQty.FindControl("rntbQty");
    if (rntbQty == null)
        return;
    rntbQty.ClientEvents.OnValueChanged = "QtyOnValueChanged";
}

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 07 Jun 2011, 10:00 AM
Hello Marianne,

You cannot add a code expression to the page programmatically as you do with the StringBuilder:

sb.Append("var grid = $find(");
    sb.Append(@"'");
    sb.Append("<%= ");
    sb.Append("rgEditOrder.ClientID");
    sb.Append(" %>");
    sb.Append(@"'");

Instead, you can directly output the ClientID of the control:

sb.Append("var grid = $find(");
sb.Append(@"'");
sb.Append(rgEditOrder.ClientID);
sb.Append(@"'");

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Elliott
Top achievements
Rank 2
Answers by
Veli
Telerik team
Share this question
or