If I add Layout Template element like the following code, I am not able to select element directly.
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
<LayoutTemplate>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
</LayoutTemplate>
</asp:Login>
Instead, I need to select element like this:
String userName = LoginUser.UserName.Text;
Actually, what is the use of LayoutTemplate and why does it cause me not able to select element directly?
LayoutTemplate helps you designing multiple layouts.
You can have more than 1 layout.
The LayoutTemplate is only here to customize the appearance of the Login Control. The Login Control encapsulates the inner Control like an Placeholder. Maybe you want to use the Authenticate Event to get the username and password while verifying the userdata.
Cato, i have a similar question. but i was able to get the username and password text values with this code:
string userName = LoginUser.UserName;
string passWord = LoginUser.Password;
That 'UserName' and 'Password' property on LoginUser are string values, not the elements themselves, which is my problem. I need to get the element because i want to set focus on the username element. But Microsoft seems to have put us between a rock and a hard place, yet again, on this issue.
But it seems to me, that the string values are what you were looking for anyways. So maybe my response answers your question.
Related
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. :-\
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??
=======================
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>
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>
I feel like I recall from days gone by an article explaining how to DECLARATIVELY set the value of the property of an object, exposed as the property of an ASP.NET User Control.
My situation is that I have a user control which contains a LinkButton, amongst other things, of course. I would like the consumer of the User Control to be able to set the TEXT value of the link button in the declarative syntax used to implement the User Control.
Here is the User Control (designer)...
<div id="toolbar">
<ASP:LinkButton runat="server" id="btnFirst" />
<ASP:LinkButton runat="server" id="btnSecond" />
<ASP:LinkButton runat="server" id="btnThird" />
<ASP:LinkButton runat="server" id="btnFourth" />
</div>
Here is the property as defined in the code behind of the User Control ...
public partial class Lookuptoolbar: UserControl
{
public LinkButton FourthButton
{
get { return (this.btnFourth); }
}
}
When I include the control in a page I EXPECTED to be able to set the TEXT of my FOURTH button using the following DECLARATIVE syntax ...
<UC:MyControl id="uc1" runat="server" FourthButton_Text="Click Me!"/>
I had read once somewhere, a long time ago, that you could access the properties of an object (exposed as the property of a user/server control) by using the underscore syntax. This is not working for me at all. Is this no longer allowed or am I missing something? IS there ANY way of doing this?
Thank You,
Gary
Ok ... for any who might be interested I think I have found the answer, or at least the beginnings of it. The syntax would be with a HYPHEN and not an underscore. So the correct syntax would be.
<UC:MyControl id="uc1" runat="server" FourthButton-Text="Click Me!"/>
When accessing the "subproperties" of complex types there may be more to it; I haven't gone into it in detail, but the book "Developing Microsoft ASP.NET Server Controls and Components" by Microsoft Press (ISBN 0-7356-1582-9) discusses this on pages 218-222.
If anybody learns anything more I would love to hear about it, otherwise I hope that this helps somebody out there!
-Gary
I have never heard of or seen anybody use the method you talk about (using underscore for accessing object properties in the declarative syntax).
One way to do what you want would be to expose a FourthButtonText property on the user control that interacts with the LinkButton's text property:
public string FourthButtonText
{
get { return this.btnFourth.Text; }
set { this.btnFourth.Text = value; }
}
This is a strange one to explain but hope I make sense.
Our organisation has a library of custom controls that we use in our solutions. One example of these controls is a textbox combined with a set of validators which can be configured appropriately by its properties set in the markup.
I now have a problem when using this control in (which I beleive to have narrowed it down) a TabContainer.
If I wanted to use the following markup in the container:
<scc:TextBox ID="txtEmailAddr" runat="server" CssClass="input EmailAddress" EnforceEntry="EmailAddress"
ErrorMessage_RequiredFieldNotCompleted="" ErrorMessage_ShowExclamation="true"
MaxLength="150" ShowErrorMessageBelow="false" Label="Email Address " />
When I save or reload the .aspx markup it then renders the following markup for the same control:
<scc:TextBox ID="txtEmailAddr" runat="server" CssClass="input EmailAddress" EnforceEntry="EmailAddress"
ErrorMessage_RequiredFieldNotCompleted="" ErrorMessage_ShowExclamation="True"
MaxLength="150" ShowErrorMessageBelow="False" Label="Email Address "
ClientSidePreventInvalidChars="True" EnableClientScript="True"
EnfoceOnPaste="False" EnforceMaxLengthWithRXOnMultiline="True"
EnforceOnPaste="False" EnforceSpaceInPostcode="True"
ErrorMessage_InvalidFormat="Email Address : Please enter a valid email address"
ErrorMessage_NumericValueInvalidOrOutOfRange="Email Address requires a number to be entered in the range to ."
GuidanceText="" GuidanceText_RenderInMouseoverPanel="False"
JavascriptURL="~/Include/TextBoxMaximumLength.js" LabelBold="False"
LabelCSSClass="" MaxValue="9999999" MinValue="-9999999" Read_Only="False"
RememberAnswer="False" RenderInParagraphs="True"
RenderRequiredTextForRequiredFields="True" Required="True"
RequiredField_InitialValue="" Rows="0" ShowMaxLength="False" Text=""
TextBox_TabIndex="0" TextboxSkinID="" TextMode="SingleLine"
TooltipPopup_BodyText="" TooltipPopup_TooltipText="(guidance)"
ValidationGroup="" ValidationExpression="" />
This would not be a problem other than the properties that are now being rendered in the markup are overriding default functionality of the actual control. In this case the default Email Address regular expression is being ignored because the property 'ValidationExpression' is being set to an empty string!
Again I could place the default regex in that property, but I would just like to understand why the markup is behaving in this manner?
Thanks.
Get the code for the AjaxContolToolkit and step through it to see why all properties are rendered. You can adjust that code as you need and compile the dll and use that. From personal experience, that is the only way I have found use for the Toolkit because of behaviors like you describe.
I'm working in ASP.NET and I have a CheckBoxList where I want one of the options to be basically like "Other: _." So I need to include a textbox where the user can fill in their own option. It doesn't seem like there's a way to include a textbox inside of a checkboxlist, however. What's the best way to make this work?
-UPDATE-
If using a separate textbox control, how do I position it so it will align correctly with the checkbox?
Make the textbox a separate control on the page, then in your codebehind, check to see if other is checked. If it is, pull the value of the textbox, and use that.
To answer the question in your edit: You'll have to play with the CSS of the page to get it positioned correctly. How you do it depends on the layout of the page, among other things. I recommend posting some of the HTML from your page in another question and ask about how to position them.
What #Kyle Trauberman said...
Make the textbox a separate control on
the page, then in your codebehind,
check to see if other is checked. If
it is, pull the value of the textbox,
and use that.
Plus use javascript to hide or gray out the option unless the checkbox is selected.
string test="";
<asp:CheckBoxList ID="chk_list" runat="server">
<asp:ListItem Value="00">xxxx</asp:ListItem>
</asp:CheckBoxList>
<asp:TextBox ID="other" runat="server"></asp:TextBox>
inside the for loop
if (chk_list.Items[i].Value == "00")
{
test +=chk_list.Items[i].Text + other.Text;
}
The following asp label fails to be displayed in the browser, can someone please
tell me what I am doing wrong. I expect to see the value <abc> but instead
I get nothing.
<asp:Label ID="Label1" runat="server" Text="<abc>"></asp:Label>
By the way, I realize that I can accomplish the same thing doing the following:
<asp:label id="Message1" runat="server"> <abc> </asp:Label>
But that is not really what I am asking for, what I would like to know is if using a string such as "<abc>" in an attribute value for an asp elements is allowed or not. In other words, is this an ASP.Net bug or is this behavior by design and if it’s by design what’s the reason for such design?
Thank you very much.
Believe it or not, but you can include entities without escaping them, thus:
<asp:Label runat="server" ID="myLabel" Text="<abc>" />
This will render an <abc> tag.
Edit: OK, sorry, you want to display the brackets, not make a tag, of course..
Using entity references in the Text attribute will give the same result - an (invisible) <abc> tag - because they are translated when the tag is parsed server-side. What you must do is:
<asp:Label runat="server" ID="myLabel" Text="<abc>" />
This will give the desired result - the & entity reference will render an ampersand to the client. Followed by lt;, the result is a correct client-side entity reference (<). Which will render as <.
To answer you questions explicitly: Yes, using entity references in ASP.NET attributes is (obviously) OK, since it's an XML format. This is not really a 'decision' on Microsoft's part (and certainly not a bug) - i's simply XML.
The trick is realizing when the entity references are parsed (when the tag is parsed on the server), and what the resulting text is, which is what will be sent to the client.
Yes it's allowed of course. Label control's purpose is to show text and markup to client. And it's really useful I think. injected code is your responsibility.
The asp.net aspx parser will unescape the "<" and ">" to "<" and ">". It will generate something like this method:
[DebuggerNonUserCode]
private Label __BuildControlLabel1()
{
Label __ctrl = new Label();
base.Label1 = __ctrl;
__ctrl.ApplyStyleSheetSkin(this);
__ctrl.ID = "Label1";
__ctrl.Text = "<abc>";
return __ctrl;
}
If you wanted to write it in the text property you could double escape like "<", but it is probably easier just to write it between start and end tags like you mention.
<asp:Label ...><abc></asp:Label>.