Use Windows Forms control in IE browser

Source: Internet
Author: User

1. Introduction <from> http://blog.csdn.net/zhzuo/archive/2004/05/10/22032.aspx

In the past, web programmers often used ActiveX controls in their web programs to implement features that some fat clients have. But now developers can easily use Microsoft. NET Framework to establish objects in a concise and secure manner, and facilitate the use in IE browsers. By creating a Windows Form Control in IE browser, developers can implement a wide range of web clients. In this article, we will know how to create Windows Forms controls and use them in IE. When using the form control in a browser, we will demonstrate how to use the form control to provide rich Web Client display, and the form control is used to call remote web service to obtain data. At the same time, we will also know how to use the. Net security model to provide a secure running environment for our form controls.

If you have experience developing web programs using Java, you will be familiar with Java applets. It is a small program running in the browser. When the browser opens an HTML document containing the applet tag, Java applets runs. Windows Forms work in the same way as Java applets on Web pages. In this implementation, you create a form control, add rich windows Form Control styles to it, and then go to the web page. When the browser loads the web page, the Code contained in the Windows Control is also executed. This will be useful in LAN or extended network programs. For example, you want to develop a program that has the characteristics of a fat client and web.

. NET is closely integrated with IE. For example, we can use the Windows Form Control in IE, without notifying users to add other things. You do not need to register any. Net CLR to provide secure code access.

When you create a Windows form control, you will have the features provided by the Form class hierarchy. For example, you can use the Windows Form Control Verification Technology to verify the accuracy of input data. Similarly, you can even call web service in your form control. With these technologies, you can create rich, robust, and dynamic applications of the base and. Net platforms.

Note: The code is rewritten according to the original text, and the path is replaced with Chinese characters. runtime environment: Windows ,. net Framework 1. 1. here are some points: Open the browser during debugging and enter the complete address. If opened as a file, the form control is not displayed. For process debugging, it can be a single source file. However, I am running components generated in the debug version, not release. If you are interested, try it.

Ii. Execution

In this section, we will understand how to create a Windows form control and use it in IE. Five steps are listed below to complete this process.

1. Create a Windows Forms Control
2. Create an HTML document with the object tag to identify Windows controls
3. Configure the virtual path of the control
4. Set Code Access Permissions
5. Run the control

Next let's take a look at the above steps.

1. Create a Windows Form Control

In this step, we create a simple windows form control. The control displays "Hello World" to users. Create a Windows control library project named helloworldcontrol and change the default user control to helloworldctl. Then add a label to the control, named lbldisplaymessage, and added a button btnclick. When you click the button, the following code is executed to display the message to the user.

Private void btnclick_click (Object sender, system. eventargs E)

{

This. lbldisplaymessage. Text = "Hello World ";

}

The next step is to compile and generate an assembly.

 

2. Create an HTML page

In this step, we will create an HTML document and insert the object tag in it. The entire document looks as follows:

<HTML>
<Body>
<P> Hello World Control
<Object ID = "helloworldcontrol1" Height = "300" width = "300" classid = "http: helloworldcontrol. dll # helloworldcontrol. helloworldctl"
Viewastext>
</Object>
<Br>
</P>
</Body>
</Html>

In the classid attribute of this label, we specify the path of the component created in step 1 and the complete qualified name of the component. The complete name includes the namespace and the entire class name. In the above code, we can clearly see that the component names and qualified names are separated. These two parameters are combined to identify the uniqueness of the control. Of course, you can write client scripts by naming the unique ID helloworldcontrol1.

3. Configure the virtual path

With the HTML page, create a virtual directory of helloworldcontrolhost. The actual directory is D: \ My Programs \ hellpworldhost. the directory contains the helloworldcontrol.dlland hellpworld.htm files. When setting a virtual directory, you must note that the execution permission is set to "Pure script ". If the permission is set to "script and executable script. The control may not work normally (I set it to "script and executable script" under IIS6, which is not normal ). You can open the properties of the virtual directory to confirm the settings. The setting window is as follows:

 

4. Set Code Access Permissions

If the Web is accessed in the LAN, it will work normally. If the Web is accessed through the Internet, you need to set Internet Explorer and modify the security attributes to allow it to run. You can add a home page to a trusted site. If you need to set it, you can choose ie tools> Internet Options> Security> trusted sites, and click the site button above to add your website to it. Next, when you open the browser, you will see the correct execution.

5. Run the control

To run this control, we can view the HTML page containing the control in the browser. If you click this button on the page, the control will display "hellp world" information. The execution page is as follows:

 

In this example, we create a Windows form control and display it in IE browser. In the next section, we use the form control on the client browser to access the Web service.

 

3. Access web service through Windows Forms Control

An important advantage of form control is that you can implement rich user information on the client. For example, you can use the form control to access web services and display them in IE without refreshing the page again. To demonstrate this, we first create a web service and then demonstrate how to call the Web service through form controls.

Create a Web Service

Create a new project of Visual C # Asp.net web service named authorswebservice.

After the service class is created, we change the service class name to authorsservice, and add a getauthors Method to the class. The code for this method is as follows:

[Webmethod]

Public dataset getauthors ()

{

String connstring = system. configuration. configurationsettings. etettings ["connectionstring"];

Sqlconnection sqlconn = new sqlconnection (connstring );

Dataset dstauthors = new dataset ("Authors ");

Sqldataadapter adapter = new sqldataadapter ("select * from authors", sqlconn );

Adapter. Fill (dstauthors, "author ");

Sqlconn. Close ();

Sqlconn. Dispose ();

Return dstauthors;

}

