views:

416

answers:

1

I'm new to this ASP.NET stuff. In my page I have a Datalist with a FooterTemplate. In the footer I have a couple panels that will be visible depending on the QueryString. The problem I am having is trying to find these panels on Page_Load to change the Visible Property. Is there a way to find this control in the Page_Load? For example this is part of the aspx page:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 <asp:DataList ID="dlRecords" runat="server">
  <FooterTemplate>
   <asp:Panel ID="pnlArticleHeader" runat="server" Visible="false" >
   </asp:Panel>
  </FooterTemplate>
 </asp:Datalist>
</asp:Content>

Here is something in the codebehind:

protected void Page_Load(object sender, EventArgs e)
    {
        location = Request.QueryString["location"];
        if (location == "HERE")
        {
          Panel pnlAH = *Need to find control here*;
          pnlAH.Visible=true;
         }
      }

Like I said I am new at this. Everything I have found doesn't seem to work so I decided to post a specific question. Thanks in advance

A: 

DataList has event OnItemCreated, overriding allows select type of row:

  Panel _pnlArticleHeader;
  void Item_Created(Object sender, DataListItemEventArgs e)
  {

     if (e.Item.ItemType == ListItemType.Footer)
     {

        _pnlArticleHeader =(Panel)e.Item.FindControl("pnlArticleHeader");
      }

  }

After event invocation in the field: _pnlArticleHeader you will get desired panel. This way is safe since created only once. NOTE! same way for common DataList's row would return only last one.

Dewfy
This is the first one I have gotten to work. Thanks. The only issue I have now is how I use it in my code. For example in the Page_Load, I get the QueryString location. Depending on the value, I have different blocks of code. I'd like to put this piece within that code instead of a separate OnItemCreated code block. How would I get this done?
SDC
Maybe I just don't understand how/when the datalist is built. Maybe I'm just showing my ignorance on how this works. Is it possible to get it outside of OnItemCreated?
SDC
@SDC To accomplish this you place this handler 'Item_Created' into the same file where Page_Load is. This event will be executed before Page_load, so variable _pnlArticleHeader will be assigned. In the aspx file for control you need declare event handler:<asp:DataList ID="dlRecords" runat="server" OnItemCreated="Item_Created" That's all
Dewfy