Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to programmatically set the DefaultValue of asp:controlparameter

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I need to set the defaultvalue of an asp:controlparameter to 1 year ago today.

<asp:SqlDataSource ID="InvDataSource"...
<SelectParameters>
<asp:ControlParameter ControlID="txtFromDate" Name="FromDate" PropertyName="Text" Type="String" DefaultValue="" />
...

How do I set this? I'm using c#. Thx for any help!
 
Avoid using the datasource controls and use stored procedures with parameters.
 
This isn't an option as I don't have that kind of access to the db.
 
Figured it out:

On page_load if !page.insPostBack

DateTime lyr = DateTime.Now.AddYears(-1);
InvDataSource.SelectParameters["FromDate"].DefaultValue = lyr.ToString("yyyy-MM-dd");
 
if you do not have access to create SPs on the database use an ORM framework (active record and nhibernate are the most popular) instead of datasource controls.

the GUI should have no knowledge of where data comes from. text file, database, webservice. the code behind shouldn't know. it should be asking an object to give it data. this object may either rely on other objects to get the data, or may get the data directly.

the objectdatasource is a little better but they are still a problem.
1. you cannot step through code
2. they are not testable (automated unit testing)
3. they pollute the service layer with presentation attributes.

all that said. it's a much better practice to separate the concerns of presentation and dataaccess into specific objects. and using and ORM (SPs [glup ;)]) to access the data.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top