The code of the above method is relatively simple. we store the database connection string to the appsettings node of the web. config file, as follows:

<Deleetask>

<Add key = "connectionstring" value = "Server = localhost; uid = sa; Pwd = thiru; database = pubs"> </Add>

</Appsettings>

 

In the code above,
We created a sqlconnnection instance and passed in the above connection string as a parameter. Create a sqldataadapter object and input two parameters: query string and sqlconnnection instance. Call the fill method of the sqldataadapter instance to execute database queries and fill in the results to dataset. Now we have created a web service, And next we will create a client call for it.

Create a Windows form control as a Web service client

Here, we want to use a form control to call web service, so we create a new Visual C # form control project named authorswebserviceclientcontrol :.

After that, change the default user control name to authorscontrol .. We added a DataGrid Control named gridauthors and a button named btnclick. Click Event of the Registration button. WebService is called in the event processing function. Before that, we need to add the project's web reference and enter the Web service address we just created. The editor generates a proxy for the service and adds a web service reference as follows:

Figure

 

After the Service proxy is created, add the code in the button event:

Private void btnclick_click (Object sender, system. eventargs E)

{

This. cursor = cursors. waitcursor;

Authorswebserviceproxy. authorsservice authorssvc = new

Authorswebserviceproxy. authorsservice ();

This. gridauthors. datasource = authorssvc. getauthors ();

This. cursor = cursors. default;

}
In the code above, we created an instance of the Web Service proxy and called the getauthors method to assign the returned dataset to the datasource attribute of gridauthors. Compile the Form Control and configure the virtual directory.

Create HTML pages and create virtual paths

In this step, we create an HTML page to use the authorswebserviceclientcontrol created above. The following is his code:

<HTML>
<Body>
<P> authors Display Control <br>
<Object ID = "authorscontrol1"
Classid = "http: authorswebserviceclientcontrol. dll # authorswebserviceclientcontrol. authorscontrol"
Height = "500" width = "500" viewastext>
</Object>
<Br>
</Body>
</Html>

Now, you need to create a virtual directory to make the control work normally and put authorsdisplay.htm and authorswebserviceclientcontrol. dll together. Open the browser and enter the address. You will see a button and an empty DataGrid. If you click this command button, the control will call the web service and write the result to the DataGrid. The page results are as follows.

In the next section, let's take a look at Debugging Processes.

4. debug Windows Forms controls

To debug the Form Control, follow these steps.

1. Open the browser and request the HTML page containing the Form Control.

2. Open Visual Studio. NET and select tool> debug process. The following dialog box is displayed.

 

 

In the process dialog box, select iexplore. EXE and click the Add button. When you click the Add button, a dialog box pops up prompting you to select the program type to debug. We chose the common language runtime and script items, for example:

 

Close the preceding window and process window.

Use the File menu of vs.net to open the user control file authorswebserviceclient. CS and set breakpoints in the source code. In this way, once you set a breakpoint, You can debug the control, as shown in.

 

Windows form code access permission

As we have discussed before. When the Form Control is executed in IE, it uses the permissions provided by. Net runtime. To understand how to use the code access security provided by the. NET runtime to run the control in IE, let's go back to the front and add the following code to the form control load event:

Private void authorscontrol_load (Object sender, system. eventargs E)

{

If (! EventLog. sourceexists ("testsource "))

EventLog. createeventsource ("testsource", "testlog ");

Else

{

EventLog. deleteeventsource ("testsource ");

EventLog. createeventsource ("testsource", "testlog ");

}

}

In the code above, we can determine whether the "testlog" log source exists on the Web server. If it does not exist, we can create it and delete and recreate it if it exists. As you expected, you need more permissions to execute the code, so the above Code will not work normally through the Internet. You will see the following prompt window.

 

In the above window, we can clearly see that your access is restricted by. Net runtime.

When using a form control in IE, you need to be aware of its advantages and limitations. Main advantages include:

1. Rich dynamic interfaces are implemented through the Web.
2. seamless integration with. Net code security policies.
3. better performance than Java applets.

Restrictions include:

1. The client must be a Windows Operating System

2. Support for ie6.0 is required

3. The client needs to install the. NET Runtime Environment

4. The server must be window2000, iis5.0, or later.

 

Due to the above restrictions, it may be advantageous for data transmission between the client and the server. For example, the Windows Forms control must be installed on the client in IE. net runtime, we can write some code to determine whether the client is installed. net runtime. We can obtain the version number through this feature. Otherwise, we will get 0.0.

V. Conclusion

 

In this article, we discuss how to use browser forms controls in IE and debug them in the process. At the same time, we also talked about the. NET code access security control in IE.

Although the implementation of this technology requires the client to install. Net runtime, we can believe that in the future Windows operating system will integrate. NET Framework, Windows2003 is a good example.

Finally, I hope you will find this article helpful and thank you for reading it.

 

About the author

 

Thiru has almost six years of experience in grouping ting, designing, developing and implementing applications using object oriented application development methodologies. he also possesses a thorough understanding of software life cycle (design, development and testing ).

 

He is an expert with ASP. net ,. net Framework, Visual C #. net, Visual Basic. net, ADO. net, XML Web Services and. net remoting and holds mcad. net, MCSD and MCP certifications.

 

Thiru has authored numerous books and articles. He can be reached at thiruthangarathinam@yahoo.com.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhzuo/archive/2004/05/10/22032.aspx

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.