Dynamically add html to panel and access the htmlcontrols - c#

I would like to dynamically add a html to a server control and then I want to have access to each control from this html. If I use the inner html property of a control I can notice the html was added as a literalControl and I would like it to be a html control with some other html controls
ex:
//aspx file
<div id="content" runat="server"><div>
//aspx.cs file
protected void Page_Load(object sender, EventArgs e)
{
content.AddControlsFromHtml("<input type='text' id='textBox' />")
}
//get the control
((HtmlInputText)content.FindControl("textBox")).Value = "hello"
Is this possible?
I need this behaviour to create different layouts for a page

for creating dynamic html, put your code in page_init event..

Related

EnableViewState for html tag

I am quite new and I will like to ask a question. Please see the html and codebehind.
HTML
<ul id="menu" runat="server" EnableViewState="True"></ul>
CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var liItem = new HtmlGenericControl("li");
var aItem = new HtmlGenericControl("a");
liItem.Attributes.Add("class", "test");
aItem.Attributes.Add("href", "#");
aItem.InnerText = "please work";
liItem.Controls.Add(aItem);
menu.Controls.Add(liItem);
}
}
Once postback, the UL data is lost though I have enabled the viewstate. I remember it works last time but now it is not. Anybody can advise? Thanks a lot
This happens because you are dynamically adding data via your first load (!IsPostback), and thereafter (when Page_Load runs again) the data is being lost. You have to keep in mind that EnableViewState is a ASP.NET specific property, So it will only work on server controls inheriting from System.Web.UI.Control
The only way you can achieve this is by either creating your html tags on each and every page load (i.e remove your !IsPostBack check)
or adding a ASP.NET control to the page which supports ViewState (Gridview, ListView, Label, Button etc).

Change label in masterpage in just one content page

How do I define a label in a masterpage from inside one control page, withouth losing it when I navigate to another control page? I know that I can use this code and it works:
(Master.FindControl("myControl") as Label).Text = "someNewContent";
But I have to define this on every page to keep the same content in the label. Is there a easier/shorter way to define this piece of code only one time in the whole program? Thanks in advance.
I think I get the gist of what you're asking:
Firstly, I would strongly type the master page, in your content page just below the #Page directive, by using the #MasterType directive:
<%# MasterType TypeName="*fully qualified type of your master page*" %>
Next, place a public property on your master page, like so:
public string MyText
{
set { this.ViewState["TheText"]; }
}
In your content page (during the Page_Init, for instance) you can add:
this.Master.MyText = "Whatever you want to say!";
Then load your master pages control text property in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
this.myControl.Text = Convert.ToString(this.ViewState["TheText"]);
}
This won't persist from content page to content page because each content page instantiates a new instance of the master page. In that case, put whatever text you want to persist in Session, then read it in the mater pages Page_Load event.
Hope this answers your question.

Change form target in content area of a master page

We have an asp.net (2.5) application where we use one master page and a few hundred forms that utilize that master. We're creating a new set of forms and we want to submit the form on a button so that we can use all of the form values on the next page.
However, the form tag is in our master page and asp.net seems to only allow 1 form tag per page. Is there a way for me to change the target of the form post of the master from the child page or is there a way to add a second post?
You can change the target. In Masterpage add an id if it's not there already:
<form runat="server" id="form1" >
Now in the content page access the form and change it's target:
protected void Page_Load(object sender, EventArgs e)
{
HtmlForm myForm = Page.Master.FindControl("form1") as HtmlForm;
if (myForm != null)
{
myForm.Target = "_blank"; // _parent, _self or _top
}
}
I don't know how much it will help.
You might look here, I found it very interesting: Form Elements in ASP.NET Master Pages and Content Pages.
Do you need to access this second form from the code-behind on your new forms? If not there is a little hack you can do just add an end form tag and start a new tag that has the new target details
</form>
<form method="POST" action="blah.svc" >
//non-asp.net form
However to change the actual postback url you would change the form action via the PostBackUrl property on your buttons.
There is also Server.Transfer, kinda depends on what you are doing on your new forms.

Display web page according to html string

I have HTML script string stored in database. I am fetching this string in one variable.
I want to display page out of this html string below gethtml button in below .aspx page.
I searched how to display sub html page inside main aspx page.
I came to know with webbrowser control, but not finding it anywhere in tool box.
Is there any other control or way to show html page out of html script in above main aspx page?
Please help me.
<div id="htmlpage" runat="server"></div>
in code behind
protected void Page_Load(object sender, EventArgs e)
{
string ss="<h1>Hello world</h1>";//your html string
htmlpage.InnerHtml = ss;
}

Load css dynamically in asp.net?

I'm creating a content management system where a user can select a css file from their server and the application will parse and store the css. The application will need to be able to parse the css classes out, record them, and store the css content to be dynamically added to another page where a user can select the different css classes from a drop down. So does anyone know of a way to add css content to a page dynamically, for example from a database? I've found a few projects for parsing the css, here .
Thanks in advance.
Make a controller which serves the CSS content:
<link rel="stylesheet" href="#Url.Action("GetCss", "Serve", new {id="filename"})" />
Controller code:
public class ServeController: Controller
{
public ContentResult GetCss(string id)
{
string cssBody = GetCssBodyFromDatabase(id);
return Content(cssBody, "text/css");
}
}
A good approach for a WebForms-only project is to link to an .ashx handler in your page instead of a static CSS file:
<link rel="stylesheet" type="text/css" href="DynamicStyles.ashx" />
Then create the handler (add a 'Generic Handler' item from Visual Studio) and within it you can load the CSS from a database or wherever. Just make sure you set the content type correctly in the handler, so that browsers recognise the response as a valid stylesheet:
context.Response.ContentType = "text/css";
I think NullReference gave you a MVC solution because you tagged your post "mvc". If you're using ASP.NET Web forms, you can use the same technique used when generating CSS-links on-the-fly on user-controls. On the page's Page_Init event, do something like the following (in the example below, I'm linking to jquery-ui-CSS):
protected void Page_Init(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlLink jqueryUICSS;
jqueryUICSS = new System.Web.UI.HtmlControls.HtmlLink();
jqueryUICSS.Href = "styles/jquery-ui-1.8.13.custom.css");
jqueryUICSS.Attributes.Add("rel", "stylesheet");
jqueryUICSS.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(jqueryUICSS);
}
If you want actual elements to be rendered on the header, then use HtmlGeneric control instead of HtmlLink in my example above. It's still the same technique--on Page_Init, add to the Page.Header.Controls collection:
protected void Page_Init(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl mystyles;
mystyles = new System.Web.UI.HtmlControls.HtmlGenericControl();
mystyles.TagName = "style";
string sampleCSS = "body { color: Black; } h1 {font-weight: bold;}";
mystyles.InnerText = sampleCSS;
Page.Header.Controls.Add(mystyles);
}

Categories