Asp. Design with event customization control in net

Source: Internet
Author: User
Tags new features zip visual studio
Asp.net| Control | Design when trying to develop an application that runs on the network for a customer, I find very little discussion about using the. NET Web control properly. The following are common problems with. NET Web controls:

1. How do you make these controls communicate with each other?

2. How do I keep these controls in the state?

3. How can multiple controls be effectively joined to a Web page?

As an ASP developer, I find it not easy to turn to asp.net. My initial idea was to keep the state through the session object or using a query statement, but found that these two methods were too sloppy and that there was a problem when trying to synchronize all the Web controls on a Web page. I stumbled across an article about creating an event in a Web control, but still suffered in practice, so I think it's important to provide an instance of creating a Web control correctly and creating custom events at the same time.

The discussion will be in the following order:
1. Create a Web control

2. Create custom events and event parameters for the control

3. Using Web controls correctly on a Web page

During the discussion, I will also provide readers with tips to enable readers to develop applications more accurately and quickly.

The Web control we created here is a custom drop-down selection box that is based on a standard version of SQL Server or the stores table in the pubs database for MSDE. In development we used the visual Studio. NET 2003 Development tools and the C # programming language.

After creating the pubs Web project, the first task (at least for me) is to rename the WebForm1.aspx file to Default.aspx and modify the class so that it matches the name. You then create a folder structure in the IDE environment to facilitate object lookup.



I created the controls directory to store all the controls that were created to make it easier to access them. Depending on the granularity of the creation of the control, we can further subdivide the controls directory.



I'm going to name the control Storeselector.ascx. The first step is to add the DropDownList control to the form.



It's time to "route" the control. To create an object for a dataset class:

#code
Private DataSet data;
#end Code
Create a binddata feature that fills in the data in a drop-down list:

#code
private void Binddata ()
{
data = new DataSet ();
SqlConnection cnn = new SqlConnection ("Data source=" (local); Initial
catalog=pubs;integrated Security=sspi ");
SqlDataAdapter adapter = new SqlDataAdapter ();
Adapter. SelectCommand = new SqlCommand ("Select stor_id, Stor_name,stor_address, city, state, zip from stores", CNN);
Adapter. Fill (data, "stores");
Storelist.datasource = data;
Storelist.datamember = "Stores";
Storelist.datatextfield = "Stor_name";
Storelist.databind ();
Session.add ("Data", data);
}
#end Code
I added a DataSet object to the session variable to make the data accessible during the conversation and when the data was passed during the control event trigger. Note that you want to ensure that the Page_onload event is correct:

#code
private void Page_Load (object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Binddata ();
}
}
#end Code
Now we drag the new control onto the Default.aspx Web page and run the project.






It's simple, isn't it? Here is the High skill section. We want to add a few tags to the default.aspx to reflect the changing store. We want each tab to display a column in the store that is now selected, where we need to create a custom event for the Storeselector control and the event parameter class. Let's first create the event Argument Class (StoreSelectorCommandEventArgs.cs):

#code
public class Storeselectorcommandeventargs
{
private string _stor_id;
private string _stor_name;
private string _stor_address;
private string _city;
private string _state;
private string _zip;

Public Storeselectorcommandeventargs (String stor_id, String stor_name,
String stor_address, String city, string state, String Zip)
{
_stor_id = stor_id;
_stor_name = Stor_name;
_stor_address = stor_address;
_city = City;
_state = State;
_zip = Zip;
}

public string stor_id{get{return _stor_id}}
public string stor_name{get{return _stor_name}}
public string stor_address{get{return _stor_address}}
public string city{get{return _city}}
public string state{get{return _state}}
public string zip{get{return _zip}}
}
#end Code
The purpose of this class is to handle the "E" variable that defines the event arguments, and all we have to do is create one of them. The following is a proxy class (StoreSelectorCommandEventHandler.cs) that defines how events are handled:

#code
public delegate void Storeselectorcommandeventhandler (object sender,
Storeselectorcommandeventargs e);
#end Code
Here is the resulting file:





Now let's adjust the Storeselector control to trigger the event.

The following code needs to be added to the Storeselector control in order to execute the event we created:

