By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Record count required when using GridView and ObjectDataSource

P: n/a
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());
}
Mar 28 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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());
}

"J055" <j0**@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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());
}

Mar 28 '06 #2

P: n/a
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.
Mar 29 '06 #3

P: n/a
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
"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:QL**************@TK2MSFTNGXA01.phx.gbl...
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.

Apr 1 '06 #4

P: n/a
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.)

Apr 3 '06 #5

P: n/a
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
"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:cg**************@TK2MSFTNGXA01.phx.gbl...
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.)

Apr 4 '06 #6

P: n/a
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.)


Apr 5 '06 #7

P: n/a
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
Apr 6 '06 #8

P: n/a
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
Apr 11 '06 #9

P: n/a
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.)

Apr 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.