asp.net dynamically add usercontrols and position them - c#

My issue is that I have a designer that will create a custom aspx page bu without any .net controls. I need a way of adding the controls dynamically. So far the only types of controls will be textboxes and a button, but there are 30 variations of what the textboxes can be (name, phone #, email, etc). Also the textboxes may or may not need to be required. Once the textboxes are added the form will be submitted to a db.
My first thought was to have the designer place something like [name] and then replace that with a user control that has a name textbox and a required field validator. In order to determine if the validator should be enabled I was thinking that the place holder could look like this, [name;val] or [name;noval]. I could either do replace the place holders in code dynamically or set up a tool that the user pastes their html into a textbox and clicks a button which then spits out the necessary code to create the aspx page.
I'm sure there must be a better way to do this but its a fairly unique problem so I haven't been able to find any alternatives. Does anyone have any ideas?
Thanks,
Kirk

IF your designer gives you html pages, just create a new website. copy and pages all the HTML pages with the Image folders and everything to your project. then for every HTML page create an aspx page, (with the same name) copy and pages the html's tags which are between to the aspx page's and for the body copy and paste HTML page's tags which are between into the of the aspx page.
Now you have your aspx page, exactly the same as html page.

Sounds like an attempt to over-engineer a solution to what should be a non-issue.
As #Alessandro mentioned in a comment above, why can't the designer provide you with pages that have the control markup? As it stands right now, the designer isn't providing you with "a custom aspx" so much as "a custom html page." If the designer is promising ASPX but delivering only HTML, that's a misinterpretation somewhere in the business requirements.
However, even if the designer is rightfully providing only HTML, there shouldn't be a problem with that. At worst, you can set each element you need on the server to runat="server" to access them on the server-side. Or, probably better, would be to simply replace them with the ASPX control markup for the relevant controls.

Write a simple parser that will recognize the [...] tags and replace them with corresponding controls. Its pretty easy to do and i've often done this... the tag i use is usually $$(..); though, but that doesn't matter as long as your parser knows your tags.
Such a parser will consist of a simple state-machine that can be in two states; text-mode or tag-mode. Loop through the whole page-text, char for char. As long as you're in text-mode you keep appending each char into a temporary buffer. As soon as you get into tag-mode you create a LiteralControl with the content of the temporary buffer and add it to the bottom of your Control-tree, and emtpy the buffer.
Now, you still keep adding each char into the buffer, but when you hit text-mode again, you analyze the content of the buffer and create the correct control - could be a simple switch case statement. Add the control to the bottom of your control tree and keep looping through the rest of the chars unto you read the end and keep switching back and forth between text-mode and tag-mode adding LiteralControls and concrete controls.
Simple example of such a parser... written in notepad in 4 minutes, but you should get the idea.
foreach (var c in text)
{
buffer.Append(c);
if (c== '[' && mode == Text)
{
mode = Tag;
Controls.Add(new LiteralControl(buffer));
buffer.Clear();
}
if (c == ']' && mode == Tag)
{
mode = Text;
switch (buffer)
{
case "[name]": Controls.Add(new NameControl());
... the rest of possible tags
}
buffer.Clear();
}

Related

How to shrink the ValidationMessage string in Blazor

I am new to blazor and currently working on a small project where I have a lot of text input fields on a page, which would be validated on submit. The page is responsive and would shrink into a column format when reducing device screen size. The message for a couple of validations is too long, and the string overflows out of its parent div element. What is a good way to fix that, and make the string break into multi-line?
Code
Code
The default ValidationMessage component generates a div and assigns the css class 'validation-message' at rendering time. If you want to change the look and feel, you can do one of these :
create a custom ValidationMessage c# class, and use your own css class (refer this SO answer) or use bootstrap css etc
quick and not so elegant fix, you can modify the validation-message class in your app.css or site.css file(s), so that it word wraps the lengthy error messages
create a custom validator blazor component (as in this)

Can I modify the <head/> section of a Page in .NET from a User Control without runat="server"?

I have a page that does not have runat="server" set in the <head/> section. I do not have access to modify any of the code in the page.
This page contains a user control which I do have access to. Can I add a <meta/> tag to the head section of the page from the user control? It needs to be server-side so a javascript solution won't work.
One option is to create a Response Filter, and then modify the output before it's sent to the user.
https://web.archive.org/web/20211029043851/https://www.4guysfromrolla.com/articles/120308-1.aspx
You can parse the text in
(this.Page.Controls[0] as LiteralControl).Text
to see where the string <head> starts, and insert whatever text you need in there thus injecting your own code into the page header without it being marked with runat="server".
Please be aware though, this is pretty hacky way of getting your code where it most likely shouldn't be (otherwise the <head> element would have been marked as runat="server" so you can access it normally). This will also break if at a later date the head element is changed to be an ASP.NET control. It might will not work with master pages, you will have to walk up the control tree looking for topmost literal element.

html page annotator

I have to write a web page annotator.
And my requirements are the following:
1) given a set of pages, I want to annotate them efficiently (in a browser, in an external application that knows how to render HTML, etc.)
2) I select (highlight, make active) manually a string of text, and dropdown menu appears that allows to select from a set of options
3) after that the iterator appears (like in a browser when pressed ctrl+F to search) and I want to be able to navigate through matches of the string, selected in the previous step, on the same page
4) comparison function on strings is given that has interface: given two strings it outputs either 1 or 0, depending on strings match
5) when I press iterator button, i move to the next match for selected string, and then a message box should appear (or any other thing where i can confirm that it is a true match)
6) having confirmed that it is a true match, the text of a page should be modified such that current match became surrounded by special tag
(for instance <<< optionX >>> matched text <<< /optionX >>> ), where optionX is defined based on a value selected in the first step (dropdown menu)
5) when all matches (defined by comparison function) are found on a page, I would like to mark another string of text on the same page and then repeat the process, by finding all the matches, confirming some of them, and modifying page source correspondingly
6) then a modified page should be stored on a local drive
QUESTIONS:
Can you please suggest what is the right tool to do that?
1)Is it OK to use javascript and work in a browser. If yes, what methods are required for that and are there any useful libraries that do just that, or at least cover some functionality described above
2) May be is it better to build a custom desktop app, that renders a page in a special frame, and have appropriate buttons to navigate, confirm etc. (python or C# are considered), and again what classes and libraries can help
[UPDATE]:
I know how to work with the content of a page, but I am curious how to make it comfortable for annotators to use, how to build the right dialog with the user: ways to have all candies such as dropdown menus and iterator that is visible for users, dialog for confirmation etc.
The goal is to annotate a lot of pages with that, therefore interface should be efficient. I am a researcher (and this is not a homework as you might think, i just described what is needed in a formal way ) and I have only poor experience writing user oriented apps.
Thank you in advance!
This is definitely a web browsers job. You can use JQuery to search and modify a html page. This example will find all occurrences of homework and change the text to << homework >>
$("*").each(function () {
if ($(this).children().length == 0) {
$(this).text($(this).text().replace('homework','<< homework >>'));
}
});

ASP.Net: User control with content area, it's clearly possible but I need some details

I have seen two suggestions for my original question about whether it is possible to define a content area inside a user control and there are some helpful suggestions i.e.
Passing in content to ASP.NET user control
and
ASP.NET User Control inner content
Now, I like the theory of the latter better than the former just for aesthetic reasons. It seems to make more sense to me but the example given uses two variables content and templateContent that the answerer has not defined in their example code. Without these details I have found that the example does not work. I guess they are properties of the control? Or some such?
EDIT - DETAILS: What I am trying to do
I have need of an ASP.Net user control that conceals some content in a panel inside a placeholder and asks for the input of a code in a visible panel.
Essentially the user will put their code into the provided textbox in Panel A and submit it, it will be checked and, if it is valid, panel B and the locked content will be displayed.
I have done a test where the content was hard coded into panel B but as soon as I need to make the content a generic input it fails. If it were just text or somesuch then I could make it a property of the control, but as it is, in fact, another User Control I am having some difficulty getting this into the "hidden" panel.
Any other workable solutions are also welcome.
EDIT NOTE: The solution I'm trying to implement this in 2.0 I did find a 3.5 solution which I cannot use.
The former example seems workable but I'd prefer to go with the latter if someone could fill in the blanks for me.
Thanks.
Okay, so this is disturbingly easy but many of the tutorials on the web that talk about this kind of thing push to do extravagant things that require the control to parse ListItems or such.
So this solution is purely so that you can build a control that, for whatever reason, has a placeholder in it that could have anything inside it (kind of like a content area on a Master page). In this instance it happens to be because the Panel containing the placeholder is hidden until appropriate input actions have taken place in another panel.
First, you need to add this:
[ParseChildren(true,"Content")]
[PersistChildren(false)]
just above the part of the control which looks like this:
public partial class MyControl : System.Web.UI.UserControl
then in the control scoped declarations at the head of the control you want to declare thus:
private Control _content;
[PersistenceMode(PersistenceMode.InnerProperty)]
public Control Content { get { return _content; } set { _content = value; } }
Finally you need to place the content into the placeholder like this:
phContent.Controls.Add((Control)_content);
That last line goes into the Page_Init event. For reference "phContent" is the name of the place holder where you want the content to appear, like this:
<asp:Panel ID="pnlLockable" runat="server" Visible="False">
<asp:Placeholder runat="server" ID="phContent" />
</asp:Panel>
On the front end the resulting implementation looks like this:
<uc:MyControl runat="server" ID="lockit1">
<Content>
//...your stuff goes here...
</Content>
<uc:MyControl>
Note that I presume that what is inbetween the Content Tags is a root control. This is because I nested another user control in there. I imagine if you put whatever content you want within a panel or placeholder it should be fine.
Also you can read "How to: Create Templated ASP.NET User Controls". Really helpful.

XML Tags in asp:TextBox prevents other controls working?

I have a series of controls on an ASP page. Some are inside an UpdatePanel and some are not.
If I put an XML tag in one of the text boxes (eg, "<foo>") then all the controls within the UpdatePanel don't work. As soon as the tags are removed, everything is fine.
My 'submit' button is in the UpdatePanel and the breakpoint on btnSubmit_Click is only hit when there aren't tags in the text boxes.
I'm a long-time C# dev but quite new to ASP.NET so might be missing something obvious... this just isn't the behaviour I expect.
If you were to take the UpdatePanel off the page, you'd find that the postback was causing an error because .NET thinks that "<foo>" is a potentially dangerous bit of data to accept at the server. See this question on StackOverflow. You don't see the error because the error page HTML is being returned to the UpdatePanel's ajax call rather than direct to you browser, and the UpdatePanel doesn't know what to do with it.
You can turn off the checking by adding
ValidateRequest="false"
to the <#Page ... > directive at the top of your aspx file. Or you can modify the web.config to get the same effect right across your web app.
You can't put markup in a textarea. You must HTML-escape any markup characters inside textarea just as you must with any other element.
<textarea><foo> & <bar></textarea>
Although in practice browsers will usually work out what you mean and show any < characters as-is, it's still invalid HTML and non-well-formed XML (presumably this is also the root of your issue in ASP.NET, though without specific code it's difficult to tell).

Categories

Resources