Change / set ObjectDataSource paramater at runtime?

P

planetthoughtful

Hi All,

I have an ASP.NET page that displays a GridView control based on an
ObjectDataSource control.

I'm wondering if it's possible to base the ObjectDataSource in question
on a parameter query, with the ability to set the parameter value at
run time (in the Page_Load event, perhaps?)?

Basically, I'd like to display records in the GridView based on values
typed into a text box. So, I guess I'm looking to base the
ObjectDataSource on a SELECT query similar to:

SELECT column1, column2, column3 FROM table1 WHERE column1 = @col1val;

Can anyone tell me if it's possible to set a parameter for an
ObjectDataSource at runtime, and how you'd do that if it is possible?

Many thanks in avance!

Much warmth,

planetthoughtful
 
G

Guest

Hi Planetthoughtful,

You can configure the ObjectDataSource to get its contents from a control on
the page. Select configure data source in the Gridview action list an run
through the wizard, selecting Control as the parameter type, or modify the
HTML like this:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetCustomers"
TypeName="Customers">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="*"
Name="filter" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>

Use something like this if you want to dynamically set it in code:

ObjectDataSource1.SelectParameters[0].DefaultValue = "<your value>";

Joe
http://www.csharp-station.com
 
P

planetthoughtful

You can configure the ObjectDataSource to get its contents from a control on
the page. Select configure data source in the Gridview action list an run
through the wizard, selecting Control as the parameter type, or modify the
HTML like this:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetCustomers"
TypeName="Customers">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="*"
Name="filter" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>

Use something like this if you want to dynamically set it in code:

ObjectDataSource1.SelectParameters[0].DefaultValue = "<your value>";

Hi Joe,

Thanks for this answer! I managed to work my way around this problem in
the interim by using an SqlDataSource for the GridView, and explicitly
setting the SELECT statement with a variable value in the WHERE clause,
but it's great to know how to achieve the same thing using
ObjectDataSources as well!

Thanks again!

Much warmth,

planetthoughtful
 

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

Top