ListView

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

I need to pick up the "TotalItemsLabel" text in my C# method. How to do
that if this is the part of my Default.aspx page which is generated from
master page:
...
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID"
DataSourceID="ObjectDataSourceNarudzba"
...
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer"
runat="server" border="1" style="background-color: #FFFFFF;
border-collapse: collapse;
border-color: #999999; border-style: none; border-width: 1px;
font-family: Verdana, Arial,
Helvetica, sans-serif;">
<tr id="Tr2" runat="server"
style="background-color: #FFFBD6; color: #333333;">
<th id="Th1" runat="server">
</th>
<th id="Th2" runat="server">
</th>
<th id="Th3" runat="server">
TEKST
</th>
<th id="Th4" runat="server">
OPIS
</th>
</tr>
<tr id="itemPlaceholder"
runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server"
style="text-align: center; background-color: #FFCC66;
font-family: Verdana, Arial, Helvetica,
sans-serif; color: #333333;">
<asp:DataPager ID="DataPager1"
runat="server" PageSize="4">
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
<asp:TemplatePagerField>
<PagerTemplate>
<b>stranica:
<asp:Label
runat="server" ID="CurrentPageLabel" Text="<%# Container.StartRowIndex
%>" />
od
<asp:Label
runat="server" ID="TotalPagesLabel" Text="<%#
Container.StartRowIndex+Container.PageSize %>" />
(ukupno
<asp:Label
runat="server" ID="TotalItemsLabel" Text="<%# Container.TotalRowCount%>" />
zapisa)
<br />
</b>
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
...
</asp:ListView>



I tried like this:
Label x =
(Label)ListView1.InsertItem.FindControl("DataPager1").FindControl("TotalItemsLabel");

but I got: Object reference not set to an instance of an object

I also tried:
DataPager y=
(DataPager)ListView1.InsertItem.FindControl("DataPager1");
Got the same error.
Finaly I tried to Find all IDs from ListView to DataPager:
DataPager stranicnik =
(DataPager)ListView1.InsertItem.FindControl("Table2").FindControl("Tr3").FindControl("Td2").FindControl("DataPager1");
//count = stranicnik.TotalRowCount;
//pageSize = stranicnik.PageSize;
And again got the same error.
How to find DataPager control to get the TotalRowCount, which is works
perfectly on my aspx page? Or how to Find that specific label ?
All that I tried within C# method ListView1_DataBound
 
Object reference not set to an instance of an object

I frankly don't know how I will solve that and many other similar
problems with Master page and Find inherits controls, without this:
http://www.west-wind.com/Weblog/posts/5127.aspx
great blog. So much artificial help in writing code, and you can't find
some control!
The same thing is with Text Binding or Eval in some aspx control. You
just need to know. It is to frustrating for begineers like me.
 
Mario said:
I need to pick up the "TotalItemsLabel" text in my C# method. How to do
that if this is the part of my Default.aspx page which is generated from
master page:

You would do in the ItemDataBound event for the control in the
codebehind file.

It's only going to work for the one item that is bounded. You can do a
Control.Find() and find TotalItemsLabel.

var lbl = Control.Find("TotalItemsLabel")

If(lbl != null)
{
var someval = lbl.Text;
}
...
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID"
DataSourceID="ObjectDataSourceNarudzba"
...
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer"
runat="server" border="1" style="background-color: #FFFFFF;
border-collapse: collapse;
border-color: #999999; border-style: none; border-width: 1px;
font-family: Verdana, Arial,
Helvetica, sans-serif;">
<tr id="Tr2" runat="server"
style="background-color: #FFFBD6; color: #333333;">
<th id="Th1" runat="server">
</th>
<th id="Th2" runat="server">
</th>
<th id="Th3" runat="server">
TEKST
</th>
<th id="Th4" runat="server">
OPIS
</th>
</tr>
<tr id="itemPlaceholder"
runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server"
style="text-align: center; background-color: #FFCC66;
font-family: Verdana, Arial, Helvetica,
sans-serif; color: #333333;">
<asp:DataPager ID="DataPager1"
runat="server" PageSize="4">
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
<asp:TemplatePagerField>
<PagerTemplate>
<b>stranica:
<asp:Label
runat="server" ID="CurrentPageLabel" Text="<%# Container.StartRowIndex
%>" />
od
<asp:Label
runat="server" ID="TotalPagesLabel" Text="<%#
Container.StartRowIndex+Container.PageSize %>" />
(ukupno
<asp:Label
runat="server" ID="TotalItemsLabel" Text="<%# Container.TotalRowCount%>" />
zapisa)
<br />
</b>
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
...
</asp:ListView>



I tried like this:
Label x =
(Label)ListView1.InsertItem.FindControl("DataPager1").FindControl("TotalItemsLabel");


but I got: Object reference not set to an instance of an object

It didn't find the control. If an object is null because it didn't find
something to set the object, then you'll the get message above.
I also tried:
DataPager y=
(DataPager)ListView1.InsertItem.FindControl("DataPager1");
Got the same error.
Finaly I tried to Find all IDs from ListView to DataPager:
DataPager stranicnik =
(DataPager)ListView1.InsertItem.FindControl("Table2").FindControl("Tr3").FindControl("Td2").FindControl("DataPager1");

//count = stranicnik.TotalRowCount;
//pageSize = stranicnik.PageSize;
And again got the same error.
How to find DataPager control to get the TotalRowCount, which is works
perfectly on my aspx page? Or how to Find that specific label ?
All that I tried within C# method ListView1_DataBound

I think you may need to use a javascript function using getelementbyid,
which would be fired off some control client side event. You would use
an ASP:HiddenField and the function would populate the HiddenField, and
you gat the value from the HiddenField on a server side event in
codebehind file.

http://www.tizag.com/javascriptT/javascript-getelementbyid.php
 
Back
Top