How does ASP. NET dynamically set selectparameters of objectdatasource?

Source: Internet
Author: User

In ASP. NET 2.0 binds the Data Object objectdatasource to the display view, which facilitates the interaction between data from the database to the display layer. This is the same as the Java strut, but it is much simpler. However, there are some problems with using objectdatasource. The parameters of objectdatasource are directly bound to the control or querystring, so you cannot directly perform manual operations on the parameters. For example, you cannot implement objectdatasource. selectparameters ["ID"]. value = '000000 '.
Although objectdatasource provides a selecting event, you can control it in this event squadron parameter.

However, to implement this function, you still needCode. Selectparameters is a set of parameters, which contains the parameters object. What is parameters? It is a new object of 2.0. Http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.parameter.aspx
In Microsoft's object description, how does one extend the parameter class. I tested it and can still be used.
Steps:
1: create a new class and copy the Microsoft Code directly.
Namespace samples. ASPnet {

Using system;
Using system. componentmodel;
Using system. Security. permissions;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

[Aspnethostingpermission (securityaction. Demand, level = aspnethostingpermissionlevel. Minimal)]
Public class staticparameter: parameter {

Public staticparameter (){
}
// The staticparameter (string, object) constructor
// Initializes the datavalue property and callthe
// Parameter (string) constructor to initialize the name property.
Public staticparameter (string name, object Value): Base (name ){
Datavalue = value;
}
// The staticparameter (string, typecode, object) constructor
// Initializes the datavalue property and callthe
// Parameter (string, typecode) constructor to initialize the name and
// Type properties.
Public staticparameter (string name, typecode type, object Value): Base (name, type ){
Datavalue = value;
}
// The staticparameter copy constructor is provided to ensure that
// The State contained in the datavalue property is copied to new
// Instances of the class.
Protected staticparameter (staticparameter original): Base (original ){
Datavalue = original. datavalue;
}

// The clone method is overridden to call
// Staticparameter copy constructor, so that the data in
// The datavalue property is correctly transferred to
// New instance of the staticparameter.
Protected override parameter clone (){
Return new staticparameter (this );
}
// The datavalue can be any arbitrary object and is stored in viewstate.
Public object datavalue {
Get {
Return viewstate ["value"];
}
Set {
Viewstate ["value"] = value;
}
}
// The value property is a type safe convenience Property
// Used when the staticparameter represents string data.
// It gets the string value of the datavalue property, and
// Sets the datavalue property directly.
Public String Value {
Get {
Object o = datavalue;
If (O = NULL |! (O is string ))
Return string. empty;
Return (string) O;
}
Set {
Datavalue = value;
Onparameterchanged ();
}
}

// The evaluate method is overridden to return
// Datavalue property instead of the defaultvalue.
Protected override object evaluate (httpcontext context, Control ){

If (context. Request = NULL)
Return NULL;

Return datavalue;
}
}
}

2: write code in selecting
Protected void objectdatasourcemtnitem_selecting (Object sender, objectdatasourceselectingeventargs E)
{
Staticparameter pcode = new staticparameter ();
Label codelabel = (Label) formview1.findcontrol ("codelabel ");

If (codelabel = NULL)
Pcode. value = request. querystring ["itemno"];
Else
Pcode. value = codelabel. text;
Pcode. Name = "Strid ";
Pcode. type = typecode. String;

Objectdatasourcemtnitem. selectparameters ["Strid"] = pcode;
}

Here, Strid is the original parameter name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.