在 C# WebBrowser 中使用只读字段 [英] Using Read-Only Fields in a C# WebBrowser

查看:29
本文介绍了在 C# WebBrowser 中使用只读字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在 C# WinForms 应用程序中使用 WebBrowser 控件,并试图控制此控件呈现的一些可变性.

I'm currently using a WebBrowser control in a C# WinForms application, and attempting to control some variability presented with this control.

基本上,我的用户登录到我的应用程序提供的单独 UI,然后显示 WebBrowser 控件,导航到预定的登录 URL,然后自动填充该页面上的用户名和密码字段.

Basically, my users log in to a separate UI provided by my application, which then displays the WebBrowser control, navigates to a predetermined log-in URL, and then auto-fills the username and password fields on that page.

但是,为了防止此 WebBrowser 控件中出现不可预测的行为,我希望在自动填充这些用户名和密码文本框后将它们设为只读.本质上,我希望用户看到已为他们填写的浏览器页面,并且无法对其进行编辑.(这是为了让我的应用程序可以处理任何身份验证错误,而不是浏览器.)

However, in order to prevent unpredictable behavior in this WebBrowser control, I want to make these username and password text boxes read-only after they are auto-populated. Essentially, I want the user to see a browser page that has been filled out for them, and that cannot be edited. (This is so that any authentication errors can be handled by my application as opposed to the browser.)

我目前用来填充文本字段并使它们只读的代码如下:

The code I'm currently using to populate the text fields and make them read only is as follows:

webBrowser1.Document.GetElementById("username").InnerText = username;
webBrowser1.Document.GetElementById("password").InnerText = password;
webBrowser1.Document.GetElementById("username").Enabled = false;
webBrowser1.Document.GetElementById("password").Enabled = false;

不幸的是,当我尝试将字段设置为只读时,身份验证服务器的行为就像密码字段未填写一样,并在单击提交"按钮后提示用户再次填写.这是预期行为吗?如果是,我可以尝试哪些其他方法来阻止用户更改浏览器自动填充的凭据?

Unfortunately, when I try to make the fields read-only, the authentication server acts like the password field was not filled out, and prompts the user to fill it out again after the "Submit" button is clicked. Is this expected behavior, and if so, what other methods can I try to prevent users from changing the credentials that the browser was auto-populated with?

推荐答案

尝试使它们readonly:

webBrowser1.Document.GetElementById("username").SetAttribute("readonly", "readonly");        
webBrowser1.Document.GetElementById("password").SetAttribute("readonly", "readonly");

这篇关于在 C# WebBrowser 中使用只读字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