How To Create The Simple Custom Value Provider In Asp.Net 4.5

Developers working in asp.net web development team will share steps to create a simple custom value provider in asp.net 4.5. You can read this article and learn how experts create custom value in asp.net.

Asp.net 4.5 supports model binding. Model binding is nothing but a value provider.

There are many inbuilt value providers available. These will be used to filter the data. Value provider is a class type, which is used to extract the data, based on the filter condition and set the parameter value to the data control.

Asp.net 4.5 In build value providers

  1. Query string
  2. Session
  3. Cookies
  4. Control Values
  5. RouteData
  6. ViewState

In this topic, we will discuss the method through which we can create our own value provider for filtering the record. In custom provider, we can write the insert, update, select and delete statements to perform the respective operations.

In this article, I am going to explain the custom value providers which will use the query string as input parameter which will filter and supply the records to the Grid View control.

To create the Custom Value provider, we need to write two classes, once class will be implemented from IValueProvider Interface and another one will be inherited from ValueProviderSourceAttribute class.

Create a new class called GetKeyValueProvider . The code is shown below:

Once it is implemented from IValueProvider, it will expect you to implement two methods ConatinsPrefix and GetValue. Write the logic inside the GetValue method. Here I am passing the Querystring value and filter the Value provider result based on the query string value. You can write your own logic inside the Getvalue method.

publicclassGetKeyValueProvider : IValueProvider
 {
 publicboolContainsPrefix(string prefix)
 {
 returnfalse;
 }

publicValueProviderResultGetValue(string key)
 {
 if (HttpContext.Current.Request.QueryString[key] != null)
 {
 string value = HttpContext.Current.Request.QueryString[key];
 ValueProviderResult result = newValueProviderResult(value, value, CultureInfo.CurrentCulture);
 return result;
 }
 else
 {
 returnnull;
 }
 }
 }

Next step is to create the Value Provider attribute class. Create a new class and name it GetKeyValueProviderAttribute.

See Also: User Role Based dynamic Navigation from SQL database Entity framework in MVC

This class is having Parameterized Constructor, which will take the parameter Key. This is nothing but a Query string name.GetKeyValueProviderAttribute method will return the query string name and GetValueProvider method will return the Instance of GetKeyValueProvider class.

publicclassGetKeyValueProviderAttribute : ValueProviderSourceAttribute
 {
 privatestring key = "";

publicGetKeyValueProviderAttribute(string key)
 {
 this.key = key;
 }

publicoverrideIValueProviderGetValueProvider(ModelBindingExecutionContextmodelBindingExecutionContext)
 {
 returnnewGetKeyValueProvider();
 }

publicoverridestringGetModelName()
 {
 return key;
 }
 }

I am going to use the Vendor data model to display the record. The above custom value provider class will filter the data based on the query string passed.

Create the Entity data model for Vendor Table.

Create the below code in your Aspx page to implement the Custom Value Provider GetKeyValueProvider. This will give the filtered GetItem method for GridviewShowVendor based on the query string passed.

publicTestDbEntities.VendorsShowVendor_GetItem([GetKeyValueProvider("vendid")]intVendorID)
 {
 TestDbEntities Entity = newTestDbEntities();
 varVenddata = from vend inEntity.Vendors
 wherevend.Vendor_ID == VenorID
 select vend;
 returnVenddata.SingleOrDefault();
 }

Conclusion

This article explained how we can create the Custom provider. Asp.net 4.5 is having plenty of in build value providers. In this post, I have inherited the existing Value provider and added my logic into it. I have explained one of the value provider query strings likewise you can use other model binding also.

This is a simple example, but it will help take you to the first step for your custom value provider creation.

Thank you for reading this article. Hope the experts have made every point clear to make you understand the process of creating custom value provider in asp.net web development project. If there is anything you did not get, ask in comments.

You might like

About the Author: Vijay Aegis

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept