R
rn5a
Assume that a ASPX page displays the products, product id, description
& price of each product in a DataGrid. Also assume that users have
been given the option to sort the DataGrid. In other words, the
headers Product Name, ProductID, Description & Price will be
hyperlinks clicking which the DataGrid will be sorted accordingly.
Each of the 4 headers in the DataGrid is accompanied with an image
which is hidden when the page loads for the first time. This is the
DataGrid code:
---------------------------------------------------
<aspataGrid ID="dgProducts" OnItemCommand="dg_ItemCommand"
OnItemDataBound="dg_ItemDataBound" AllowSorting="true"
EnableViewState="true" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkID" CommandArgument="Products.ProductID"
Text="ID" runat="server"/> <img id="imgID" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Container.DataItem("ProductID") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkName" CommandArgument="ProductName" Text="NAME"
runat="server"/> <img id="imgName" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Container.DataItem("ProductName")
%>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
.........
</aspataGrid>
---------------------------------------------------
This is the dg_ItemDataBound sub which not only makes the images
visible (depending upon which header a user clicks to sort the
DataGrid) but also changes the image so that users can easily make out
whether the DataGrid has been sorted ascendingly or descendingly. This
is code of dg_ItemDataBound:
---------------------------------------------------
Sub dg_ItemDataBound(ByVal obj As Object, ByVal ea As
DataGridItemEventArgs)
If (Page.IsPostBack) Then
If (ea.Item.ItemType = ListItemType.Header) Then
If (Session("Sort") = "Products.ProductID ASC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
ElseIf (Session("Sort") = "Products.ProductID DESC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
End If
If (Session("Sort") = "ProductName ASC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
ElseIf (Session("Sort") = "ProductName DESC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
End If
End If
End If
End Sub
---------------------------------------------------
As such the above code works fine but if I place the ItemDataBound
code (shown above) which makes the images visible/invisible & changes
it depending upon whether the DataGrid is sorted ascendingly or
descendingly in an ItemCreated event function, then it doesn't work.
Why so?
Thanks
& price of each product in a DataGrid. Also assume that users have
been given the option to sort the DataGrid. In other words, the
headers Product Name, ProductID, Description & Price will be
hyperlinks clicking which the DataGrid will be sorted accordingly.
Each of the 4 headers in the DataGrid is accompanied with an image
which is hidden when the page loads for the first time. This is the
DataGrid code:
---------------------------------------------------
<aspataGrid ID="dgProducts" OnItemCommand="dg_ItemCommand"
OnItemDataBound="dg_ItemDataBound" AllowSorting="true"
EnableViewState="true" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkID" CommandArgument="Products.ProductID"
Text="ID" runat="server"/> <img id="imgID" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Container.DataItem("ProductID") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkName" CommandArgument="ProductName" Text="NAME"
runat="server"/> <img id="imgName" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Container.DataItem("ProductName")
%>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
.........
</aspataGrid>
---------------------------------------------------
This is the dg_ItemDataBound sub which not only makes the images
visible (depending upon which header a user clicks to sort the
DataGrid) but also changes the image so that users can easily make out
whether the DataGrid has been sorted ascendingly or descendingly. This
is code of dg_ItemDataBound:
---------------------------------------------------
Sub dg_ItemDataBound(ByVal obj As Object, ByVal ea As
DataGridItemEventArgs)
If (Page.IsPostBack) Then
If (ea.Item.ItemType = ListItemType.Header) Then
If (Session("Sort") = "Products.ProductID ASC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
ElseIf (Session("Sort") = "Products.ProductID DESC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
End If
If (Session("Sort") = "ProductName ASC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
ElseIf (Session("Sort") = "ProductName DESC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
End If
End If
End If
End Sub
---------------------------------------------------
As such the above code works fine but if I place the ItemDataBound
code (shown above) which makes the images visible/invisible & changes
it depending upon whether the DataGrid is sorted ascendingly or
descendingly in an ItemCreated event function, then it doesn't work.
Why so?
Thanks