Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

asp.net - How do I set up ObjectDataSource select parameters at runtime

I'm trying to add parameters to an objectDataSource at runtime like this:

        Parameter objCustomerParameter = new Parameter("CustomerID", DbType.String, customerID);
        Parameter objGPDatabaseParameter = new Parameter("Database", DbType.String, gpDatabase);

        //set up object data source parameters
        objCustomer.SelectParameters["CustomerID"] = objCustomerParameter;
        objCustomer.SelectParameters["Database"] = objGPDatabaseParameter;

At what point in the objectDataSource lifecycle should these parameters be added (what event)? Also, some values are coming from a master page property (which loads after the page_load of the page containing the objectDataSource).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add them to the event for the operation you are trying to use. For example, if these parameters are part of the SELECT command then add them to the Selecting event, if they need to go with the UPDATE command then add them on the Updating event.

The ObjectDataSource raises an event before it performs each operation, that's when you can insert parameters (or validate/alter existing parameters).

Also, don't try and modify the parameters collection of the ODS itself. You want to add your parameters to the ObjectDataSourceSelectingEventArgs that is passed to the event handler.

Something like:

e.InputParameters["CustomerID"] = customerId;
e.InputParameters["database"] = dbName;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...