Getting value from a Masterpage in the controller Asp.Net MVC - c#

How do I get value of a textbox that's placed in the masterpage?

I agree with Pete.
Sometimes it is convenient to put general items into the master page. In contrast to classic asp.net it is harder to get the value with every post. In classic asp.net there is only one formular on the html page wich will be posted and all works well. In mvc there could be more formulars on the page. You have to put the textbox into a every form to get the value in every post - no solution.
A custom user control (ascx) might be a solution. Put the textbox in and place the custom control into the form where you need the value. If you have more forms or a complicated layout, than it is no solution.
A hidden field might be a solution. Replicate value changes of your original textbox into hidden fields placed in the formulars where you need the value.

I would find another solution. The purpose of master pages is to decouple the general layout from the specific pages (or controllers). You should be free to change the master pages without breaking the application.

Related

ASP.NET webform and Client element ID

When using ASP.NET Webform I get element id´s like these :
ctl00_ContentPlaceHolder1_ShowThreads1_repThreads_ctl03_divThreadTitle
I know about the ASP.NET 4.0 Client ID Feature but this will only change the name of the element and I do not even use this part?
What I need is to change the id of the elements on the client side to a shorter and better version.
Is this possible? And if so? how?
It looks like you have a user control dropped on a page, and that page inherits a master page.
If you place ClientIDMode="Static" on the user control as well as your other controls it will shorten the overall name. But why be concerned with the length?
In your code go after the control dynamically by looking at Control.ClientID(). This way you are not hard coding the control name.
This is one of the disadvantages of ASP.NET Webforms. It provides a very less control over HTML. In Web Forms many times we are not sure about what html we will get at the end (in your case the id of the control) making integration with JavaScript frameworks like jQuery a difficult task.
Checkout this Webforms Vs MVC to explore more.

How to set an event for all pages in ASP.NET dynamicaly

I'm trying to fire one event for all ASP.NET pages.
I don't want to duplicate this in all pages. So is there a way to do so for all pages ?
It might be available in web.config ?
My goal is to translate all pages before send to client.
So keep in mind I can't use events before Rendering because the controls texts are unknown.
Also we can't use events after Unload because we have no permission to write stream after this.
Any idea ?
Why do you want hardcoded translation of the controls to be fired after rendering them? I think that you should check using resources for localization in ASP.NET. And if you prefer allowing changes in translations, consider using solution suggested in this question.
EDIT:
If you really need to fire method for each page, try binding to one of events handled by MasterPage. Here is an ordered list of events fired by content pages and Master page.
If you won't be able to achieve what you want to do with Master page, you can try creating new page that inherits from System.Web.UI.Page, defind the behaviour in this page, and then make all other pages to inherit from this page and not from System.Web.UI.Page directly.

Dynamically creating asp.net with c# pages

I am struggling with finding clear answers to dynamically creating the same page over and over. The questions and samples I have found seem to be all over the board on this topic. I have studied the life cycle and still seem to not have a clear answer as to where code should go.
I have a master page and a content page. All the content in the content area needs to be dynamically created (text boxes, ddl's, page tabs, buttons/onclick etc.).
After a user fills in data and clicks a submit button, I need to read the values off the form and rebuild the page completely again (not add/remove controls to current content).
My question is then.
Where do I put my code to build the page?
Will this area allow me to use IsPostBack so I can rebuild content with Request.Form values?
Will my buttons _Click events work?
Are there any working samples out there you could direct me to?
Thank you very much for the feedback...
I don't know all the answers to your questions, but I hope this may get you started. When dynamically generating the UI through code, this happens in Init. Controls dynamically loaded on Init is key because between init and load, on postback, viewstate is loaded for these controls.
This means you need, on every postback, recreate the page as is to match the previous control tree, then deconstruct it after init and recreate the new UI, if something is supposed to change UI wise. This is because it validates the tree structure to determine its the same UI. Now, if you don't need viewstate, this may not be as much of an issue. I haven't verified this without viewstate to see if it behaves different.
It depends how dynamic you need it, whether you need viewstate (is a big factor).
HTH.
Try creating the controls in the page's PreInit method. "IsPostBack" should work and the click event handlers should work as well.
What you need is a web user control, see ASP.NET User Controls
Brian's advices are good and you should follow them.
This might not really answer your question but still I add it as an advice. I'm professionally creating ASP.net web applications at quite a large scale and from my experience I can say that too much "dynamics" is usually bad and should be avoided because it just introduces complexity. Normally you might want to expose UI parts into ASP.net UserControls or if you want to make them even more reusable (if that's a factor) then into ASP.net Server controls. Then you replace different of them dynamically rather than creating everything from scratch.

Form POST in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5. The page has several forms on it for several different things.
In ASP.NET, there's a server form control wrapping the entire page, and form controls don't work within a server form control, and you can't have more than one server form control on a page.
So in order to keep this functionality, I can either:
Remove the server form control that's wrapping the page, and leaving the html forms on the page.
Create button click events for every form and perform the POST in the code-behind.
What's the preferred method here?
I wonder if converting to vanilla asp.net (aka webforms) is a bad idea here. Personally I'd go to MVC instead - allows multiple forms etc, and the views are much closer to he HTML, a lot like ASP.
I guess I'm saying there are some glitches vanilla asp.net introduces that you don't have to suffer.
I would go with the second option, any button click is going to post the whole page back anyway so you're not saving any bandwidth. Simply handle each button appropriately.
Check the answer I provided to a similar question here :-)
How to get past embedding a html form for paypal buttons asp.net
If you're going to use different button clicks, you still need to use this override to disable the non-related buttons in each handler, otherwise it won't work. You can only have one form tag at a time - this way you can toggle/disable the ones you're not using as appropriate.
Better still, refactor your application to use a single form. While MVC would be a closer match to the model you're using right now, it wouldn't make sense to go that route unless you were experienced enough with it; Web Forms is an easier jump.

ASP.NET Wizard Control inline editing

I have being developing a form with the wizard control. The final step is a summary of the content that has been entered by the user as a confirmation step before submit the form.
These summary sections also catgorised into visual sections that corresponds with the each WizardSteps.
Each summary section is provided with an edit button that should allow user to edit the content then and there and update the content.
However the form should work without javascript so no use of AJAX. I'm not sure how this can be achieved? Is there a way to assign corresponding WizardStep to a placeholder onClick of the "EDIT" button for that summary section or is there any other way to do this?
Personally i would not bother trying to create a website in asp.net were one specification was that it should have to work without javascript. It is possible but your options are very limited. Here is a list of controls that don't work without javascript. I'm afraid wizard is one of them. If you must not use javascript you are going to have to create your own control that mimics what the wizard does but without JS.

Categories