表单加载时,ASP.NET TextBox(HTML 输入字段)自动填充用户名

发布于 2024-11-27 11:05:07 字数 5646 浏览 1 评论 0原文

我的表单中有一个 TextBox 控件,当 HTML 表单呈现时,该控件仍在提取数据。我尝试将 AutoCompleteType 设置为“None”,但我认为这只是控制它是否会找到该字段之前输入的数据,而不是页面加载时实际填充到该输入字段中的数据。为什么这个文本框会拉入数据?这引发了另一个更大的问题。此文本框位于控件(*.ascx 文件)内部。它是从另一个控件动态加载的——不确定这是否重要。这种情况只发生在 Mozilla Firefox 中。当我在包含 TextBox 的控件的 Page_Load 事件中检查 txtKeywords.Text 时,该值为 null。所以该值显然来自浏览器,而不是服务器。什么会导致这个?

<asp:TextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" autocomplete="False"></asp:TextBox>

渲染的 HTML:

<input type="text" style="width: 125px;" autocomplete="False" id="ExplorerPageHtmlLeft_ctl01_txtKeywords" name="ExplorerPageHtmlLeft$ctl01$txtKeywords" gtbfieldid="77">

背后的父控件代码(searchPanel1 控件包含 TextBox):

    Private Sub Page_Load(ByVal sender As Object, _
                          ByVal e As EventArgs) _
            Handles MyBase.Load
        navigation1 = CType(LoadControl("ExplorerNavigation1.ascx"), ExplorerNavigation1)
        searchPanel1 = CType(LoadControl("SearchPanel.ascx"), SearchPanel)
        navigation2 = CType(LoadControl("ExplorerNavigation2.ascx"), ExplorerNavigation2)

        Me.PlaceHolder1.Controls.Add(navigation1)
        Me.PlaceHolder1.Controls.Add(searchPanel1)
        Me.PlaceHolder1.Controls.Add(navigation2)
    End Sub

在 Massimiliano Peluso 发表评论后,我还没有找到任何可疑的 JavaScript。但是,我确实注意到此输入字段中的值始终与用户的 ID 相同。当我检查 Firebug 中的元素时,TxtUser 字段上的“gtbfieldid”属性的值似乎总是在变化。此 TxtUser 字段始终与我们的 txtKeywords 输入字段不同。它们位于不同的页面上。而且它们似乎每次都会填充不同的值。所以我没有看到那里的相关性。我还是不知道这个属性有什么用。此属性不会在 Internet Explorer 中显示,因此由于某种原因必须将其插入到 Firefox 中。

<input type="text" id="TxtUser" name="TxtUser" gtbfieldid="37">

感谢 Johnny #5 的输入,我们证明服务器没有设置此文本。这不是我问题的答案,但这非常有帮助。我在设置器中放置了一个断点,但它没有被调用。只有吸气剂被调用。澄清一下,这是一个自定义 Web 服务器控件(.vb 文件),不要与自定义用户控件(.ascx 文件、*.ascx.vb 文件,有时还有 *.ascx)混淆。 Designer.vb 文件)。我向此类添加了 Render 事件。我还必须导入 2 个命名空间。 我想弄清楚的下一件事是一种能够扩展浏览器在此字段中设置文本的能力的方法(通过客户端),就像我们下面为服务器端所做的那样。对于此类问题来说,这将是一个非常棒的方法。但不确定 Firebug 是否具有该功能。我还是不知道这段文字是怎么设置的。所以我的问题仍然悬而未决。

Imports System.Web
Imports System.Web.UI.WebControls

Public Class MyTextBox
    Inherits System.Web.UI.WebControls.TextBox

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
    End Sub

End Class

在我实现此自定义 Web 服务器控件的页面中,我需要注册此类。

<%@ Register Assembly="Acu.LIMS.UI.Web" Namespace="Acu.LIMS.UI.Web" TagPrefix="MyTextBox" %>

<MyTextBox:MyTextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></MyTextBox:MyTextBox>

还好我没疯!这不是服务器端代码,也不是客户端代码导致此问题。这是 Mozilla Firefox 的 >选项>安全>记住密码复选框会导致问题。但我还是很困惑。 :-\

