Host. Net Windows Forms in IE .NET Windows Forms

xiaoxiao2021-03-06  61

If you have developed a web application using Java, you will be familiar with Java Applets, which is a small program that is basically run in the browser. When the browser loads an HTML code containing the Java Applets flag, Java Applets will execute, the operation mode of the Windows Forms control in the web page is similar to Java Applets. We can develop Windows Forms controls using the rich classes provided by Windows Forms technology, and then deploy them from the web page. When the browser loads these pages, the code contained in the Windows Forms control is executed.

One of the greatness of .NET is that it provides seamless integration capabilities with IE. For example, we can even activate a Windows Forms control from IE without knowing. Although all features of the Code Access Security provided by the .NET CLR are utilized, this is done without registration.

When developing a Windows Forms control, we have all the features provided by the Windows Forms class by inheritance. For example, we can use Windows Forms Control Verification Technology to extensively verify the data input by the user, in the same manner, we can call remote web services from the Form Control. By using all these technologies, we can use the .NET platform to develop rich and powerful applications.

achieve

In this section, we will learn how to create a simple Windows Forms control and hosted it in IE. Below is 5 steps to activate the Windows Forms control in IE:

· Create a Windows Forms control.

· Create an HTML document that contains an object flag identified by the Windows Forms control.

• Configure the virtual directory you need to correctly activate the control.

· Configure code access.

· Run the control.

Let's learn about every step in the above steps in detail.

Create a Windows Forms control

In this step, we will create a simple Windows Forms control that displays the "Hello World" information to the user. As indicated by pollorating, we will first create a new Visual C # Windows Control Library project called HelloWorldControl.

Once the project is created, we will rename the default user control to HelloWorldctl. We will add a tag control called lblMessage and a button called BTNClick. When the user clicks on the click button, the program executes the following code to display a simple information to the user:

