Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘HTML’

Refused to display url in a frame because it set ‘X-Frame-Options’ to ‘SAMEORIGIN’.

Posted by Viral Sarvaiya on July 21, 2016


Hello All,

After a very log time adding new post in my blog.

Few days before I get one problem that my MVC website is not opening in Iframe.

for eg. my URL is http://localhost/websitename/page

This URL I am opening in Iframe tab like below.


<iframe src="http://localhost/websitename/page" width="500" height="500" />

and getting error as below.

Refused to display ‘http://localhost/websitename/page’ in a frame because it set ‘X-Frame-Options’ to ‘SAMEORIGIN’.

and it is not opening my website in IFrame.

Solution-1:

Open Global.asax.cs file, and add one method as below.

protected void Application_PreSendRequestHeaders()
 {
 Response.Headers.Remove("X-Frame-Options");
 Response.AddHeader("X-Frame-Options", "AllowAll");
 }

This will open website in Iframe.

Solution-2:


protected void Application_Start()
 {
 AntiForgeryConfig.SuppressXFrameOptionsHeader = true;
 }

This will open website in IFrame

Hope this will helps you.

Thanks.

Posted in .Net, ASP.NET, C#, MVC | Tagged: , , , , | Leave a Comment »

Get HTML code from URL in ASP.NET

Posted by Viral Sarvaiya on November 10, 2009


To get HTML of web page you need only few lines of code.

To start, place two TextBox controls named txtURL and txtPageHTML, and one button control on web form, like in image bellow:

Web form for getting page HTML at design time

Now, on button’s click event function, place this code:

[ C# ]


// We  need these namespaces
using  System;
using  System.Text;
using  System.Net;

public partial class DefaultCS : System.Web.UI.Page
{

protected void  btnGetHTML_Click(object sender, EventArgs e)
{
// We'll use WebClient class for reading HTML of web  page
WebClient MyWebClient = new WebClient();

// Read web page HTML to byte array
Byte[] PageHTMLBytes;
if (txtURL.Text !=  "")
{
PageHTMLBytes =  MyWebClient.DownloadData(txtURL.Text);

// Convert result from byte array to string
// and display it in TextBox txtPageHTML
UTF8Encoding oUTF8 =  new UTF8Encoding();
txtPageHTML.Text  = oUTF8.GetString(PageHTMLBytes);
}
}
}

[ VB.NET ]

‘ We need these namespaces


Imports  System
Imports  System.Text
Imports  System.Net

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnGetHTML_Click(ByVal  sender As Object, ByVal e As  System.EventArgs) Handles btnGetHTML.Click
' We'll use WebClient class for reading HTML of web  page
Dim MyWebClient As  WebClient = New WebClient()

' Read web page HTML to byte array
Dim PageHTMLBytes() As Byte
If txtURL.Text <> "" Then
PageHTMLBytes =  MyWebClient.DownloadData(txtURL.Text)

' Convert result from byte array to string
' and display it in TextBox txtPageHTML
Dim oUTF8 As  UTF8Encoding = New UTF8Encoding()
txtPageHTML.Text =  oUTF8.GetString(PageHTMLBytes)
End If
End Sub

End Class

Now you can start sample project, type some valid URL in first TextBox control and click to “btnGetHTML” button. Code listed above will return HTML code of requested URL and display it in second text box, like in image bellow:

HTML code is read and shown in text box

As you see, loading of HTML code of web page is relatively easy. Analyzing of this data is much harder and depends of page structure.

Reference :

http://www.beansoftware.com/ASP.NET-FAQ/Read-Page-HTML.aspx

Posted in ASP.NET | Tagged: , , , , | 5 Comments »

Get HTML Input Controls Values Server Side

Posted by Viral Sarvaiya on July 17, 2009


Many times a need arises to use HTML Controls rather than HTML controls. In that case the issue is how to get the values Server Side. See the example below

<input type = “text” id = “txt” value = “Hello” name = “txt” />

Now the name attribute is important Since Server Side You will need to access the controls using the name field. It is not necessary that the ID is same as the name it can be different

On Server Side

If Not Request.Params(“txt”) Is Nothing Then

Dim strVal As String = Request.Params(“txt”)

End If

Now most interesting part, suppose you have multiple TextBoxes and you need to get values of all of them then just give all of them same name attribute different ID is not an issue

<input type = “text” id = “txt1″ value = “Hello” name = “txt” />

<input type = “text” id = “txt2″ value = “Hello” name = “txt” />

<input type = “text” id = “txt3″ value = “Hello” name = “txt” />

<input type = “text” id = “txt4″ value = “Hello” name = “txt” />

<input type = “text” id = “txt5″ value = “Hello” name = “txt” />

Then on server Side

If Not Request.Params(“txt”) Is Nothing

Then Dim arr() As String = Request.Params(“txt”).Split(“,”)

For i As Integer = 0 To  arr.Length – 1

Dim strVal As String= arr(i)

Next End If

You will get values of all TextBoxes separated by comma.

Now you can split them and get it into an array as shown above

and the other way is also there, you can also use the “Request.form(“txt”) for geting the value of the html control.

This technique works for
1. TextBoxes
2. CheckBoxes
3. RadioButton
4.Select (DropDownList)

Posted in ASP.NET | Tagged: , | Leave a Comment »

querystring in to javascript and HTML page Redirect.

Posted by Viral Sarvaiya on March 13, 2009


In the HTML page, we can not use the value of one page to second page without useing server side coding.

to do that we have to use javascript.

Step 1 :  Make a file1.html

<HTML>
<HEAD>
</HEAD>

<BODY>

<Form name=”frmDefault” action=”file2.htm” method=”get”>

<p>Item1: <Input Type=”text” name=”txtItem1″></p>
<p>Item2: <Input Type=”text” name=”txtItem2″></p>
<p><Input type=”submit”></p>
</Form>

</BODY>
</HTML>

step 2 : Make file2.html

<HTML>
<BODY>
<Script Language=”Javascript”>
<!–//
var arrArgs = location.search.substring(1).split(“&”);

for (var i=0; i<arrArgs.length; i++) {
document.write (‘<p>’ + arrArgs[i] + “</p>”);
}
//–>
</Script>
</BODY>
</HTML>

javascript write a value of querystring that comes to another  page.

Posted in Javascript | Tagged: , , , , , | Leave a Comment »

 
%d bloggers like this: