ASP .NET Reference Web Element - c#

I'm new to ASP .NET. I'm attempting to duplicate the example found here for a simple in-browser PDF editor.
I made a blank ASP .NET project in Visual Studio and added _Default.aspx with the code from the link above:
_Default.aspx
<%# Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Assembly="RadPdf" Namespace="RadPdf.Web.UI" TagPrefix="radPdf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>RAD PDF Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<radpdf:pdfwebcontrol id="PdfWebControl1" runat="server" height="600px" width="100%" />
</div>
</form>
</body>
</html>
_Default.aspx.cs
using System;
using RadPdf.Web.UI;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Get PDF as byte array from file (or database, browser upload, remote storage, etc)
byte[] pdfData = System.IO.File.ReadAllBytes(#"C:\Users\scotth\Desktopt-1 work order - Copy.pdf");
//Load PDF byte array into RAD PDF
this.PdfWebControl1.CreateDocument("Document Name", pdfData);
}
}
}
However, I get an error on the last line of _Default.aspx.cs:
this.PdfWebControl1.CreateDocument("Document Name", pdfData);
The error is:
'_Default' does not contain a definition for 'PdfWebControl1' and no extension method 'PdfWebControl1' accepting a first argument of type '_Default' could be found (are you missing a using directive or an assembly reference?)
Why is this reference broken? It seems like it should be referencing this in _Default.aspx:
<radpdf:pdfwebcontrol id="PdfWebControl1" runat="server" height="600px" width="100%" />
Yet I have this error. How do I fix this reference? What is the standard way to reference these elements in ASP .NET? Thanks in advance.

Related

The 'Name' and 'Src' attributes are mutually exclusive in aspx page without code behind

I have a webform (default.aspx) page without code behind in my website. Here i am using a dll reference to call method from it. Below are the Code details what i have done.
<%# Page Language="C#" %>
<!DOCTYPE html>
<%# Import Namespace="System.Data;"%>
<%# Import Namespace="System.Data.SqlClient;"%>
<%# Assembly name="ClassLibrary" Src="~/Bin/ClassLibrary.dll"%>
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
ClassLibrary.Class1 obj = new ClassLibrary.Class1();
Response.Write(obj.Test());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
When I am starting executing my website it gives an error The 'Name' and 'Src' attributes are mutually exclusive
This means you can have one or the other. IE, <%# Assembly name="ClassLibrary" %> or <%# Assembly Src="~/Bin/ClassLibrary.dll"%>
Basically, 'Name' is going to tell it to go look for an assembly in the GAC and private probing path for a matching assembly, while 'Src' tells it the Assembly you want to load is 'right here at this path'.

Request.Form is empty (asp.net c#)

I've been doing some really clever stuff (I think) in ASP.Net c#, so much so that the simple stuff is more difficult (if that makes sense)
I have this snippet of code in my page
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" />
<asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" Text="Confirm">
</asp:LinkButton>
</form>
I have this snippet of code in confirm.aspx.
if !(IsPostback)
{
lblConfirm.Text = Request.Form["hdnConfirm"]
}
I was expecting this to be nice and simple but when i click the button and go to page "confirm.aspx" the Request.Form has no values. What have I missed ?
[TESTING]
I ran a test on a brand new web forms project in VS2013. Dot.Net 4.5.1 This does not work. PreviouPage is always null. Whether surrounded by (!IsPostBack) or not. Doesn't matter if the submitting control is a Button, LinkButton or Hyperlink. Request.Form["hdn"] is also null. I have restarted my computer just in case and still no joy. I am missing something really simple I am sure of it but I can't see what
This is the first page nothing in the code behind
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton runat="server" PostBackUrl="~/WebForm2.aspx">click</asp:LinkButton>
<asp:HiddenField runat="server" ID="hdn" Value="3" />
</div>
</form>
</body>
</html>
This is the second page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = ((HiddenField)this.PreviousPage.FindControl("hdn")).Value;
}
}
}
On confirm.aspx use PreviousPage.FindControl instead :
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
hiddenValue = hdnFieldName.Value;
}
Here is good example to get you started.
What's going on Here:
By default with VS 2013 and Asp.Net v4.5.1 , there is a feature FriendlyUrls which is enabled.
The FriendlyUrls feature does kind of Asp.Net routing , thus changing URLs from localhost/webform1.aspx to localhost/webfrom1
PostbackUrl property will not work in combination with Asp.Net Routing. When ASP.NET routing is in use the PreviousPage URL is the final routed URL. The runtime will check for a file represented by the Routed URL now, which will be webform1 and NOT webfrom1.aspx. Since there is NO such file as webform1, so it will always set the PreviousPage to null.
Possible Solutions:
So, now you know the issue at hand.
1.) Either don't use the Routing system of Asp.Net Friendly Urls , in this case, therefore try adding the <%# PreviousPageType VirtualPath="~/WebForm1.aspx"%> to webform2.aspx page and check.
2.) OR if you keep the FriendlyURLs and hence the Routing system, make changes in your code to read the Form values using some other alternatives.

