Problem using Placeholder

3 posts, 0 answers
  1. Pat
    Pat avatar
    14 posts
    Member since:
    Apr 2011

    Posted 29 Apr 2011 Link to this post

    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. Gimmik
    Gimmik avatar
    170 posts
    Member since:
    May 2011

    Posted 03 May 2011 Link to this post

    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>
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Pat
    Pat avatar
    14 posts
    Member since:
    Apr 2011

    Posted 04 May 2011 Link to this post

    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
Back to Top