Set CMS content in .NET - c#

What would be the best approach to set dynamic content from the database to controls in an aspx page?
My database consists of pages (index.aspx, home.aspx and so on) which consists of controls (DivStart, LabelDescription, and so forth).
The first technique that came to my mind was looping through all the controls in the page, looking for controls that have a certain class, e.g. "Cms_DivStart", and would then set the inner html for that control from the database control called "DivStart". The problem is only runat server controls shows up, and I don't want to make all controls server side.
I could store all the dynamic texts for a page in hidden variables and set it with jQuery when the page has loaded, but that would make the text not show up directly..
Any other ideas would be greatly appreciated.
Thanks
/Andreas

You could use asp:PlaceHolder tags, these don't add any html unless they are utilised.

You can write a custom class, I'll call it "CustomPage", that inherits the System.Web.UI.Page class. Then your pages can inherit "CustomPage". In this class, add methods to retrieve data, set custom properties and display your content in the available controls.

Related

Use one control multiple times in ASP.NET

What I have is few dropDownLists in few different templates of FormView.
And of course I can't get access to any of those.
What I want to do is get the same behavior for every DropdownList.
Is there any way to declare that DropDownList globally and reuse it or it should be done somehow with FindControl?
User control declarative syntax is very similar to syntax used to create an ASP.NET Web page.
The primary differences are that the user controls use an # Control directive in place of an # Page directive, and that the user controls do not include the html, body, and form elements around the content.
MSDN/creating usercontrol

Dynamically Add iFrame to a Page?