How do you reference HTML/ASP elements with C# objects?

I'm new to ASP.NET and C#, I've primarily worked with Java. I want to dynamically add nodes to a tree view. I've followed a few tutorials but whenever I implement them they do not appear to work. I keep getting an error: "The name 'MyTreeView' does not exist in the current context".
Here's the C# code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LocalTest
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyTreeView.Nodes.Add(new TreeNode("Node1"));
MyTreeView.Nodes[0].ChildNodes.Add(new TreeNode("ChildNode"));
}
}
}
}
And the HTML/ASP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="MyTreeView" Runat="server">
</asp:TreeView>
</div>
</form>
</body>
</html>
"MyTreeView" is the ID of the treeview contained in the Default.aspx file.
The few tutorials I have followed seem to access the treeview directly with its ID as seen above which makes very little sense to me. For instance, if I were to do this in Android I would have to use the findViewById and establish a link between the XML and the Object.
Does anyone have any suggestions? I've bumbled around this site for a bit and I'veonly found similar issues but I cannot get a clear answer.
Thank you.
Looking at the code above, you are using what's known as ASP.Net WebForms where there are "server side" controls (e.g. <asp:TreeView ID="MyTreeView" Runat="server">)
The "page" (html aspx) needs to somehow "wire itself" to the code (c# file). It will need a "server-side" directive like so:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormsApplication.WebForm1" %>
<!DOCTYPE html>
//...the rest of the html goes here plus web controls, etc.
At which point, you can see stuff like Inherits="WebFormsApplication.WebForm1" that maps to the Class:
namespace WebFormsApplication
{
public partial class WebForm1 : System.Web.UI.Page
....
found in the WebForm1.aspx.cs file, also in the directive CodeBehind="WebForm1.aspx.cs"
So voila :)
establish a link between the XML and the Object.
Hth...
You do not have TreeView control in default.aspx. It is in .html file and default.aspx.cs can not see TreeView control.
It seems you are missing Page directive in your asp.net page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

Using Tinymce results in Internal server error in asp.net

I am using Tinymce in my web page and in client side every thing works fine. But when I try to access the tinymce textbox in code behind the page control doesnot render. I mean Page load Didn't Render. Here is my HTML Code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="RichText.aspx.cs" Inherits="TESTING.RichText"
ValidateRequest="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Rich Text</title>
<script src="<%=ResolveClientUrl("~/Script/jquery-ui-1.7.1.custom.min.js") %>" type="text/javascript"></script>
<script type="text/javascript" src="Scirpt/tinymce/jscripts/tiny_mce/tiny_mce.js"> </script>
<script type="text/javascript" src="Scirpt/tinymce/jscripts/tiny_mce/InitializeRichTextBox.js"> </script>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 400px">
<div style="height: 100px; width: 100%;">
</div>
<asp:TextBox ID="TextBox1" runat="server" TextMode="multiline" CssClass="RichTextBox"
Width="150px"></asp:TextBox>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
Here is my Code Behind code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TESTING
{
public partial class RichText : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string here = TextBox1.Text;
}
}
}
When i leave empty text then Page Load renders and when I write something and click on Button then this error occurs A potentially dangerous Request.Form value was detected from the client (TextBox1="<ul><li><em><span ..."). I tried to find the solution in internet but unable to find the solution. Please help me out to find the solution.
Add this in Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime requestValidationMode="2.0"/>
</system.web>
</configuration>
and add ValidateRequest = "false" in Page directive like this
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
for your reference click here

Passing value from JScript to ASP.NET Control

I am trying to pass a value from a JScript function to an ASP.NET Control but I am not able to make it work. What am I doing wrong here:
aspx.cs
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function saveMyData() {
var myData = "TestData";
var hiddenInput = document.getElementById('<%=HiddenField1.ClientID %>');
hiddenInput.Value = myData;
}
</script>
</head>
<body onload="javascript:saveMyData();">
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<br><input type="submit" value="Press Me!"><br> <--New Code
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class _Default : Page
{
protected HtmlInputHidden HiddenField;
protected void Page_Load(object sender, System.EventArgs e)
{
string value = HiddenField1.Value;
}
}
}
**UPDATE**
I added in a Submit button but I am still not able to assign the myData Value to the Hidden Control, any thoughts?
All you're missing is a postback. You will need the page to post back before it is able to read the newly inserted value into HiddenField1. The javascript is adding the value to HiddenField1 after Page_Load.
What are you trying to do? From what it looks like, you try to read out the value you set via Javascript (on the client side), but you never actually returned the data to the server.
You wont be able to read it without a PostBack, because the javascript does not get executed on the server side.
Add a button, make a Postback and see if you can read out the data then.

Categories