Display web page according to html string - c#

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;
}

Related

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.

How to close a modal window after a save in Asp.NET WebForms

I have some javascript code on my main page that opens a popup when a link is clicked on:
<script language="javascript">
function OpenResidentialAddressWin(subscriberContactRelationGid, routeId, btn) {
window.showModalDialog("SubscriberResidentialAddress.aspx" + BuildQueryStringValuesForSubscriber(subscriberContactRelationGid, routeId, returntxtReceiptDate().value), this, strWindowFeatures + ";scroll:no;dialogWidth:442px;dialogHeight:350px");
location.href = location.href;
}
</script>
So the above code opens up the page SubscriberResidentialAddress.aspx in a modal window (as well as passing a number of variables into this page)
Now this page is arranged in such a way that it either displays or edits information. (These are in separate divs that are displayed or hidden dependign on what buttons have just been pressed in the window).
Currently after a user makes a save to the information on the page it switches to display mode. Instead I want to close the modal window completely and reload the main page.
A colleague suggsested that I use a literal control on the aspx page of the modal window and write javascript to it after a successful save has been carried out. So I placed the following in the head section of the SubscriberResidentialAddress.aspx page:
<asp:Literal runat="server" ID="litCloseWind"></asp:Literal>
And then in the code behind in the function that is called when a save is carried out
protected void btnAddSave_Click(object sender, EventArgs e)
{
////// code related to saving
if (status.IsSuccessful)
{
litCloseWind.Text = "<script>this.close()</script>";
Response.Redirect(Request.Url.AbsoluteUri, false);
}
}
I have tried the above and also litCloseWind.Text = "window.close()"; but neither were successful in closing the modal window after a save is carried out.
Is this plan logically sound and have I just made an error somewhere or was this a bad way of doing it to begin with?
Why don't you register a script here, like this:
ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
No need of putting any Literal Control here.
So your code will look something like this:
protected void btnAddSave_Click(object sender, EventArgs e)
{
if (status.IsSuccessful)
{
ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
}
}

Ajax call page load twice Asp.net

I have a AjaxCall.aspx page that will be called another page by .ajax() using jquery.
At AjaxCall.aspx I had remove all the html tag and left only
<%# Page Language="C#" AutoEventWireup="true" CodeFile="AjaxCall.aspx.cs" Inherits="_AjaxCall" %>
and at the code behind it was a simple on the pageload event.
protected void Page_Load(object sender, EventArgs e)
{
Response.write("{ \"Testing\": \"Hello World!\" }");
}
I monitor the ajax request in Chrome developer tools in Network and I notice that every time I make a ajax request to AjaxCall.aspx page, it requested twice in a row. I later went into debug mode to check and discover that the pageload event fired twice.
I did some digging and found that an empty img src has something to do with this, but the problem is I have no any html tag in the AjaxCall.aspx page!
I later tried to add back the HTML body and form tag, and try to call that page again, and good news is it load once, but as soon as I add those html tag back to AjaxCall.aspx, it load twice again.
I can't have those html tag at my AjaxCall.aspx because it suppose to return a json format data when it is requested, having those html tag will cause error on whatever page that called it. At the same time I don't want it to continue load twice on every call I made. So is there anyway to overcome this problem?
If you want to deliver JSON response with application page (.aspx and code behind), you have to flush your response and close it in order to stop the process.
If you don't do that, the page .aspx will be return, may be with some HTML tag generating multiple request (like img with bad src)
Try this:
protected void Page_Load(object sender, EventArgs e)
{
Response.write("{ \"Testing\": \"Hello World!\" }");
Response.Flush();
Response.End();
}
Moreover, you can also use before your Response.write a Response.Clear in order to clear the optionnal content that could have been injected by any code, just to be sure to send only this content.

is it possible to understand at Code behind whether page is being called inside iframe or not

I need to understand that whether page is being called inside iframe or not at code behind. Is this possible?
I need to determine it in master page code behind.
asp.net 4.0, C#
In general, no.
Of course you can emit client script that detects iframe and reloads the page with e.g. a querystring.
It's not possible. However there's a workaround this. You can use querystring and check on page load if that querystring contains value, for example:
<iframe src="Default.aspx?iframe=true" />
In your Default.aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.QueryString["iframe"]))
{
if(Convert.ToBoolean(Request.QueryString["iframe"])
{
// this page is loaded in an iframe
}
}
}

Dynamically add html to panel and access the htmlcontrols

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..

Categories