Custom control : getting Rowcount 0 on button event of the control.

Last post 11-29-2009 11:19 PM by jkirkerx. 2 replies.

Sort Posts:

  • Custom control : getting Rowcount 0 on button event of the control.

    11-06-2009, 1:38 AM
    • Contributor
      2,576 point Contributor
    • imran_khan
    • Member since 03-11-2008, 7:57 AM
    • Ahmedabad
    • Posts 489

    I am working on custom control. I am creating databound control. I have one property name RowDataSource that
    accept DataRowCollection as data source and I bind control on OnDataBinding event. I have another property
    name RowItem.
    My code looks like this.

    public DataRowCollection RowDataSource
    {
    	get
        {
    		if (ViewState["rowDataSource"] == null)
    		{
    	        ViewState["rowDataSource"] = null;
    		}
            return (DataRowCollection)ViewState["rowDataSource"];
        }
    	set
        {
    		if (value == null)
    		{
    			throw new ArgumentException("unvalid data source.", this.ID);
    		}
    		ViewState["rowDataSource"] = value;
    	}
    }
    public RowCollections ExelRows
    {
       get
       {
       // rCollection is object of RowCollections class
    	if (rCollection == null)
            {
               rCollection = new RowCollections();
            }
            return rCollection;
    
       }
    }
    
    //OnDataBinding event code
    protected override void OnDataBinding(EventArgs e)
    {
    	base.OnDataBinding(e);
    	if (RowDataSource != null)
    	{
    		foreach (DataRow dr in RowDataSource)
    		{
    		    //RowItem is my class
    			RowItem item = new RowItem();
    			item.Text = dr[rTitle].ToString();
    			this.ExelRows.Add(item);
    		}
    	}
    }
       
    //CreateChildControls method to render control		 
    protected override void CreateChildControls()
    {
    	if (ExelRows.Count > 0)
    	{
    		//here i am rendering table structure to add exelrows and create control.
    	}
    }
    


    Now, I am binding this control with DataRowCollection and It works file. I have one button in this control and
    I am raising bubble event when button is clicked.

    My problem is that when I click on button then CreateChildControls method is called first before OnDataBinding event.
    so I am getting ExelRows.Count = 0.

    so what I have to do to getting all rows on every bubble event of control.

    regard

    imrankhan

    Imrankhan
    -----------------------------------------------------
    Always remember to mark as answer on the post that helped you.

    My Blog :
    http://aspnet-solutions.blogspot.com/
    http://javascriptsolution.blogspot.com/
  • Re: Custom control : getting Rowcount 0 on button event of the control.

    11-12-2009, 9:10 PM
    Answer

    Hi,

    You cannot get rows if they are not populated yet. In general we can get all populated controls in PreRender event. You can add your code logic there. 

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Custom control : getting Rowcount 0 on button event of the control.

    11-29-2009, 11:19 PM
    • Participant
      1,892 point Participant
    • jkirkerx
    • Member since 12-07-2007, 2:52 AM
    • Huntington Beach CA
    • Posts 442

    CreateChildControls just renders the control to the screen. After rendering, call Protected Sub OnLoad to perform logic

Page 1 of 1 (3 items)