Private void btnclick_click (Object sender, system.eventargs e) {lbldisplayMessage.text = "hello world";

Since the control has been created, let's compile the project and create an executable code.

Creating an HTML page In this step, we will create an HTML document and insert an object flag used to activate the Windows Forms control. The HTML page is as follows:

Hello World Control



in the classid attribute of the object mark, we will specify a full path name, the full name of the control's control library assemblies and controls, including namespaces and controls Class name. From the above code, it can be seen that between the combined body and the control is separated from # 号, the two parameters constitute a unique control identifier that identifies the control. Since it is uniquely identified by the name HelloWorldControl1, we can write a client script code for the control.

Configure virtual directory

Once you have created an HTML code, we can create a new virtual directory called HelloWorldControlHost and add control (HelloWorldControl.dll) and HLLOWORLD.HTM. It is also important to set the execution permission of the virtual directory to Scripts in addition to configuring the virtual directory. If you are executed to Scripts & Executables, the control will not be activated correctly. By opening the Properties window of the virtual directory (below), we can verify the permissions of the virtual directory.

Configuring code access

If the control is on the Internet, it is implemented, but if you want to run the control from the Internet website, you need to configure or modify the security policy to the IE, so that it can run. This can be done by viewing the hosted web page as part of a trusted section. In order to set your site as part of the trusted section, we can use "Tools" -> "Sites" in IE, then add your site to In the list, click the "OK" button. This will then get the correct execution next time, the control is set, because the Internet license has been set.

Runput control

To run the control, just browse the HTML page hosting the control in your browser. In the displayed HTML web page, if you click the "Click Here" command button, the control will display "Hello World" as shown below.

In this example, we primarily exercise how to create a simple Windows Forms control and hosted it in IE. In the next part, we will learn how to use the Windows Forms control from accessing Web services from the client.

The main advantage of accessing the web service Windows Forms control from the Windows Forms control is that it enables us to realize a wealth of user experience on the client. For example, we can access the web services directly from the client, and even display the results to the user even without refreshing the web page. To demonstrate this, we will first create a web service and then call the web service from the Windows Forms control.

Create a web service

First, as shown, we will create a Visual C # ASP.NET web service called AuthorsWebService. After the Web service is created, we can modify the name of the web service class to AuthorsService and add a new method called getAuthors in the AuthorsService class. The GetAuthors method is as follows:

[WebMethod] public DataSet GetAuthors () {// profile obtained from the connection string string connString = System.Configuration.ConfigurationSettings.AppSettings [ "connectionString"]; SqlConnection sqlConn = new SqlConnection (connString); DataSet dstAuthors = new DataSet ( " "); Sqldataadapter adapter = new SqlDataAdapter (" Select * from authors ", sqlconn); // Fill in DataSetAdapter.Fill (DSTAUTHORS, AUTHOR"); // Close and process the opened database connection SQLConn.close (); sqlconn.dispose (); // Returns Authors DataSetReturn Dstauthors to the caller;}

The code of the GetAuthors method is very simple. We first get a connection string from the web.config file, which is stored in the AppSettings section of the web.config file.

After creating the agent, we can start adding the code called the web service, which adds the Click event in the previously added command button. private void btnClick_Click (object sender, System.EventArgs e) {this.Cursor = Cursors.WaitCursor; AuthorsWebServiceProxy.AuthorsService authorsSvc = new AuthorsWebServiceProxy.AuthorsService (); gridAuthors.DataSource = authorsSvc.GetAuthors (); this.Cursor = Cursors.Default }

In the above lines of code, we created an instance of the web service proxy class, then call the GetAuthors method, which gives the DataSour attribute of the DataGrid control from the Web service. Now, the project can be compiled and created the assembly.

The main advantage of accessing the web service Windows Forms control from the Windows Forms control is that it enables us to realize a wealth of user experience on the client. For example, we can access the web services directly from the client, and even display the results to the user even without refreshing the web page. To demonstrate this, we will first create a web service and then call the web service from the Windows Forms control.

Create a web service

First, as shown, we will create a Visual C # ASP.NET web service called AuthorsWebService.

After the Web service is created, we can modify the name of the web service class to AuthorsService and add a new method called getAuthors in the AuthorsService class. The GetAuthors method is as follows:

[WebMethod] public DataSet GetAuthors () {// profile obtained from the connection string string connString = System.Configuration.ConfigurationSettings.AppSettings [ "connectionString"]; SqlConnection sqlConn = new SqlConnection (connString); DataSet dstAuthors = new DataSet ( " "); Sqldataadapter adapter = new SqlDataAdapter (" Select * from authors ", sqlconn); // Fill in DataSetAdapter.Fill (DSTAUTHORS, AUTHOR"); // Close and process the opened database connection SQLConn.close (); sqlconn.dispose (); // Returns Authors DataSetReturn Dstauthors to the caller;}

The code of the GetAuthors method is very simple. We first get a connection string from the web.config file, which is stored in the AppSettings section of the web.config file.

After creating the agent, we can start adding the code called the web service, which adds the Click event in the previously added command button.

private void btnClick_Click (object sender, System.EventArgs e) {this.Cursor = Cursors.WaitCursor; AuthorsWebServiceProxy.AuthorsService authorsSvc = new AuthorsWebServiceProxy.AuthorsService (); gridAuthors.DataSource = authorsSvc.GetAuthors (); this.Cursor = Cursors.Default }

In the above lines of code, we created an instance of the web service proxy class, then call the GetAuthors method, which gives the DataSour attribute of the DataGrid control from the Web service. Now, the project can be compiled and created the assembly.

The main advantage of accessing the web service Windows Forms control from the Windows Forms control is that it enables us to realize a wealth of user experience on the client. For example, we can access the web services directly from the client, and even display the results to the user even without refreshing the web page. To demonstrate this, we will first create a web service and then call the web service from the Windows Forms control.

Create a web service

First, as shown, we will create a Visual C # ASP.NET web service called AuthorsWebService.

After the Web service is created, we can modify the name of the web service class to AuthorsService and add a new method called getAuthors in the AuthorsService class. The GetAuthors method is as follows:

[WebMethod] public DataSet GetAuthors () {// profile obtained from the connection string string connString = System.Configuration.ConfigurationSettings.AppSettings [ "connectionString"]; SqlConnection sqlConn = new SqlConnection (connString); DataSet dstAuthors = new DataSet ( " "); Sqldataadapter adapter = new SqlDataAdapter (" Select * from authors ", sqlconn); // Fill in DataSetAdapter.Fill (DSTAUTHORS, AUTHOR"); // Close and process the opened database connection SQLConn.close (); sqlconn.dispose (); // Returns the AUTHORS DATASetreturn DSTAUTHORS DATASetReturn Dstauthors;} The code of the getAuthors method is very simple. We first get the connection string from the web.config file, which is stored in the web.config file AppSettings In the section.

After creating the agent, we can start adding the code called the web service, which adds the Click event in the previously added command button.

private void btnClick_Click (object sender, System.EventArgs e) {this.Cursor = Cursors.WaitCursor; AuthorsWebServiceProxy.AuthorsService authorsSvc = new AuthorsWebServiceProxy.AuthorsService (); gridAuthors.DataSource = authorsSvc.GetAuthors (); this.Cursor = Cursors.Default In the few lines of code above, we created an instance of the web service proxy class, then call the GetAuthors method, which gives the DataSet from the Web Service to the DataSource property of the DataGrid control. Now, the project can be compiled and created the assembly.

转载请注明原文地址:https://www.9cbs.com/read-113481.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.038, SQL: 9