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

Problem using Placeholder

2 Answers 316 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pat
Top achievements
Rank 1
Pat asked on 29 Apr 2011, 02:20 PM
I am creating a RadGrid in C# and adding it to a placeholder.

RadGrid RadGrid1 = new RadGrid();
RadGrid1.ID = "RadGrid1";
// Add the RadGrid instance to the controls
this.PlaceHolder1.Controls.Add(RadGrid1);
 
RadGrid1.DataSourceID = "SqlDataSource1";
RadGrid1.MasterTableView.DataKeyNames = new string[] { "Jobnumber" };
RadGrid1.Skin = "Default";
RadGrid1.Width = Unit.Percentage(100);
RadGrid1.PageSize = 15;
RadGrid1.AllowPaging = true;
RadGrid1.AllowSorting = true;
RadGrid1.AllowFilteringByColumn = true;
RadGrid1.AutoGenerateColumns = false;
RadGrid1.GridLines = GridLines.Both;
RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
 
//Add columns
 
GridBoundColumn boundColumn;
 
foreach (DataRow dr in dt.Rows)
{
    //Add column
 
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = dr["DataField"].ToString().Trim();     // Initalize the DataField value.
            boundColumn.HeaderText = dr["HeaderText"].ToString().Trim();   // Initialize the HeaderText field value.
            boundColumn.HtmlEncode = false;
            boundColumn.ReadOnly = true;
            boundColumn.SortExpression = dr["SortExpression"].ToString().Trim();
            boundColumn.HeaderStyle.CssClass = "headerstyle";
            if (dr["align"].ToString().Trim().ToUpper().Equals("RIGHT"))
                boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            if (dr["align"].ToString().Trim().ToUpper().Equals("LEFT"))
                boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            if (dr["align"].ToString().Trim().ToUpper().Equals("CENTER"))
                boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
 
            if (dr["width"].ToString().Length == 0)
                boundColumn.HeaderStyle.Width = 100;
            else
                boundColumn.HeaderStyle.Width = Convert.ToInt32(dr["width"].ToString().Trim());
 
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
 }

<asp:PlaceHolder ID="PlaceHolder1" runat="server" />

The problem I am having is accessing the RadGrid in my other functions.

   protected void btn1AusePrint_Command(GridCommandEventArgs e)
    {
        RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl("RadGrid1");
 
       Grid1.PageSize = 1;                           // Set to one record per page when displaying document
 :
:
}

I get the following error on "Grid1.PageSize = 1;" when I build the solution

Error    1    'System.Web.UI.ControlCollection' does not contain a definition for 'Grid1' and no extension method 'Grid1' accepting a first argument of type 'System.Web.UI.ControlCollection' could be found (are you missing a using directive or an assembly reference?)   

2 Answers, 1 is accepted

Sort by
0
Gimmik
Top achievements
Rank 1
answered on 03 May 2011, 11:17 PM
Hi Pat,

Without a little more context as to how you are setting up your RadGrid, it is hard to know exactly what is causing the error. However, it appears that Grid1 may be getting a null value. This would cause the error you are seeing when you attempt to set the PageSize property.

When your ASPX page fires the  'btn1AusePrint_Command' event, a postback will occur. The code that sets up the RadGrid and adds it to the PlaceHolder will need to be rerun each time that event launches, otherwise the control is "lost." This is a product of the stateless nature of ASP.NET. There are a few ways to ensure that your control is recreated after each postback. The simplest method is to include all the code in the Page_Init event handler. The whole control will be recreated from scratch after each postback and added to the PlaceHolder.

I will include a sample program below that I believe demonstrates these principles. The program includes an ADO.NET object that contains sample data. The object is saved to the session so it won't need to be recreated after the postback. This should be a good starting point for you.

Hope this helps!

-Gimmik

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
 
public partial class Default : System.Web.UI.Page
{
     
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
 
        // Create or load the sample DataSet
        DataSet sampleDS;
        if (Session["SampleDS_Exists"] == null)
        {
            sampleDS = Build_Sample_DS();
            Session["SampleDS_Exists"] = sampleDS;
        }
 
        else
        {
            sampleDS = (DataSet)Session["SampleDS_Exists"];
        }
         
        // Declare your object
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
 
        RadGrid1.AutoGenerateColumns = true;
        RadGrid1.Skin = "Default";
        RadGrid1.Width = Unit.Percentage(100);
        RadGrid1.PageSize = 15;
        RadGrid1.AllowPaging = true;
        RadGrid1.AllowSorting = true;
        RadGrid1.GridLines = GridLines.Both;
        RadGrid1.DataSource = sampleDS;
        RadGrid1.DataBind();
        this.PlaceHolder1.Controls.Add(RadGrid1);
    }
 
    // Build the sample DataSet the first time the page loads.
    protected DataSet Build_Sample_DS()
    {
        DataSet sampleDS = new DataSet("SampleDS");
        DataTable sampleTable = sampleDS.Tables.Add("SampleTable");
        sampleTable.Columns.Add("SampleID", typeof(Int32));
        sampleTable.Columns.Add("SampleQuantity", typeof(Int32));
        sampleTable.Columns.Add("CompanyName", typeof(string));
 
        DataRow sampleRow;
 
        for (int i = 0; i <= 199; i++)
        {
            sampleRow = sampleTable.NewRow();
            sampleRow[0] = i;
            sampleRow[1] = i * 7;
            sampleRow[2] = "CustName" + i.ToString();
            sampleTable.Rows.Add(sampleRow);
        }
 
        return sampleDS;
    }
 
 
    // On-click (or any event) this will set the pagesize to 1
    // and rebind the data.
    protected void TestButton_Click(object sender, EventArgs e)
    {
        RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl("RadGrid1");
        Grid1.PageSize = 1;
        Grid1.Rebind();
    }
}

<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
 
<br />
    <asp:Button ID="TestButton" runat="server" Text="Set Page to 1" OnClick="TestButton_Click" />
</div>
0
Pat
Top achievements
Rank 1
answered on 04 May 2011, 07:26 PM
Gimmik

Thanks for the response.  What ever was causing the problem disappeared this morning when I started up my system.  However I appreciate the feedback and suggestions.

Pat
Tags
Grid
Asked by
Pat
Top achievements
Rank 1
Answers by
Gimmik
Top achievements
Rank 1
Pat
Top achievements
Rank 1
Share this question
or