ASP.Net Page abstraction - asp.net

We have a win application that shows a web form in a web browser.
In order to get data from this web form we are using a hidden text box and get its text using HtmlDocument object of web browser control.
I want to make an abstraction of this web form that has this text box element so that other forms can use this abstraction.
I made a web control and put the text box on it.I thought that if I put this control on my page it would have the text box.When i ran my application I noticed that the text box had been rendered but had its control name in its name (WebControl$TextBoxName) and its id(WebControl_TextBoxName) and the win app throw an exception since it couldn't find the element by its id(TextBoxName).
So here's my question:
How can I make an abstract web form/web control that has some elements on it and I can use it to make my final forms have these elements on them? (their names and ids should not be changed)
Thank you for your help

dotNet 4.0 supports static id's so they don't get mangled, read up on Client Id Mode
Alternatively, you could override the render of your control to output a standard html hidden form field with whatever ID you want, and then also add a custom property that will return the textbox that will hide the fact that it isn't an asp.net server control.

Though I've never used the browser control in WinForms, I think what you want to use is a Master Page. Assuming what you're rendering in the browser control is an ASPX page, create a Master Page with the hidden text box that you want to grab your data from, and tell all of the pages you want to have that common control on to use your Master Page. When the page renders, the control id will then be "ctl00_TextBoxName". There is no way of getting around the ID concatenation, since unique IDs are needed and that's the only way to guarantee uniqueness with all the nested control abilities of ASP.NET. However, doing this will guarantee you always have that control named the same on every new form you create that inherits the Master Page. Hope that helps!
In summary (because who reads paragraphs?):
Create Master Page
Place your common control in the Master Page
Have your Form inherit the Master Page
You can read up on how Master Pages work in MSDN's Documentation.

Related

ASP NET WebForm application Show <ElementName> is not valid

I'm working on a .NET WebForm app which has a master page. The .NET can not recognize many elements in the aspx file. For example it say Label is not a valid asp element and then says most likely reason is a malformed web.config file.
I double check my web.config and all looks good. The other webpages do not have same issue. The interesting thing is I do not have this issue inside the MasterPage.
I also tried recreate the page from scratch but still having same issue.
I'm using VS2019. Framework 4.6
You have to provide some sample code and markup. Also, if you have a master page, then that is a huge deal also. As a general rule, asp.net controls you drop on the page are able to be used from code behind. However, the code for the standard page(s) that we use as a "child" of a master pages means that code behind for the master page can easy use controls in master page. And code behind for the page being displayed in that master page ALSO can freely use its own controls. But controls between the master page and the working child page is VERY different matter.
And of course controls dropped into a repeater, or say listview (or even gridview) means that the one label or text box control is automatic repeated over and over. As such, you have to pull/get/use the one row out of that data bound repeating control, and then from that one repeating row grab the control in question.
So, saying I can't start my car, or I can't use or get a control?
We need more information as to the context of what control, where it is (in the master or child), and is the control perhaps nested inside of a data bound repeater, listview, gridview etc.
so, edit your question - add some details as to the markup, where it is (master or the child page), and we can help.
So, as a general rule, code behind in master page is free to use controls in the master page.
And in the web page you created, once again code behind is free to use controls in that page.
It can be more difficult to say have code in master page, and have it reference controls in the child page that is being displayed. But, then again, it is VERY rare that code in master page would need to reference or play with controls in the child page, since a master page will (usually) just be your main navigation bar - and it will be the same for many if not all pages you display - hence you master page really can't know what controls will exist in the current child page being displayed.

Can we use multiple forms in a web page?

So far, all the web pages I met contain at most one <form> tag. Why not multiple ones? I can not think of reasons why multiple forms can't coexist within the same web page.
Also, to be specific to ASP.NET - why are all the server controls are placed within the <form> tag? Why not place them somewhere else?
Plus,
I noticed that in an .aspx file, the <form> tag has the runat=server attribute, while a normal server control such as Button also has one. So it seems the <form> is also a server control. But strangely enough, I cannot find it in the Visual Studio Toolbox.
There can be multiple forms, with hacks.
It is indeed a shortcoming of WebForms. In ASP.NET MVC you can implement as many forms as you want (and it is valid & correct behavior of web pages).
The reason all server controls are placed inside <form> tag is to allow the WebForms engine to recognize them, load their values & save their values from/to the ViewState. Almost all infrastructure of control management in WebForms is based on the idea that a tag contains everything you access from the code-behind.
As pointed out, this is one of the shortcomings of WebForms. I do want to point out, additionally, that with cross-page posting and validation groups, you can typically reach your desired behavior (for most "multi-form" solutions).
Regarding the additional question: the <form runat="server"> is parsed as HtmlForm class behind the scenes, which inherits from HtmlControl like any other HTML element with runat="server".
Unlike any other HtmlControl though, there can exist only one instance per page and it does not appear in the toolbox as it's added automatically to every new Form you create, so it's quite pointless.
Yes, it can be done - by creating a custom HtmlForm object and toggling the forms as needed. I've just answered a similar question here (with code):
Paypal Form Ruins My ASP.NET webforms layout -> How to Solve?
many non server forms - you can , but only one runAt Server form
i also found this :
A server-side form tag is the tag which has a runat="server" attribute. If this attribute
is missing, then it's a typical HTML form tag. The conclusion is that you are allowed to use
multiple form tags on a page, as long as only one has the runat="server" attribute. The
disadvantage of the form that doesn't have this attribute, is that view state won't work
(meaning form values will disappear when using the back/forward browser buttons). It's a
small price to pay if you really need multiple forms on a page.
Take master page & set design.
Take one form in master page.
Second form take in contain place holder.
In contain place holder in only for write form tag (not use)
Add aspx page & design second form but not write form tag only for control put
Take button click event fire code write
This is proper way of two form