在此处输入图像描述

在导致问题的计算机上,我不断切换此复选框,并且无法获取浏览器问我这个问题:“您希望 Firefox 记住您的密码吗?” (不记得确切的措辞)。但在另一台计算机上,我立即被问到。当我单击“记住”并转到该页面时,“x”显示在此输入字段(又名 TextBox Web 控件)中。我的用户名和密码都是“x”,所以我不确定它在拉哪个。尽管它提取了值,但我的罪魁祸首 TextBox 没有 id(用户 id 或密码),因为它是 id。 下一个问题:Firefox 如何确定何时填写用户名和密码? 此表单的一个有趣之处是另一个字段确实有密码文本框,但我在此表单中没有用户 ID 。我想知道 Firefox 是否将用户名放在它找到的第一个字段中?

在此处输入图像描述

======================== =

答案

所以我在 SearchPanel 控件中放入了一个虚拟字段,并猜测我的用户名填写在哪里。我正在阅读有关 Mozilla 密码管理器的链接,显然 Mozilla 使用其智能来确定放置用户的最佳位置id(有时称为用户名)。就我而言,我的身份验证控件中有一个用户名标签(不是用户 ID),但我不需要用户 ID 字段。由于用户已经登录,因此他们不应输入任何用户 ID。这就是为什么用户名标签位于密码字段前面的原因。我们审核应用程序中的更改,因此这就是将其包含在应用程序中的目的。

https://wiki.mozilla.org/Firefox%3aPassword_Manager

所以我做了一个测试来验证Mozilla 的功能。我在旧的罪魁祸首 TextField 之后放置了另一个虚拟字段(ID 为“txtKeywords”,因此它的位置是密码输入字段之前的最后一个输入字段。猜猜 Mozilla Firefox 将用户 ID 放在哪里?是的,在虚拟 TextBox/input 中当我关闭 Mozilla > 选项 > 安全下的“记住网站密码”选项时,它不会填充,我的密码字段也不会填充。我想现在,我将在我的身份验证控件中放置一个隐藏的 TextBox 字段,以便 Mozilla 填充该字段,但

SearchPanel.ascx 控件:

<tr>
    <td nowrap class="categoryverticalNav">
        <asp:Label id="lblSearch" runat="server">Search</asp:Label><BR>
        <asp:DropDownList id="ddlCategory" runat="server" Width="164px"></asp:DropDownList><BR>
        <asp:TextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></asp:TextBox>&nbsp;
        <asp:TextBox id="TextBox1" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></asp:TextBox>&nbsp;
        <asp:Button id="btnGo" runat="server" Width="28px" Text="Go" CausesValidation="False" />
    </td>
</tr>

在此处输入图像描述

是的,放入隐藏的输入字段解决了这个问题。我想我也可以放入文本框,但我只是选择了一个输入字段和runat="server".. 因为 CLR 不需要在 ASP.NET 中与它交互。一旦我这样做了,“x”(用户 ID)就从 SearchPanel 控件 txtKeywords 输入字段中消失了。 :-)

<td>
    <b><asp:Label ID="LblUserDisplayName" Runat="server" EnableViewState="False"></asp:Label></b>
</td>
<td>Password</td>
<td>
    <input runat="server" type="text" style="display:none;">
    <asp:TextBox id="TxtPassword" Runat="server" Width="80" EnableViewState="False" TextMode="Password"></asp:TextBox>
</td>

I have a TextBox control in a form which is still pulling in data when the HTML form renders. I tried setting the AutoCompleteType to "None" but I think that just controls whether or not it will find previously entered data for that field, not what actually fills into that input field when the page loads. Why would this textbox be pulling in data? It's causing another larger issue. This TextBox is inside of a control (*.ascx file). It's loaded from another control dynamically--not sure if that matters. It's only happening in Mozilla Firefox. When I check txtKeywords.Text in the Page_Load event of the control that contains the TextBox, the value is null. So the value is obviously coming from the browser, not the server. What would cause this??

<asp:TextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" autocomplete="False"></asp:TextBox>

Rendered HTML:

