Avatar of spaceplanner
spaceplanner
 asked on

referencing controls in a datagrid query

I am trying to make changes to an existing asp.net web app and am having problems identifying controls (I am a newbie from asp)
The relevant code is :

<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
      AutoGenerateColumns="False" CellPadding="0">
      <ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></ItemStyle>
      <Columns>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <IMG height="1" src="Images/clear.gif" width="10" border="0">
                        <asp:Label id="lblUser" runat="server" CssClass="txtSmall"></asp:Label>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:LinkButton>
                        <asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:Label>
                        <asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.gif" ToolTip="Additionl user must be removed before desk can be made OPEN/SPARE."></asp:ImageButton>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_ChangeUser.gif" ToolTip="Change the Primary user of this Desk."></asp:ImageButton>
                        <asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_removeUser.gif" ToolTip="Remove this person as an additional user at this Desk."></asp:ImageButton>
                        <asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_adduser.gif" ToolTip="Add an additional Person to this Desk. The Order of the occupants can be sorted after the user is added."></asp:ImageButton>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <asp:ImageButton id="btnLGeneric" runat="server" ImageUrl="Images/btn_generic.gif" ToolTip="Add a Generic Use to this Desk . i.e. HOTDESK, VISITOR etc."></asp:ImageButton>
                        <asp:ImageButton id="btnLMakePrime" runat="server" ImageUrl="Images/btn_makePrime.gif"></asp:ImageButton>
                        <IMG height="1" src="Images/clear.gif" width="10" border="0">
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn Visible="False" DataField="msfwid"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="rowid"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="WholeName"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="PrimeUser"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="Locked"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="Generic"></asp:BoundColumn>
      </Columns>
</asp:DataGrid>
My problem is that I don't understand the code that governs this datagrid. There are a complex set of rules as to which of the buttons should show, depending on the data retrieved. Unfortunately the original developers did not annotate the work so it is difficult for me to follow. A sample of the code behind is :

if(dgLocation.Items.Count == 2)
{
     if((((LinkButton)dgLocation.Items[0].Cells[1].Controls[1]).Text.ToUpper() !="OPEN")&&
          (((Label)dgLocation.Items[0].Cells[1].Controls[3]).Text.ToUpper() !="OPEN"))
     {
          dgLocation.Items[0].Cells[1].Controls[5].Visible = true;                                  
         
          if(dgLocation.Items[0].Cells[8].Text == "True")
               dgLocation.Items[0].Cells[2].Controls[1].Visible = false;                                                            
          else
               dgLocation.Items[0].Cells[2].Controls[1].Visible = true;

          ((ImageButton)dgLocation.Items[0].Cells[2].Controls[1]).Attributes.Add("onclick","popPersonnelList('"+ dgLocation.Items[0].Cells[4].Text +"','"+ ((Label)(dgLocation.Items[0].Cells[0].Controls[1])).Text.Replace(":","") +"','1')");
          dgLocation.Items[0].Cells[2].Controls[3].Visible = false;
          dgLocation.Items[0].Cells[2].Controls[5].Visible = false;
         
          btnEditCC.Visible = false;
     }                                        
     else
     {
          blCheck = true;
          dgLocation.Items[0].Cells[1].Controls[5].Visible = false;                                  
         
          dgLocation.Items[0].Cells[2].Controls[1].Visible = false;                                                                                                        
          dgLocation.Items[0].Cells[2].Controls[3].Visible = false;
          dgLocation.Items[0].Cells[2].Controls[5].Visible = true;
          ((ImageButton)dgLocation.Items[0].Cells[2].Controls[5]).Attributes.Add("onclick","popPersonnelList('"+ dgLocation.Items[0].Cells[4].Text +"','"+ ((Label)(dgLocation.Items[0].Cells[0].Controls[1])).Text.Replace(":","") +"','1')");

          dgLocation.Items[1].Cells[0].Controls[1].Visible = false;                                                                                    
          dgLocation.Items[1].Cells[1].Controls[1].Visible = false;
          dgLocation.Items[1].Cells[1].Controls[3].Visible = false;
          dgLocation.Items[1].Cells[1].Controls[5].Visible = false;                                        
          dgLocation.Items[1].Cells[2].Controls[1].Visible = false;
          dgLocation.Items[1].Cells[2].Controls[3].Visible = false;
          dgLocation.Items[1].Cells[2].Controls[5].Visible = false;                                        
          dgLocation.Items[1].Cells[3].Controls[1].Visible = false;
          dgLocation.Items[1].Cells[3].Controls[3].Visible = false;                                                                                                        
         
          btnEditCC.Visible = true;

          btnEditCC.Attributes.Add("onclick","popCostCenterOptions('"+ lblAddress.Text +"','"+ ((Label)dgLocation.Items[0].Cells[1].Controls[3]).Text.ToUpper() +"')");
     }
}
else
{
     dgLocation.Items[0].Cells[1].Controls[5].Visible = false;                              
     
     if(dgLocation.Items[0].Cells[8].Text == "True")
          dgLocation.Items[0].Cells[2].Controls[1].Visible = false;                                                            
     else
          dgLocation.Items[0].Cells[2].Controls[1].Visible = true;
     
     ((ImageButton)dgLocation.Items[0].Cells[2].Controls[1]).Attributes.Add("onclick","popPersonnelList('"+ dgLocation.Items[0].Cells[4].Text +"','"+ ((Label)(dgLocation.Items[0].Cells[0].Controls[1])).Text.Replace(":","") +"','1')");
     dgLocation.Items[0].Cells[2].Controls[3].Visible = false;
     dgLocation.Items[0].Cells[2].Controls[5].Visible = false;

     btnEditCC.Visible = false;
}

Please can someone explain to me how the controls are numbered. I notice that they all have an index number of 1,3 or 5. There is never a reference to 2 nor 4. How does the Item[].Cell[].Control[] system work?

Thanks in advance
Paul
ASP.NET

Avatar of undefined
Last Comment
spaceplanner

8/22/2022 - Mon
Edwin_C

Hi Paul

Items[n]  means the n+1 th row in the datagrid.  E.g. Items[0] is the 1st row.

Cells[m]  means the m+1 th table cell in the row.  E.g. Items[0].Cells[0] is the first cell of the first row.

Controls[q] means the q+1 the web control in a cell.  Since ASP.NET datagrid automatically insert a literal control in a cell, the user control always start from Controls[1].  E.g. Items[0].Cells[0].Controls[1] means the 1st user control appeared in the 1st cell of the 1st row.

<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
     AutoGenerateColumns="False" CellPadding="0">
     <ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></ItemStyle>
     <Columns>
          <asp:TemplateColumn>  <<==== this is the start of the 1st column i.e. 1st cell of a row.
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <IMG height="1" src="Images/clear.gif" width="10" border="0">  <<== this is the first control
                    <asp:Label id="lblUser" runat="server" CssClass="txtSmall"></asp:Label>
               </ItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn>
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:LinkButton>  <<== Items[0].Cells[1].Controls[1]
                    <asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:Label>  
                    <asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.gif" ToolTip="Additionl user must be removed before desk can be made OPEN/SPARE."></asp:ImageButton>
               </ItemTemplate>
          </asp:TemplateColumn>
 

Hope that helps

Edwin
spaceplanner

ASKER
Hi Edwin,
Thanks for your swift response.

Does ASP.NET datagrid automatically insert a literal control before each user control?

So that, in the 3rd TemplateColumn

<asp:TemplateColumn>
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_ChangeUser.gif" ToolTip="Change the Primary user of this Desk."></asp:ImageButton><<== Items[0].Cells[2].Controls[1]

                    <asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_removeUser.gif" ToolTip="Remove this person as an additional user at this Desk."></asp:ImageButton><<== Items[0].Cells[2].Controls[3]

                    <asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_adduser.gif" ToolTip="Add an additional Person to this Desk. The Order of the occupants can be sorted after the user is added."></asp:ImageButton><<== Items[0].Cells[2].Controls[5]

               </ItemTemplate>
          </asp:TemplateColumn>


Also, in this example, which control is Item[n].Cell[1].Control[5] ?

thanks in advance
Paul
ASKER CERTIFIED SOLUTION
Edwin_C

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
spaceplanner

ASKER
Thanks very much for your help Edwin. I think I understand it now

regards
Paul
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23