How To Create Nested Form in Asp.net

I have a simple website and I use masterPage for designing my template.
everythings work fine, but when I add a Custom (google) Search Box in it my pages correpted.
infact asp does not support Nested Form and as you all know google use a simple form to get queries from the users.
so at first I redesign my site and put 2 Form in it. One server form for my pages content and one other form for google search box. untill here everything work fine .
so I force to add 2 new button beside of my search box and these buttons need a runat=server form, so now I need an approach that let me enable a third form (second runat=server form ) or find an approach to use simple form inside of runat=server form, actually
howcan I put 2 form inside each other or how could we enable a nested form ?
Nested forms won't be possible. You'll need to make those buttons work without being in a runat="server" form.
Cause Asp Forms are not a display control and just accessible in code for programmers,
so I use it in a unregular manner,
as you all know every XML markup like XHTML (asp) have some element (in asp case : control) and each element have its own attribute (in asp case controls properties)
so I just need to put my controls inside the Form (root) Element and cause when page loading on client machine, whenever browser see the server form just change a flag to true (the server form is available) then you can use what ever you have inside of the form,
so if you can lagically put your controls inside of the form , so putit, nothing bad happen )

ASP.Net Custom controls vs. user controls: Are these two the same

If they are different, under what circumstances should we decide to use either approach?
Also, what is the advantage of ascx over aspx?
user controls are a form of custom control, that gives you a visual designer. They are ideal for use when you want a reusable control within the same web site. (It is possible to create and package user controls as seperate assemblies but that is beyond the scope of this question and I know has been asked on SO).
A custom control is typically used to refer to a Web Control, or a Composite Control which is specialized form of a web control. These controls have no designer and are usually implemented in seperate projects from your web allowing them to be reused accross many sites.
Now your second question, ASCX and ASPX are two different things. ASCX is the extension for a User Control, where as ASPX is an ASP.Net Page. You cannot use an ASCX by itself it must be placed onto an ASPX or Master page.
One way I like to use user controls is I have for example a very complex page which have 7 tabs, 5 of those have grids, of those grids three of them are identicle. Well what I can do is create a seperate user control for the content of the tabs, this now reduces the code I need down significantly (Since three grids are identicle except for the data).
Further more it allows multiple to work on various parts of the page, and it helps me keep everything straight since I am reducing the complexity of the page. You do not use User Controls instead of Pages, you use them in conjuction with a page.
Edit
You do not use ascx over aspx. You use ascx to complement. For example on my site every page has the same footer, but I don't want every page to derive from a single master page. I can create my footer as an acsx control and put it in each of my master pages.
Another example, I have a form that lets a user enter three different date ranges. (And we have other forms). So I put the logic to enable a calender button, and a text box that when clicked on opens up the calender, in a user control. I can then reuse that user control in all my aspx pages.
Custom controls are control build entirely in code. The pro is that you can put them in libreries, add an icon to the toolbox and other fine control.
User controls are more easy to do, and in general is a way to encapsulate things to simplify other pages or when you need to use the same markup in several pages.
The advantage of controls over regular aspx pages is that you only need to do some part of the markup (not the whole page)
User Controls/Composite controls
User controls that are made up of other ASP.Net or custom controls. They are usually quite straight forward and can be created quite quickly. They Are generally not shared across projects however there are some tricks that can allow you to do this.
Custom controls
Custom controls are controls that you implement the UI by creating everything from the HTML output to design time support. Custom controls take much longer to make. You must use either Web.UI.Control or inherit from a sub control (Textbox for example). Custom controls are compiled to binary format to allow them to be distributed more easily. Since they are compiled they can be referenced from the toolbox in visual studio.
There are 2 main advantages to using a control in an aspx page. Encapsulation of logic and reuseability.

best way to generate and highlight menu items on server (asp.net)

I have an ascx component, that holds two-level menu, because there are several user types, and the menu needs to be computed on server.
Anyways - I'm also doing the highlighting of current menu item on server (adding a selected class/css to an item). Highlighting with javascript is not good option, because there is content in datagrids, that causes postback and needs to remain the menu in the same position as selected value is.
So I am doing this as some kind of mapping with a hashtable (e.g. pairs (url-of-the-site, menu-item-to-highlight)), and i have to include a mapping for every site my web application contains in order i want to have menus highlighted... so somehow it bothers me that there has to be a better way to do this. are there any better techniques?
A technique I use on my master page/content pages with .NET 2.0 is to have a publicly accessible method called "SetNavigation" that takes a string character that correlates to the hyperlink control I want to highlight. What the method does is set the CSS stylesheet of the specified hyperlink to my "selected" one.
Then in my child pages, in the Page_Load method, I call the SetNavigation method and pass in the link based on the current page I'm on, such as SetNavigation("hypSearch")
I would gather that you could create a similar control on your ASCX control and then have your Page_Load events, or even button click events call the method if so desired.

Resources