<input type="text" style="width: 125px;" autocomplete="False" id="ExplorerPageHtmlLeft_ctl01_txtKeywords" name="ExplorerPageHtmlLeft$ctl01$txtKeywords" gtbfieldid="77">

Parent control code behind (searchPanel1 control contains the TextBox):

    Private Sub Page_Load(ByVal sender As Object, _
                          ByVal e As EventArgs) _
            Handles MyBase.Load
        navigation1 = CType(LoadControl("ExplorerNavigation1.ascx"), ExplorerNavigation1)
        searchPanel1 = CType(LoadControl("SearchPanel.ascx"), SearchPanel)
        navigation2 = CType(LoadControl("ExplorerNavigation2.ascx"), ExplorerNavigation2)

        Me.PlaceHolder1.Controls.Add(navigation1)
        Me.PlaceHolder1.Controls.Add(searchPanel1)
        Me.PlaceHolder1.Controls.Add(navigation2)
    End Sub

I couldn't find any suspicious JavaScript yet, after Massimiliano Peluso's comment. However, I did notice that the value in this input field is ALWAYS the same as the user's id. The value of the "gtbfieldid" attribute on the TxtUser field always seems to change when I inspect the element in Firebug. This TxtUser field is always different from our txtKeywords input field. They are on different pages. And they seem to populate with different values each time. So I didn't see a correlation there. I still don't know what this attribute is for. This attribute doesn't show in Internet Explorer, so it must be inserted into Firefox for some reason.

<input type="text" id="TxtUser" name="TxtUser" gtbfieldid="37">

Thanks to Johnny #5's input, we proved that the server was not setting this text. This is not the answer to my question, but this was very helpful. I put a breakpoint into the setter, and it did not get called. Only the getter got called. To clarify things, this is a custom web server control (.vb file), NOT TO BE CONFUSED with a custom user control (.ascx file, *.ascx.vb file and sometimes *.ascx.Designer.vb file). I added Render event to this class. I also had to import 2 namespaces. The next thing I'd like to figure out is a way to be able to extend the browser's ability to set text in this field (via client side), like we did below for the server side. That would be a frickin' awesome methodology for this type of problem. Not sure if Firebug has that capability though. I still don't know what's setting this text. So my question is still up for grabs.

Imports System.Web
Imports System.Web.UI.WebControls

Public Class MyTextBox
    Inherits System.Web.UI.WebControls.TextBox

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
    End Sub

End Class

In my page where I'm implementing this custom web server control, I need to register this class.

<%@ Register Assembly="Acu.LIMS.UI.Web" Namespace="Acu.LIMS.UI.Web" TagPrefix="MyTextBox" %>

<MyTextBox:MyTextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></MyTextBox:MyTextBox>

Well, I'm not crazy! It's not server side code and it's not client side code causing this. It's Mozilla Firefox's > Options > Security > Remember password checkbox causing the problem. BUT I'M STILL CONFUSED. :-\

enter image description here