#code
public event Storeselectorcommandeventhandler Storeselectorchanged;
protected virtual void onstoreselectorchanged (Storeselectorcommandeventargs e)
{
if (storeselectorchanged!= null) storeselectorchanged (this, e);
}
#end Code
Now that we have defined the event for the control, we need to trigger the event. We plan to trigger the event when the DropDownList onchange event is triggered. Note that the Autpostback property of the DropDownList control is set to true.



The following is the code for the event:

#code
private void Storelist_selectedindexchanged (object sender, System.EventArgs e)
{
data = (DataSet) session["Data"];
Onstoreselectorchanged (
New Storeselectorcommandeventargs
(Data. Tables["Stores"]. Rows[storelist.selectedindex]. Itemarray[0]. Tostring
(),
Data. Tables["Stores"]. Rows[storelist.selectedindex]. ITEMARRAY[1]. ToString (
),
Data. Tables["Stores"]. Rows[storelist.selectedindex]. ITEMARRAY[2]. ToString (),
Data. Tables["Stores"]. Rows[storelist.selectedindex]. ITEMARRAY[3]. ToString (),
Data. Tables["Stores"]. Rows[storelist.selectedindex]. ITEMARRAY[4]. ToString (),
Data. Tables["Stores"]. Rows[storelist.selectedindex]. ITEMARRAY[5]. ToString (
)));
}
#end Code
Let's analyze the work done here. When the SelectedIndexChanged event is triggered, I pass it to the new event created for the control, and the data I transmit is directly related to the dataset that was filled in. All entries are passed to the Storeselectorcommandeventargs object one by one, and then the event is triggered.


To access the new features of the Default.aspx Web page, we need to add an event handler in the OnInit section of the class:




As shown in the figure above, the Storeselectorchanged event appears on the Default.aspx Web page. Here we give it a function. I'll add 6 tabs to the Default.aspx Web page and display the values as DropDownList changes:




Now we're going to write the event.

Beauty is IntelliSense is the principle that IntelliSense recognizes custom EventArg class attributes:




The final event function looks like this:

#code
private void Storeselector1_storeselectorchanged (object
sender, Pubs.Controls.StoreSelectorCommandEventArgs e)
{
Label1.Text = e.stor_id;
Label2.Text = E.stor_name;
Label3.text = e.stor_address;
Label4.text = e.city;
Label5.text = e.state;
Label6.text = E.zip;
}
#end Code
Now we're going to test the project. As the page loads, the reader's mind may flash the idea that it has a problem, but I assure you that there is no problem with the project. If you want to trigger the event when a Web page is loaded, we must set it in the control that has been created by setting the indexed property that is selected in the DropDownList control.




As soon as we choose another store from the DropDownList, the label will change:




Now we Tangacai the table to the first record. We added the following property to the Storeselector control:

#Code
public int SelectedIndex
{
get{return storelist.selectedindex;}
Set
{
if (! Page.IsPostBack)
{
Binddata ();
}
if (Value < StoreList.Items.Count)
{
Storelist.selectedindex = value;
Onstoreselectorchanged (
New Storeselectorcommandeventargs
(Data. Tables["Stores"]. Rows[value]. Itemarray[0]. ToString (),
Data. Tables["Stores"]. Rows[value]. ITEMARRAY[1]. ToString (),
Data. Tables["Stores"]. Rows[value]. ITEMARRAY[2]. ToString (),
Data. Tables["Stores"]. Rows[value]. ITEMARRAY[3]. ToString (),
Data. Tables["Stores"]. Rows[value]. ITEMARRAY[4]. ToString (),
Data. Tables["Stores"]. Rows[value]. ITEMARRAY[5]. ToString ()));
}
}
}
#End Code
Then set the properties of the Page_Load event in Default.aspx:

#code
private void Page_Load (object sender, System.EventArgs e)
{
The code for the user to initialize the Web page
if (! Page.IsPostBack)
{
Storeselector1.selectedindex = 0;
}
}
#end Code
When you run the project, it Tangacai the table to the first record.

Summary

I hope this article can be helpful to the general readers. There is little limit to the development of this type of Web application, and every Web control we create can be used throughout the Web application as long as it is properly designed.


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.