troubleshooting Question

referencing controls in a datagrid query

Avatar of spaceplanner
spaceplanner asked on
ASP.NET
4 Comments1 Solution291 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Edwin_C

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros