开发者

ASP.NET TextBox (HTML input field) populates with username automatically when form loads

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. :-\

ASP.NET TextBox (HTML input field) populates with username automatically when form loads

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??

ASP.NET TextBox (HTML input field) populates with username automatically when form loads

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

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>&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>

ASP.NET TextBox (HTML input field) populates with username automatically when form loads

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>


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

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


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


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>


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."


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