I have a front end written in html that I am converting to asp, and many of the controls have names with "-" in them. This is causing crazy headaches, as there is no time to rename everything, and the ctrl-f and replace somehow breaks my css. Is there any way to access these controls in the code behind while they have the dashes? I have tried the code below.
//Can find when there is no dash in it, but that breaks the css after find/replace of full solution
HtmlGenericControl body = (HtmlGenericControl)this.Page.FindControl("page-list");
body.Attributes.Add("class", GlobalVariables.webAppSkin);
//I have also tried this, but logout stays null
WebControl logout = (WebControl)Page.FindControl("logout-link");
This is the html control:
<body id="page-list">
Sorry, that's not gonna happen.
You cannot have an element with an id containing "-", and still be a runat="server" ASP.NET control.
Microsoft's docs about the control's IDs states:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.id.aspx
Only combinations of alphanumeric characters and the underscore character ( _ ) are valid values for this property. Including spaces or other invalid characters will cause an ASP.NET page parser error.
If you tried adding runat="server" to the body tag you showed: <body id="page-list">, it would give you the following line in aspx.designer.cs:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl page-list;
Which is obviously throwing an exception on C# syntax.
<body id="page-list"> is not a HTML Control (i.e. an instance of (a subclass of) System.Web.UI.Control because it doesn't have the runat="server" attribute. If you were to add runat="server" then you would get a JIT compile-time error message informing you that "page-list" is not a valid identifier.
ASP.NET Web Forms 4.0 added the ClientIDMode attribute, however it doesn't allow you to set a completely custom ID attribute value. There's no easy solution for this without using <asp:Literal> or implementing your own control (or my preferred option: switching to ASP.NET MVC).
You can access controls in code behind with their ID if you write runat="server". Here is the example for your case
<body runat="server" id="testID">
In code behind you can access it like this:
body.Attributes.Add("class", value);
Related
We are using a custom form control in our application. When we try to use asp dropdownlist control inside this custom form control, I get this given exception. But, when I use asp literal control we don't get this error.
Please suggest if you can provide me any directions in which to look to fix this issue.
That's because when you are trying to add a DropDownList, it must be adding it outside the from tag, but the asp dropdownlist requires a form tag with runat="server", since it is a server side control.
On the other hand your Literal control is working because - A Literal Control represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server.
To prove this, just try adding this in a webform:-
<body>
<asp:Literal ID="Literal1" runat="server" Text="Hello"></asp:Literal> //Correct
<asp:DropDownList ID="ddlTest" runat="server"></asp:DropDownList> // Error
<form id="form1" runat="server">
</form>
</body>
Literal control will not throw any error while Dropdownlist control will throw the error you have mentioned because it is outside the form tag.
If you are experiencing this because you are trying to write a user control which is executed on the server and passed via a service / ajax, then you should be using System.Web.UI.HtmlControls instead of System.Web.UI.WebControls.
For example, in this case you can use HtmlSelect instead of the DropDownList.
HtmlControls do not require a form tag with the runat=server parameter; It does however mean that any events related to these controls have to be handled by the front end (ie. Javascript).
I'm trying to render an element's mark-up using asp controls while avoiding using code-behind. So I want to dynamically generate the href property to include what is rendered from a FieldValue control (SharePointWebControls).
So for example this control I have:
<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
Appears as:
"TestPage"
And I have a link on that same page looking like this:
CLICK HERE!
But above in the <a> element - I need TestPage to be there as a result of what's rendered by my FieldValue control; so I basically need a way of 'embedding' the output of this control within the <a> element's href property.
There's no messy bits of markup to accompany the rendered version of FieldValue - it's literally just text - so I'm assuming this isn't complicated.
Not familiar with SP controls, but I guess the compiler stops on the double quotes when you try to embed you control in the href.
Maybe you can try replacing you href's double quotes with single quotes like this :
<a href='http://www.mysite.com/mypage.aspx?title=<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>'>CLICK HERE!</a>
( be aware that it will fail if there are quotes in your text )
another solution I see is using some js/jquery (almost the same solution, in fact) :
$('selectorForYourA').attr("href", 'http://www.mysite.com/mypage.aspx?title=<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>');
There seems to be a problem with the ASP textbox control when set to multiline and serving the page as xhtml. The project I am working on uses content negotiation to serve asp pages as application/xhtml+xml to browsers which support it. The problem is when asp textbox renders a textarea to the page, it explicitly prepends a newline to the text. Reflection of the textbox's render method looks like the following:
if (TextMode == TextBoxMode.MultiLine)
HttpUtility.HtmlEncode(Environment.NewLine + this.Text, (TextWriter) writer);
When firefox and opera are served this with xhtml content type, they interpret the newline as part of the text in the textarea and so I get extra newlines at the beginning of my text areas.
I could subclass textbox and override render, but that seems like a bit of overkill to correct something like this. Is there another way to correct this? And does anyone know why asp textbox does this anyway?
An alternative to subclassing is to use control adapters, or to write the <textarea by hand and get ASP.NET to generate the control name attribute for you.
I suspect ASP.NET WebForms does this simply because of a short-sight. The future is MVC anyway, so don't expect this to be changed any time soon. I suspect the original purpose of the newline is to give the textarea a "value" rather than nothing (thus making the textarea "successful" in HTML forms parlance).
This isn't the only odd behaviour you'll see in ASP.NET. The atrocious HTML formatting of <head runat="server"> is also on the list.
I have some text being fetched from the DB, which I am binding to the DataList ItemTemplate in the following form:
<asp:LinkButton runat="server" Text='<%#Eval("url")%>' />
The text that is fetched from the DB might be long and I want to restrict it to (let's say 50 chars at max. with a ... afterwards) in the above eval assignment.
How can this be done here?
Secondly, how do I specify the link here in LinkButton so that on clicking on it, it goes to the specified, the link should open in a new window as in taget=_blank
You can use a tag directly
<a href='<%#Eval("url")%>' taget=_blank> <%# BindText(Eval("url"))%></a>
Codebehind:
public string BindText(obj url)
{
if(url!=null) {return (url.ToString().length > 50) ? url.ToString().Substring(0,50) + '...': url.ToString() ;}
return "";
}
One easy way to handle that would be to create a "Truncate" extension of type String which simply strips X characters from the end of it.
Regarding "target=_blank" - you should be able to accomplish this with the Attributes property of the LinkButton.
Depending on the target browser, using CSS text-overflow is an elegant way to do this at the client instead of the server (maximizes space; only that text which must be truncated will be truncated, and it also takes into account simple punctuation rules).
https://developer.mozilla.org/en/CSS/text-overflow
This blog post shows a decent solution in that it seeks whitespace in which to inject the ellipses (rather than blind truncation).
For setting the target of a LinkButton...
<asp:LinkButton runat="server" target="_blank">
ASP.Net will (usually) ignore attributes that it doesn't recognize and just render them to the client verbatim. However, this won't actually work because a LinkButton is meant to initiate a postback. You can use an anchor tag instead.
I have an issue with the following text display:
<asp:RegularExpressionValidator ID="Password_RegularExpValidate" runat="server"
Text="TEST!"
Display="Dynamic"
BorderStyle="None"
ControlToValidate="txtNewPass"
ValidationExpression="(?=^.{8,255}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*"
meta:resourcekey="Password_RegularExpValidateResource1" /></td>
The pattern by itself is:
(?=^.{8,255}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*
The text initially had some stuff in it as the ValidationExpression was different. I've changed the regex expression and that works, but when I write something in Text= it doesn't update on the page. I've restarted IIS, cleard the IE chache... everything I could think of. The old text keeps appearings (ie. "TEST!" doesn't show up when the validation fails as it should).
Any help would be appreciated.
Edit:
The code for txtnewpass:
<asp:TextBox ID="txtNewPass" runat="server"
TextMode="Password"
MaxLength="256"
meta:resourcekey="txtNewPassResource1"></asp:TextBox>
Also, it's worth noting that I can remove an entire table from a page that it disappears when I reload the page. But when I change text values from controls or anything that runat="server" and the page doesn't seem to update with the text....
And the code behind doesn't edit the field that displays, the validator validates the text in the textbox and uses that value later.
Edit 2: Same thing happening with -
<asp:Label ID="Label1" runat="server"
Text="Change Password!!!!!"
meta:resourcekey="Label1Resource1"></asp:Label></td>
I've added the exclamation marks (!!!!!) and that's not showing up when I refresh the page either.....
Edit 3: As I've noted in one of the comments, if I delete a table from the page and reload the page, that table disappears, so I know the page is reloading properly. The runat="Server" property, does that work a certain way where it caches text or something? I'm out of ideas....
Like Kirill said, use ErrorMessage instead of Text.
But the main problem is, I think, your localization, which is handled through the meta:resourcekey tag and resources.
Here is a good explanation:
ASP.NET meta:resourcekey
If you set automatically or manually the resource localization file and change something afterwards, for example a Label Text property, then you need to do it in the resource file too. Because there should be your initial value, which is loaded at runtime.
There are a lot of possibilities for the source of the problem, and there's not really enough info to tell which one it is. It sounds to me like one of these:
1) An external issue with your application or page (perhaps your ViewState isn't being set up properly, or the validation is getting called before PostBack).
2) You should be using RegularExpressionValidator.ErrorMessage instead of Text, as Kirill suggested. You said that you had changed that, but I wonder if you've reloaded the page (you could try rebuilding the app or something if it's getting cached somehow).
3) Your regex might not be doing what you think it is. The pattern is extremely long, and it seems strangely written. Adding some whitespace, we find that it looks like this:
(?=^.{8,255}$)
(
(?=.*\d)(?=.*[A-Z])(?=.*[a-z])
|
(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])
|
(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])
|
(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9])
)^.*
Which simplifies down to something like:
^(
(?=.*[A-Z])(?=.*[\W_])(?=.*[a-z\d])
|
(?=.*[a-z])(?=.*\d)(?=.*([A-Z\W_]))
).{8,255}$
Is this what you intended? I see where you're going with the password rules, but it may be easier to just simplify them and require one capital, one lowercase, one number, and one special character. You could also try to get validation working with a simpler regex pattern, and then add complexity once everything else is working.
Try to use RegularExpressionValidator.ErrorMessage instead of Text.
Try restarting VS. Sometimes incredible bugs disappear after that.
Try removing and regenerating your ..aspx.designer.cs file (to
regenerate right click the aspx/ascx file and choose "convert to web
application").
Agree with others, you should indeed check your resource file.