On the computer causing the issue, I kept toggling this checkbox, and could not get the browser to ask me the question, "Do you want Firefox to remember your password?" (don't remember the exact wording). But on another computer, I got asked immediately. And when I clicked "Remember" and went to that page, "x" shows in this input field (aka TextBox web control). Both my user name and password are both "x", so I'm not sure which it is pulling. Despite the value it pulls, my culprit TextBox doesn't have either id (of the user id or password) as it's id. Next question: How does Firefox determine when to fill in the username and password? One interesting thing about this form is that another field does have the password textbox, but I DO NOT have the user id in this form. I wonder if Firefox puts the user name in the first field it finds??

enter image description here

=======================

Answer

So I put in a dummy field in the SearchPanel control, and guess where my user name filled in. I was reading on this link about the Mozilla Password Manager, and apparently Mozilla uses its intelligence to determine the best place to put the user id (sometimes referred to as user name). In my case, I have a label for the user name (not the user id) in my Authentication control, but I don't want a user id field. Since the user is already logged in, they shouldn't be entering any user id. So that's why the user name label is in front of the password field. We audit changes in our application, so that is the purpose for having it inside the application.

https://wiki.mozilla.org/Firefox%3aPassword_Manager

So I did a test to validate Mozilla's functionality. I put another dummy field after the older culprit TextField (with id "txtKeywords", so that it's placement is the last input field preceding the password input field. And guess where Mozilla Firefox put the user id? Yep, in the dummy TextBox/input field. When I turn off the "Remember password for sites" option under Mozilla > Options > Security, it doesn't populate, nor does my password field. So this is my answer. I guess for now, I'll just put a hidden TextBox field in my Authentication control so Mozilla populates that one instead, but is not visible.

SearchPanel.ascx control:

<tr>
    <td nowrap class="categoryverticalNav">
        <asp:Label id="lblSearch" runat="server">Search</asp:Label><BR>
        <asp:DropDownList id="ddlCategory" runat="server" Width="164px"></asp:DropDownList><BR>
        <asp:TextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></asp:TextBox> 
        <asp:TextBox id="TextBox1" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></asp:TextBox> 
        <asp:Button id="btnGo" runat="server" Width="28px" Text="Go" CausesValidation="False" />
    </td>
</tr>

enter image description here

Yep, putting in a hidden input field solved it. I suppose I could have either put in a TextBox also, but I just chose an input field with runat="server".. since the CLR doesn't need to interact with it in ASP.NET. Once I did that, the "x" (user id) disappeared from the SearchPanel control txtKeywords input field. :-)

<td>
    <b><asp:Label ID="LblUserDisplayName" Runat="server" EnableViewState="False"></asp:Label></b>
</td>
<td>Password</td>
<td>
    <input runat="server" type="text" style="display:none;">
    <asp:TextBox id="TxtPassword" Runat="server" Width="80" EnableViewState="False" TextMode="Password"></asp:TextBox>
</td>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城挽邺 2024-12-04 11:05:07

简单的解决方案:将文本框的自动完成属性设置为“关闭”

<asp:TextBox id="TxtPassword" Runat="server" Width="80" autocomplete="off" TextMode="Password"></asp:TextBox>

Simple solution: set autocomplete property of text box to "off"

<asp:TextBox id="TxtPassword" Runat="server" Width="80" autocomplete="off" TextMode="Password"></asp:TextBox>
默嘫て 2024-12-04 11:05:07

在ASP.NET生命周期中,有两个事件负责回发后维护数据。一个是LoadViewState,另一个是LoadPostBackData。如果实现 IPostBackDataHandler 接口的控件通过 LoadPostBackData 事件中的 Http Post 数据的值加载。 TextBox 控件不从视图状态获取其值,而是从该事件中表单中的发布数据获取其值。因此,即使您禁用 TextBox 控件的视图状态,如果它实现 IPostBackDataHandler 接口,它也可以从 HTTP post 数据中获取其值。

If Page.IsPostBack(){
     yourTextBox.Text=string.empty;
}

http://forums.asp.net/t/1104194.aspx/1

还可以看看下面的内容:

http://www.codeproject.com/KB/aspnet/ASPViewStateandPostBack.aspx

In the ASP.NET life cycle, there are two events which are responsible for the maintain data after postback. One is LoadViewState and another is LoadPostBackData. If a control which implement IPostBackDataHandler interface gets loaded by the values from Http Post data in the LoadPostBackData event. A TextBox control does not get its value from the view state but from the post data in the form in this event. So even if you disable view state for the TextBox control, it can get its value from the HTTP post data if it implements IPostBackDataHandler interface.

If Page.IsPostBack(){
     yourTextBox.Text=string.empty;
}

http://forums.asp.net/t/1104194.aspx/1

have also a look at the below:

http://www.codeproject.com/KB/aspnet/ASPViewStateandPostBack.aspx

酒与心事 2024-12-04 11:05:07

如果您想知道文本是否在服务器端设置,您可以尝试以下操作:

public class MyTextBox : System.Web.UI.WebControls.TextBox
{
      public override string Text
      {
         get
         {
            return base.Text;
         }
         set
         {
            base.Text = value;
         }
      }
}

在用户控件中使用此文本框而不是原始文本框,然后在设置器上放置一个断点,并在出现时检查调用堆栈那里。

MacGyver 的注释:

添加一些说明。这并没有回答我的问题,但对于证明文本没有在服务器端设置非常有用。这是自定义 Web 服务器控件(.vb 文件),不要与自定义用户控件(.ascx 文件、*.ascx.vb 文件,有时还有 *.ascx.Designer.vb 文件)混淆)。我向此类添加了 Render 事件。我还必须导入 2 个命名空间。