I need to generate a composite page made up of other pages in our system.
Is it possible for me to dynamically add iFrames to a page, each with its own src pointing to different URLs which are determined on the fly? If so, is there a preferred method to this?
Otherwise I need to refactor the other pages into user controls so I can add them as needed.
Although I'd seriously consider revising your pages to usercontrols if you'd like the "pages" to interact with each other. But for the question itself: you could add LiteralControls to your page (containing the iFrames) or to a placeholder.
PlaceHolder1.Controls.Add(new LiteralControl("<iframe src='mypage.aspx'></iframe>"));
You can do this the same way you dynamically add any .net control to a page, but you need to add an HtmlGenericControl
You could use the Document Object Model (DOM) / Javascript [1,2] to add iframes to your page. If you'd better like c# you'll have to postback before modifying the document I think (I'm not an expert in c# / asp).
[1] http://www.howtocreate.co.uk/tutorials/javascript/dombasics
[2] http://www.w3schools.com/jsref/default.asp

How to access Form controls on Postback from Dynamically built form

I'm adding controls at run-time to the Page Form object.
Each control has an Id.
On postback, I want to be able to access these controls and their values.
Currently, when I do a postback, the form has no controls in the Form.Controls Collection.
How can I rectify this?
Is this only possible if you add the controls to the page every time the page loads?
Dynamically added controls need to be added with every page load or else they will be lost.
However the viewstate of these controls can be maintained as long as they always get added with the same ID.
I believe you have to add the controls dynamically in order to access them on postback. So if you add a textbox dynamically, your event handler can't retrieve its value unless you add it again.
EDIT: One workaround I used was to add a predetermined set of server controls to the page and then use JavaScript to hide/show those elements. You avoid postbacks and you avoid the unnecessary tom-foolery associated with retrieving values from dynamically added server controls. Of course, this limits you to a predefined number of controls.
This was a big pet peeve of mine with ASP.NET web forms and is a factor in my decision to explore ASP.NET MVC - no more viewstate/postback mess.

How do I dynamically create new Hyperlinks in ASP.NET?

I'll explain what I'm trying to achieve:
I want to have a situation where I can create as many controls as I want by creating them in a loop in the code behind. I can do this while using PHP, by mixing the PHP code and the HTML code. This allows me to generate actual HTML tags dynamically.
In ASP.NET, I haven't found a way to replicate this functionality.
I've thought about using a loop in the code behind on something like the Init() function to create an array of new() objects, setting their attributes and hoping it is passed into the aspx file, but it didn't work.
How do I do this?
If you want to creat Dynamically ASP.Net Hyperlink control
You can simply do this:
HyperLink hyp = new HyperLink();
hyp.ID = "hypABD";
hyp.NavigateUrl = "";
Page.Controls.Add(hyp);
Well, keep in mind there's a difference between Hyperlinks and LinkButtons in ASP.Net
If you just want Hyperlinks, you can create as many as you want by creating a HyperLink object, creating new objects and then adding them to some control's Controls collection like panel or span or something.
LinkButtons are a little different. You have to use a Repeater control if you want to create a link that posts back to the server. (Well, you don't have to but the alternative is a hack job.)
Hyperlinks can be created by using a Repeater control as well and, if possible is the method I would recommend.
There are lots of ways to do this in asp.net:
If you really wanted to, you could put a loop using <% %> tags directly into the aspx markup, just like with php or classic asp. This is not recommended.
You can put a placeholder or panel control on your form, create as many hyperlink controls or anchor tags as you want in your code behind and add them to the placeholder's controls collection. This is a little better, but still not optimal.
You can put a reasonable number of hyperlink controls on the page, set their visible property to false by default, and "enable" the ones you need. This is preferred to #2 because of some oddities with asp.net page lifecycle.
These links come from somewhere, and that somewhere is usually a database or other reasonable datasource. So make it data driven — bind that datasource to a control like a repeater, gridview, details view, or similar. This is probably your best option.
If you want to generate controls dynamically & don't need to have those PostBack to the server (i.e. when a control is clicked/changed, it will come back to the same page) - you could use controls in System.Web.UI.HtmlControls namespace.
e.g.HtmlAnchor link = new HtmlAnchor();
link.href = "www.stackoverflow.com";
Page.Controls.Add(link);
Hope this gives you enough background.
EDIT: You can use the above code in a loop & replace the fixed value with what comes from a database/object.

UserControl - accessing textbox within UserControl within a web form

I am using c#.net
I have different views within my webform, these all generally display different information except for three textboxes (arrival / seen / depart time). To try and cut down on code, I have created a UserControl which contains these three textboxes.
I have referenced the UserControl at the top of my webform and also within each view.
<%#Register TagPrefix="uc1" TagName="userTimes" Src="~/usercontrols/userTimes.ascx"%>
<uc1:userTimes id="userAppointmentTimes" runat="server"></uc2:userTimes>
can’t seem to access the textboxes from the code behind. I need to firstly populate the textboxes and also hold any updated information to be re-inserted back into the database if changed.
Also each textbox has two Validation controls:
First makes sure it is in time
format HH:MM
Second makes sure the arrival is
before the seen time etc
My two questions are:
How do I access the UserControl from
the code behind? I have read that I
need to use the FindControl but I
don’t understand why, when I know
what it is called.
Do I undertake the validation
(server side) within the UserControl
code behind or the webform code
behind?
Thanks in advance for any help.
Clare
1.) You can access the User Control by its ID from the page's code behind - userAppointmentTimes in your case. To access your TextBoxes within the webform you need to use the FindControl-Method at the User Control level. So something like userAppointmentTimes.FindControl("WhateverTextBoxID") should work. You need to cast the result to TextBox of course.
You can't access the text boxes because ASP.Net does not automatically expose them for you. So alternatively you can provide public properties to set/get values to/from your textboxes inside your user control.
Within the user control, you can access your textboxes by their IDs.
2.) Put the validation controls inside your user control.
By webform you mean it's all inside the asp.net form-tag or do you have an asp.net form like FormView nested inside? If the latter is true you need to use FindControl at the FormView level - formView.FindControl("userAppointmentTimes"). Otherwise the user control is accessible from page level via its ID.

Categories

Resources