More in this section

Forums / Developing with Sitefinity / Accessing base.ListCnt in ControlPanel

Accessing base.ListCnt in ControlPanel

8 posts, 0 answered
  1. Carl
    Carl avatar
    23 posts
    Registered:
    15 Jul 2008
    17 Oct 2008
    Link to this post
    Hi, it's me again, with another question that I'm sure that I'll figure out after asking.

    I'm trying to access the Gridview thats on the list page of my custom Generic Content module, and add an event to a button that I placed inside of the header row. I do not want to change any of the current functionality of teh Gridview.

    I see that I should be able to access the List container from within the base (base.ListCnt), and I see that I should be able to access the GridView from the List container (base.ListCnt.Grid).

    Where would the best place for me to do this in the ControlPanel, if it can be done at all? I've tried the CreateChildControls() (yes, I did try before asking), and placed a switch within it that checks to see if the Mode is List. However, I keep getting an object reference exception on the List container.

    Thanks again,
    Carl J
    http://www.carlj.ca

  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    20 Oct 2008
    Link to this post
    Hi Carl,

    You need to check if base.ListCnt is null in CreateChildControls() method:

    protected override void CreateChildControls() 
        { 
            base.CreateChildControls(); 
     
            if (base.Mode == Modes.List && base.ListCnt != null
            {  
              // Do something 
            } 
        } 

    Hope this helps.

    All the best,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Carl
    Carl avatar
    23 posts
    Registered:
    15 Jul 2008
    20 Oct 2008
    Link to this post
    Hi Pepi,

    That's pretty much what I had, except I was using a switch instead of an if.

    The actual problem it turns out, was when I tried to find a button in the header row. At times the header row doesn't exist, and was throwing an error.

    In case anyone else comes across a problem like this, I'll share my solution.

    Here's the code in my CreateChildControl():

            protected override void CreateChildControls()  
            {  
                base.CreateChildControls();  
     
                if (this.Page != null)  
                {  
                    switch (this.Mode)  
                    {  
                        case Modes.List:  
                            if (base.ListCnt != null)  
                            {  
                                if (base.ListCnt.Grid != null)  
                                {  
                                    base.ListCnt.Grid.RowCreated+=new GridViewRowEventHandler(Grid_RowCreated);  
                                }  
                            }  
                            break;  
                        default:  
                            break;  
                    }  
                }  
            }  
     

    And here's the code for the RowCreated event:

            protected void Grid_RowCreated(object sender, GridViewRowEventArgs e)  
            {  
                if (e.Row.RowType == DataControlRowType.Header)  
                {  
                    LinkButton btn = (LinkButton)e.Row.FindControl("btnDelete");  
     
                    if (btn != null)  
                    {  
                        btn.Click += new System.EventHandler(btnDelete_Click);  
                    }  
                }  
            } 

    As always, thanks again,
    Carl J
    http:www.carlj.ca
  4. Carl
    Carl avatar
    23 posts
    Registered:
    15 Jul 2008
    20 Oct 2008
    Link to this post
    Quick semi off topic question. For the Generic Content Gridviews, how do I access the Id of the row item?

    I need to iterate through each row (done), and retrieve the Id of that item in that row.

    Thanks,
    Carl J
  5. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    22 Oct 2008
    Link to this post
    Hi Carl,

    If you need to access the ID of the item in the handler of RowCreated or RowDataBound, you could use the following:

    if (e.Row.RowType == DataControlRowType.DataRow) 
                { 
                    IContent cnt = (IContent)e.Row.DataItem; 
                    Guid id = cnt.ID; 
                    ... 
                } 

    Otherwise, you should iterate through the collection of GridViewRow objects like this:

    foreach (GridViewRow row in this.ListCnt.Grid.Rows) 
                { 
                    if (row.RowType == DataControlRowType.DataRow) 
                    { 
                        IContent cnt = (IContent)row.DataItem; 
                        Guid id = cnt.ID; 
                    } 
                } 

    Hope this helps.

    All the best,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Carl
    Carl avatar
    23 posts
    Registered:
    15 Jul 2008
    22 Oct 2008
    Link to this post
    Hi Pepi,

    It was the IContent that I was missing.

    Thanks again for all of your help
  7. Carl
    Carl avatar
    23 posts
    Registered:
    15 Jul 2008
    22 Oct 2008
    Link to this post

    It's me once again? Getting sick of me yet? :)

    Just wanted to let you know, that the DataItem in the row is null, when I placed the code inside of btnDelete_Click(object sender, EventArgs e).

    So, instead, I've added the datakeynames="Id" property to the GridView, and here's my code in the ControlPanel.

    private void btnDelete_Click(object sender, EventArgs e)  
    {  
        CheckBox chk = null;  
        if (base.ListCnt != null && base.ListCnt.Grid != null)  
        {  
            foreach (GridViewRow row in base.ListCnt.Grid.Rows)  
            {  
                if (row.RowType == DataControlRowType.DataRow)  
                {  
                    string key = this.ListCnt.Grid.DataKeys[row.RowIndex].Value.ToString();  
                    Guid id = new Guid(key);  
     
                    // Do some top secret stuff with the Id right here ...  
                }  
            }  
        }  
  8. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    23 Oct 2008
    Link to this post
    Hi Carl,

    The DataItem property is only available during and after the RowDataBound event of a GridView control. That is the reason why its value is null in btnDelete_Click method. So the solution provided by you is the right one in this case.

    Best wishes,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
8 posts, 0 answered