导入系统.Web
导入 System.Web.UI.WebControls

公共类 MyTextBox
继承 System.Web.UI.WebControls.TextBox

Public Overrides Property Text() As String
    Get
        Return MyBase.Text
    End Get
    Set(ByVal value As String)
        MyBase.Text = value
    End Set
End Property

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    MyBase.Render(writer)
End Sub

End Class

在我实现此自定义 Web 服务器控件的页面中,我需要注册此类。

<%@ Register Assembly="Acu.LIMS.UI.Web" Namespace="Acu.LIMS.UI.Web" TagPrefix="MyTextBox" %>

<MyTextBox:MyTextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></MyTextBox:MyTextBox>

If you want to find if the text is setted on server side, you could try this :

public class MyTextBox : System.Web.UI.WebControls.TextBox
{
      public override string Text
      {
         get
         {
            return base.Text;
         }
         set
         {
            base.Text = value;
         }
      }
}

Use this text box inside your user control instead of the original one, then put a break point on the setter, and check the call stack when you got there.

MacGyver's Notes:

Adding some clarification. This does NOT answer my question, but was VERY useful in proving that the text was not getting set on the server side. This is a custom web server control (.vb file), not to be confused with a custom user control (.ascx file, *.ascx.vb file and sometimes *.ascx.Designer.vb file). I added Render event to this class. I also had to import 2 namespaces.

Imports System.Web
Imports System.Web.UI.WebControls

Public Class MyTextBox
Inherits System.Web.UI.WebControls.TextBox

Public Overrides Property Text() As String
    Get
        Return MyBase.Text
    End Get
    Set(ByVal value As String)
        MyBase.Text = value
    End Set
End Property

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    MyBase.Render(writer)
End Sub

End Class

In my page where I'm implementing this custom web server control, I need to register this class.

<%@ Register Assembly="Acu.LIMS.UI.Web" Namespace="Acu.LIMS.UI.Web" TagPrefix="MyTextBox" %>

<MyTextBox:MyTextBox id="txtKeywords" runat="server" Width="125px" AutoCompleteType="None" AutoComplete="False"></MyTextBox:MyTextBox>
一张白纸 2024-12-04 11:05:07

请参阅我的问题底部的答案。一切都解释清楚了。本质上,Mozilla Firefox 密码管理器将用户 ID 放入存储输入字段之前的最后一个输入字段中(基于 Mozilla Firefox 密码管理器中存储的内容)。因此,我将在身份验证控件中放置一个隐藏的虚拟文本框(或输入)字段,该字段位于密码输入字段之前。

这篇文章有一个引述可以解释它,但我想我有一个更好的解释。

https://wiki.mozilla.org/Firefox%3aPassword_Manager

引用上面的链接:
“然后使用用户名字段/密码字段值作为提示,通过将它们与“名称”属性相匹配来查找网页中的适当元素。”

See the bottom of my question for the Answer. It's all explained. Essentially Mozilla Firefox Password Manager places the user id into the last input field preceding the stored input field (based on what's stored in the Mozilla Firefox Password Manager). So I'm just going to place a hidden dummy textbox (or input) field inside my Authentication control that precedes my password input field.

This article has one quote that sort of explains it, but I think I have a better explanation.

https://wiki.mozilla.org/Firefox%3aPassword_Manager

Quote from link above:
"Then uses the usernamefield/passwordfield values as hints to find the appropriate elements within a webpage by matching them to the "name" attribute."

水波映月 2024-12-04 11:05:07

复制&将以下代码粘贴到页面标题部分。

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/jscript">
$(document).ready(function () {
setTimeout(function () {
$("input[type='password'],input[type='text']").val('');
}, 50);
}); 

</script>

Copy & paste below code into your page head section.

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/jscript">
$(document).ready(function () {
setTimeout(function () {
$("input[type='password'],input[type='text']").val('');
}, 50);
}); 

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文