Record count required when using GridView and ObjectDataSource

J

J055

Hi

I'm trying to get an instance of UserLists to persist after it's been used
by the GridView. I understand from the documentation that The
OnObjectCreated event allows access to the instance. I have a TotalUsers
property which is initiated when the GetUsers method is called, however it
is empty after the OnObjectCreated event runs. What am I doing wrong? Is
there a way to get the Row count from the GetUsers method (It is a
DataTable)? I don't want to call the GetUsers method twice.

Thanks for your help.
Andrew


protected void Page_Load(object sender, EventArgs e)
{
ObjectDataSource1.TypeName = "Business.UserLists";
ObjectDataSource1.SelectMethod = "GetUsers";
}

protected void ObjectDataSource_Created(object sender,
ObjectDataSourceEventArgs e)
{
UserLists users = (UserLists)e.ObjectInstance;
Response.Write(users.TotalUsers.ToString());
}
 
J

J055

OK, I've found that I can use the ObjectDataSourceStatusEventArgs class in
the OnSelected method to get the row count of the DataTable. But why do all
these events get called twice? I only have one ObjectDataSource and
GridView.

protected void ObjectDataSource_Selected(object sender,
ObjectDataSourceStatusEventArgs e)

{

//ReturnValue is a DataTable

DataTable dt = (DataTable)e.ReturnValue;

Response.Write(dt.Rows.Count.ToString());


}
 
S

Steven Cheng[MSFT]

Hi J055,

Thank you for posting.

Yes, the Selected event will always be fired after the
objectdatasource(also available to other datasource controls) and we can
access some returned value or returned parameters there. As for the event
firing twice, it is not the expected behavior. Have you tried also register
the GridView's "Databinding" event to see whether it is also called twice.
BTW, if convenient, would you provide some detailed code logic on the page
and your data access object class (used in objectdatasource control)?

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

J055

Hi Steven

I've finally been able to work out what's causing events to be fired twice.
I registered the Databinding event as you suggested. The Databinding fires
twice when I add this line to the Page_Load event.

GridView1.BottomPagerRow.CssClass = "break";
//It only seems to do it on Postbacks. The declarative code looks like this:

<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
OnDataBinding="Grid_DataBinding"
AllowSorting="True" AutoGenerateColumns="False" CssClass="data"
AllowPaging="true"
EnableTheming="False" EnableViewState="False" GridLines="None" PageSize="5"
PagerSettings-Mode="NumericFirstLast"
PagerSettings-Position="TopAndBottom">
<Columns>
<asp:ImageField>
</asp:ImageField>
<asp:BoundField DataField="FullName" HeaderText="Name"
SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"
/>
<asp:BoundField DataField="LastLogin" HeaderText="Last Login"
HtmlEncode="False"
SortExpression="LastLogin" />
<asp:BoundField DataField="AccessLevel" HeaderText="Access Level"
SortExpression="AccessLevel" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetUsers"
TypeName="Empetus.Accounts.Business.UserLists"></asp:ObjectDataSource>

Is this supposed to happen? I couldn't see why but you may be able to
provide me with an explanation. I can supply you with the whole project if
that helps.

Many thanks
Andrew
 
S

Steven Cheng[MSFT]

Hi Andrew,

Thanks for the response.

Of course, this is an unexpected behavior. However, I've ever encountered
some problem on certain events on page get fired twice, some of them are
caused by the page be posted back(or requested) twice. For example, when
there is an image tag that put a relative path reference the page's url, it
makes a GET http request to the page, thus make the page's load event
execute twice. Not sure whether this could be the case and related to your
page's code logic. I think you can try looking up the IIS log of the server
to see whether there will always occur double request entires when that
problem page get postback, if so, it is suffering the similiar problem.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

J055

Hi Steven

I've been using the ASP.NET development server so haven't had change to look
in the IIS logs but I have since moved the line to the Page PreRender event
which has stopped the DataBinding event firing twice...

protected void Page_PreRender(object sender, EventArgs e)
{

if (UserGrid.AllowPaging)

UserGrid.BottomPagerRow.CssClass = "break";

}

Thinking about it, this may be a more appropriate place for setting these
types of properties?

Cheers
Andrew
 
S

Steven Cheng[MSFT]

Thanks for your response Andrew,

Yes, Prerender is a good place for applying styles or other UI setting of
webserver control. However, I still feel very strange on the double
databinding behavior. Anyway, you can still have a check when hosting it in
IIS.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

J055

Hi Steven

I've checked this in IIS. Still get the same problem as with the development
web server.

To test I did the following:

1. Load the page - DataBinding fires once
2. Click a GridView page button - DataBinding fires twice (and for any
further postbacks)

The page didn't contain any links to external files (images, css etc). This
is the IIS log file entries for the above:

#Software: Microsoft Internet Information Services 5.1
#Version: 1.0
#Date: 2006-04-06 16:26:26
#Fields: date time c-ip cs-method cs-uri-stem sc-status
2006-04-06 16:26:26 127.0.0.1 GET /Default.aspx 200
2006-04-06 16:26:28 127.0.0.1 POST /Default.aspx 200

If I move [ GridView1.BottomPagerRow.CssClass = "break"; ] to the
Page_PreRender event from the Page_Load event it doesn't fire twice when the
page buttons are clicked and I get exactly the same IIS log output.

I've also just noticed that if I put a ButtonField with an image in the
GridView the DataBinding event fires twice on postback. e.g.

<asp:ButtonField ButtonType="Image" ImageUrl="~/images/edit.gif" Text="Edit"
/>

I can't figure out what's happening here so would really appreciate some
suggestions on what's happening and how to resolve it.

Thanks again
Andrew
 
J

J055

Hi Steven

I posted the response above on the 6th. I wondered if

you'd had chance to look at it and come up with any answers? I'm finding

that my pages using the GridView and ObjectDataSource controls seem to be

calling my Business Object method twice for no apparent reason. I clearly

can't release anything into production with this type of behaviour so I'd be

very grateful if you could let me know how to resolve this.

Cheers

Andrew
 
S

Steven Cheng[MSFT]

Thanks for the response Andrew,

This is indeed a strange behavior, so far I haven't found any known issue
on this. Based on my experience, this issue may require further
troubleshooting. And if you feel it urgent, you may consider contact CSS to
perform thorough troubleshooting/debugging on it. Also, you need to create
a simplified reproduce project/page on this.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Joined
Mar 18, 2009
Messages
5
Reaction score
0
I may have misunderstood what you're trying to do but couldn't you simply get a record count from the SQL SelectCommand in your data source - SELECT COUNT(*) AS RecordCount. You could then bind this to a control using <%#Eval("RecordCount")%> in the template.

Another approach would be to count the number of rows in the gridview after binding. This example in VB.net shows a text response to a search which appears just above a gridview:

Code:
If grdResults.Rows.Count = 0 Then
                lblResults.Text = "Sorry, no records matched your search text."
            ElseIf grdResults.Rows.Count = 1 Then
                lblResults.Text = "1 result found."
            Else
                lblResults.Text = grdResults.Rows.Count.ToString & " results found."
            End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top