ObjectDataSource OnSelected event raised twice

C

chris.c.woodward

This event seems to be being raised twice in my codebehind. I have
only one SelectMethod and do not have a SelectCountMethod. Its causing
a problem because I'm dynamically loading a user control into a
PlaceHolder control in the method and am having to do a check to see
if it already exists or not otherwise it gets loaded twice. Also it
would be nice to figure out what the heck is going on! Here is the
code ...


protected void odsCatalogues_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
Response.Write("event called");
Response.Write("<br />");


if (e.ReturnValue != null)
{
PagedDataSource pds = (PagedDataSource)e.ReturnValue;
totalRowCount = pds.DataSourceCount;

lblPageInfo.Text = totalRowCount + " records found. Page "
+ (pageIndex + 1) + " of " + pageCount;

if (PlaceHolder1.Controls.Count == 0)
{
Control control1 = Page.LoadControl("~/UserControls/
NumericPager.ascx");
UserControls_NumericPager numericPager1 =
(UserControls_NumericPager)control1;
numericPager1.PageCount = pageCount;
numericPager1.PageIndex = pageIndex;
PlaceHolder1.Controls.Add(numericPager1);
}
}
}


Anybody got any ideas why this event is being called twice?
 
N

nahid

This event seems to be being raised twice in my codebehind. I have
only one SelectMethod and do not have a SelectCountMethod. Its causing
a problem because I'm dynamically loading a user control into a
PlaceHolder control in the method and am having to do a check to see
if it already exists or not otherwise it gets loaded twice. Also it
would be nice to figure out what the heck is going on! Here is the
code ...

protected void odsCatalogues_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
Response.Write("event called");
Response.Write("<br />");

if (e.ReturnValue != null)
{
PagedDataSource pds = (PagedDataSource)e.ReturnValue;
totalRowCount = pds.DataSourceCount;

lblPageInfo.Text = totalRowCount + " records found. Page "
+ (pageIndex + 1) + " of " + pageCount;

if (PlaceHolder1.Controls.Count == 0)
{
Control control1 = Page.LoadControl("~/UserControls/
NumericPager.ascx");
UserControls_NumericPager numericPager1 =
(UserControls_NumericPager)control1;
numericPager1.PageCount = pageCount;
numericPager1.PageIndex = pageIndex;
PlaceHolder1.Controls.Add(numericPager1);
}
}
}

Anybody got any ideas why this event is being called twice?

can you pleasae provide some aditional code how do you bind the grid

nahid
http://nahidulkibria.blogspot.com/
http://www.kaz.com.bd
 
C

chris.c.woodward

Thanks for your reply.

I'm using a DataList bound to an ObjectDataSource. Here is the HTML
markup in the relevant .aspx page with some code removed for clarity:

<asp:DataList ID="dlCatalogues" runat="server"
DataSourceID="odsCatalogues" EnableViewState="false">

...

</asp:DataList>

<asp:ObjectDataSource ID="odsCatalogues" runat="server"
SelectMethod="SelectCatalogues" TypeName="AGT.Business.EventDB"
OnSelected="odsCatalogues_Selected"
OnSelecting="odsCatalogues_Selecting">
<SelectParameters>
<asp:QueryStringParameter ... / <asp:QueryStringParameter ... / <asp:QueryStringParameter ... / <asp:parameter ... />
</SelectParameters>
</asp:ObjectDataSource>
 
N

nahid

Thanks for your reply.

I'm using a DataList bound to an ObjectDataSource. Here is the HTML
markup in the relevant .aspx page with some code removed for clarity:

<asp:DataList ID="dlCatalogues" runat="server"
DataSourceID="odsCatalogues" EnableViewState="false">

...

</asp:DataList>

<asp:ObjectDataSource ID="odsCatalogues" runat="server"
SelectMethod="SelectCatalogues" TypeName="AGT.Business.EventDB"
OnSelected="odsCatalogues_Selected"
OnSelecting="odsCatalogues_Selecting">
<SelectParameters>
<asp:parameter ... />
<asp:ControlParameter ... /

<asp:QueryStringParameter ... /

<asp:QueryStringParameter ... /

<asp:QueryStringParameter ... /

<asp:parameter ... />
</SelectParameters>
</asp:ObjectDataSource>

can you please check this post
http://www.dotnetspider.com/qa/Question13248.aspx
hope help

nahid
http://nahidulkibria.blogspot.com/
http://www.kaz.com.bd
 
C

chris.c.woodward

Think I've figured out what was going on.

The ObjectDataSource markup contained this line:

<asp:ControlParameter DefaultValue="0" Name="clientId" Type="Int32"
ControlID="DDList1" PropertyName="SelectedValue" />

where DDList1 is actually a data driven UserControl I have created.

Replacing the line with:

<asp:parameter DefaultValue="0" Name="clientId" Type="Int32" />

and setting the value in the ObjectDataSource Selecting event:

protected void odsCatalogues_Selecting(object sender,
ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["clientId"] =
Convert.ToInt32(DDList1.SelectedValue);
}

meant that my DataList databinding was now not occurring twice.

Still not sure why this should be but this work around has fixed it.

:)
 
Joined
Dec 3, 2008
Messages
1
Reaction score
0
I know it's been over a year since this thread has been active, but I ran
into a similar problem and after debugging found what the problem was.

I had a DataList that was calling DataBind on it's objectDataSource twice.
While debugging I found that the first call was happening because of code in
the Master page of the page. We have a function called "FindControl" which
recursively goes through all the controls on the page to find a specific
control with a specified id. This function uses [control].Controls to
recursively call. When the function got to the DataList, calling DataList1.
Controls caused DataBind to occur and the Select function to get called.

So the point is whenever DataList.Controls is called, it will try to fill in
the DataItems. Doing so raises the DataBind event on the DataSource control.

Hope this helps all those who are searching.
Philip
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top