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"></Item Style>
<Columns>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item Style>
<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"></Item Style>
<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:ImageBu tton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item Style>
<ItemTemplate>
<asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_Chang eUser.gif" ToolTip="Change the Primary user of this Desk."></asp:ImageButton>
<asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_remov eUser.gif" ToolTip="Remove this person as an additional user at this Desk."></asp:ImageButton>
<asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_addus er.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"></Item Style>
<ItemTemplate>
<asp:ImageButton id="btnLGeneric" runat="server" ImageUrl="Images/btn_gener ic.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_makeP rime.gif"> </asp:Imag eButton>
<IMG height="1" src="Images/clear.gif" width="10" border="0">
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="msfwid"></asp:B oundColumn >
<asp:BoundColumn Visible="False" DataField="rowid"></asp:Bo undColumn>
<asp:BoundColumn Visible="False" DataField="WholeName"></as p:BoundCol umn>
<asp:BoundColumn Visible="False" DataField="PrimeUser"></as p:BoundCol umn>
<asp:BoundColumn Visible="False" DataField="Locked"></asp:B oundColumn >
<asp:BoundColumn Visible="False" DataField="Generic"></asp: BoundColum n>
</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)dgLocatio n.Items[0] .Cells[1]. Controls[1 ]).Text.To Upper() !="OPEN")&&
(((Label)dgLocation.Items[ 0].Cells[1 ].Controls [3]).Text. ToUpper() !="OPEN"))
{
dgLocation.Items[0].Cells[ 1].Control s[5].Visib le = true;
if(dgLocation.Items[0].Cel ls[8].Text == "True")
dgLocation.Items[0].Cells[ 2].Control s[1].Visib le = false;
else
dgLocation.Items[0].Cells[ 2].Control s[1].Visib le = true;
((ImageButton)dgLocation.I tems[0].Ce lls[2].Con trols[1]). Attributes .Add("oncl ick","popP ersonnelLi st('"+ dgLocation.Items[0].Cells[ 4].Text +"','"+ ((Label)(dgLocation.Items[ 0].Cells[0 ].Controls [1])).Text .Replace(" :","") +"','1')");
dgLocation.Items[0].Cells[ 2].Control s[3].Visib le = false;
dgLocation.Items[0].Cells[ 2].Control s[5].Visib le = false;
btnEditCC.Visible = false;
}
else
{
blCheck = true;
dgLocation.Items[0].Cells[ 1].Control s[5].Visib le = false;
dgLocation.Items[0].Cells[ 2].Control s[1].Visib le = false;
dgLocation.Items[0].Cells[ 2].Control s[3].Visib le = false;
dgLocation.Items[0].Cells[ 2].Control s[5].Visib le = true;
((ImageButton)dgLocation.I tems[0].Ce lls[2].Con trols[5]). Attributes .Add("oncl ick","popP ersonnelLi st('"+ dgLocation.Items[0].Cells[ 4].Text +"','"+ ((Label)(dgLocation.Items[ 0].Cells[0 ].Controls [1])).Text .Replace(" :","") +"','1')");
dgLocation.Items[1].Cells[ 0].Control s[1].Visib le = false;
dgLocation.Items[1].Cells[ 1].Control s[1].Visib le = false;
dgLocation.Items[1].Cells[ 1].Control s[3].Visib le = false;
dgLocation.Items[1].Cells[ 1].Control s[5].Visib le = false;
dgLocation.Items[1].Cells[ 2].Control s[1].Visib le = false;
dgLocation.Items[1].Cells[ 2].Control s[3].Visib le = false;
dgLocation.Items[1].Cells[ 2].Control s[5].Visib le = false;
dgLocation.Items[1].Cells[ 3].Control s[1].Visib le = false;
dgLocation.Items[1].Cells[ 3].Control s[3].Visib le = false;
btnEditCC.Visible = true;
btnEditCC.Attributes.Add(" onclick"," popCostCen terOptions ('"+ lblAddress.Text +"','"+ ((Label)dgLocation.Items[0 ].Cells[1] .Controls[ 3]).Text.T oUpper() +"')");
}
}
else
{
dgLocation.Items[0].Cells[ 1].Control s[5].Visib le = false;
if(dgLocation.Items[0].Cel ls[8].Text == "True")
dgLocation.Items[0].Cells[ 2].Control s[1].Visib le = false;
else
dgLocation.Items[0].Cells[ 2].Control s[1].Visib le = true;
((ImageButton)dgLocation.I tems[0].Ce lls[2].Con trols[1]). Attributes .Add("oncl ick","popP ersonnelLi st('"+ dgLocation.Items[0].Cells[ 4].Text +"','"+ ((Label)(dgLocation.Items[ 0].Cells[0 ].Controls [1])).Text .Replace(" :","") +"','1')");
dgLocation.Items[0].Cells[ 2].Control s[3].Visib le = false;
dgLocation.Items[0].Cells[ 2].Control s[5].Visib le = 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
The relevant code is :
<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
AutoGenerateColumns="False
<ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></Item
<Columns>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item
<ItemTemplate>
<IMG height="1" src="Images/clear.gif" width="10" border="0">
<asp:Label id="lblUser" runat="server" CssClass="txtSmall"></asp:
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item
<ItemTemplate>
<asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:
<asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:
<asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item
<ItemTemplate>
<asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_Chang
<asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_remov
<asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_addus
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item
<ItemTemplate>
<asp:ImageButton id="btnLGeneric" runat="server" ImageUrl="Images/btn_gener
<asp:ImageButton id="btnLMakePrime" runat="server" ImageUrl="Images/btn_makeP
<IMG height="1" src="Images/clear.gif" width="10" border="0">
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="msfwid"></asp:B
<asp:BoundColumn Visible="False" DataField="rowid"></asp:Bo
<asp:BoundColumn Visible="False" DataField="WholeName"></as
<asp:BoundColumn Visible="False" DataField="PrimeUser"></as
<asp:BoundColumn Visible="False" DataField="Locked"></asp:B
<asp:BoundColumn Visible="False" DataField="Generic"></asp:
</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)dgLocatio
(((Label)dgLocation.Items[
{
dgLocation.Items[0].Cells[
if(dgLocation.Items[0].Cel
dgLocation.Items[0].Cells[
else
dgLocation.Items[0].Cells[
((ImageButton)dgLocation.I
dgLocation.Items[0].Cells[
dgLocation.Items[0].Cells[
btnEditCC.Visible = false;
}
else
{
blCheck = true;
dgLocation.Items[0].Cells[
dgLocation.Items[0].Cells[
dgLocation.Items[0].Cells[
dgLocation.Items[0].Cells[
((ImageButton)dgLocation.I
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
dgLocation.Items[1].Cells[
btnEditCC.Visible = true;
btnEditCC.Attributes.Add("
}
}
else
{
dgLocation.Items[0].Cells[
if(dgLocation.Items[0].Cel
dgLocation.Items[0].Cells[
else
dgLocation.Items[0].Cells[
((ImageButton)dgLocation.I
dgLocation.Items[0].Cells[
dgLocation.Items[0].Cells[
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
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"></Item Style>
<ItemTemplate>
<asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_Chang eUser.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_remov eUser.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_addus er.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
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"></Item
<ItemTemplate>
<asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_Chang
<asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_remov
<asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_addus
</ItemTemplate>
</asp:TemplateColumn>
Also, in this example, which control is Item[n].Cell[1].Control[5]
thanks in advance
Paul
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much for your help Edwin. I think I understand it now
regards
Paul
regards
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
<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
AutoGenerateColumns="False
<ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></Item
<Columns>
<asp:TemplateColumn> <<==== this is the start of the 1st column i.e. 1st cell of a row.
<ItemStyle VerticalAlign="Top"></Item
<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:
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle VerticalAlign="Top"></Item
<ItemTemplate>
<asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:
<asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:
<asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.
</ItemTemplate>
</asp:TemplateColumn>
Hope that helps
Edwin