ASP.NET Web Site to Web Application conversion error - asp.net

We had a Visual Studio Website project.
We needed a .VBPROJ so I had to convert our Web Site Project to a Web Application Project.
I followed several walktrough, did everything well for the conversion :
create new webapp project
add references
copy files (from website
to webapp folder)
include them in the project
click "Convert to Web App".
After all that , i get three identical compilation errors, javascript related with an .ASPX page.
<%# Page Language="VB" AutoEventWireup="false" MasterPageFile ="~/GAR.master" Inherits="GARWA._Default" Codebehind="Default.aspx.vb" %>
'TableWeekID' is not declared. It may be inaccessible due to its protection level.
<script type="text/javascript" language="javascript">
//Very important variable!
var TableWeekTag = '<%=TableWeekID%>'
var DivYearSmallTableTag = '<%=DivYearSmallTable%>'
var TableNameTag = '<%=TableNameID%>'
What's the problem here ?
Thanks

You can do it in this way.
Use a Hiddenfield control and store the server variable value in it.
One hidden field for each server variable.
And get the value into the javascript function document.getElementById()
Use the below to POPULATE the serverside input control(s) datavalues first.
ASPX SERVERSIDE CODE
svrTableWeek.Value = TableWeekID
svrDivYear.Value = DivYearSmallTable
svrTableName.Value = TableNameID
Also, if you opt for the SERVERSIDE Inputs then you need the following HTML in your ASP page
USING ASP SERVER CONTROLS FOR HIDDEN VALUES
<asp:HiddenField ID="svrTableWeek" runat="server" />
<asp:HiddenField ID="svrDivYear" runat="server" />
<asp:HiddenField ID="svrTableName" runat="server" />
And if you prefer CLIENTSIDE inputs then use the code below instead of ALL of the above
USING HTML (clientside) CONTROLS FOR HIDDEN VALUES
<input type="hidden" id="svrTableWeek" name="svrTableWeek" value="<%=TableWeekID%>" />
<input type="hidden" id="svrDivYear" name="svrDivYear" value="<%=DivYearSmallTable%>" />
<input type="hidden" id="svrTableName" name="svrTableName" value="<%=TableNameID%>" />
Finally regardless of which of the two above methods you choose,
now your ready to re-use those values in your clientside javascript
CLIENT SIDE SCRIPT ROUTINES
<script type="text/javascript" language="javascript">
//Very important variable!
var sTableWeek = document.getElementById('svrTableWeek').value;
var sDivYear = document.getElementById('svrDivYear').value;
var sTableName = document.getElementById('svrTableName').value;
It seems a bit long winded I know, but it should work without issues.

Try global replacing all of the "~/GAR with "GAR . I just converted a Web Site to a Web App, had similar odd errors, all because the virtual reference to the master page files was no longer correct. My first indicator was a DIV ID that wasn't seen in a code-behind file.

Related

will it be changed .aspx.cs file after converting server controls into html controls?

my question was that when I send the request to the web server, of course, the web server will take the request and give me a page that will be in the HTML format so I want to know when the web server will accept the request and of course, the request will be for .aspx page and .aspx page will be executed by the web server and all the asp server-side controls that all are on the .aspx page will be converted into HTML format so then I want to know here in the Testing. asp. cs file will be changed as well? because finally, the .aspx page will be converted into HTML you know when we get a request from an HTML tag like an HTML form tag then we need to use there like this
int no1=Convert.ToInt32(Response["textbox1"]);
int no2=Convert.ToInt32(Response["textbox2"]);
instead of this
int no1 = Convert.ToInt32(textbox1.Text); int no2 = Convert.ToInt32(textbox2.Text);
so I want to know after converting server-side controls into HTML then .aspx.cs will be changed or not
these two lines will be converted into like this int no1=Convert.ToInt32(Response["textbox1"]); int no1=Convert.ToInt32(Response["textbox2"]); these two lines will be converted like this or not
file- Testing.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="login.Testing" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="background-image:url(C:\Users\Sudarshan\source\repos\login\login\Images\uWjEogFLUTBc8mSvagdiuP.jpg)">
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
Enter the Number 1:<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Enter the Number 2:<asp:TextBox ID="textbox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<center>
<br />
<br />
<asp:Button ID="button1" runat ="server" Text="Add" Height="38px" OnClick="Unnamed1_Click" Width="101px" /> </center>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
file name- Testing.aspx.cs
Request["textbox1"]
Request["textbox2"]
our .cs file? It NEVER changes, and is compiled code. So, in fact, both the aspx page and the code behind .cs page should not under go changes. But, no reason why I can think of that the cs page behind would be changed, has to be changed, or would require to be changed. The aspx page from server is loaded, then a page class is created based on the aspx page, and then your code behind can and does operate against that class instance of the page - it does not really operate against the aspx page, but a loaded aspx page converted into a page class object. Your code runs against that class object.
So, your code behind is not really running against the aspx page. But that aspx page is converted + loaded into a page created class. It is this class object that your code behind runs against. this is kind of like loading "json" into a object when you de-serlize the json object.
So, the aspx page is really ONLY used for creating a page class object. it is this page class object that your code runs against.
So, when you go
TextBox1.Text = "Albert"
That code is not running against or modifying the aspx page. Your code is modifying the class object that was created from that aspx page. Then the class object is converted into HTML when rendered for the client side.
So, as your code "runs" it is NOT making changes to the aspx file, it is making changes to the page class object that was created from that aspx page.
ZERO changes are made to the aspx page for this to work. The aspx page becomes your class object (the page class), and your code runs against that class with properties and methods like any C# class object has.
when you use code behind say to inject script, or even add controls to the page? You not operating against the aspx page at that point in time, but are in fact operating against the page class object - no changes EVER make their way back to the aspx page, and ZERO concept exists that your code behind is making changes to the aspx page - it NEVER does (or better said never should!!).
So, your code behind operates against the class object created from that aspx page. From that point on, your code is making changes to the class object, and does not care or modify the original aspx page. As a general rule, that original page is never changed or touched at all (once it been loaded and converted to that page class object).
So no changes have to occur to your c# code, but that code NEVER operates against the aspx page, but ONLY every operates against the page class object that was created for you from the aspx page. Your c# code runs BEFORE any conversion of the page class to standard HTML which is then sent to the client.

Using sections in ASP.NET Webforms

I am having a bootstrap problem I hope one of you might have a solution to.
I have placed all my JavaScript references in the bottom of my masterpage. This usually works fine, but now I have a ASCX control which needs to add some JavaScript too the footer (initialization of a module). The reason why I can't initialize the module from the master page is because I need some properties from my codebehind file.
In ASP.NET MVC I would have used sections to inject data from a usercontrol to a section in the masterpage, but is this even possible in ASP.NET Webforms 4?
No, that concept of sections in not available in WebForms.
One way to do what you've described is to use the <%= %> syntax and send the values of server properties to the client (HTML output). In your ASCX control you can have the following markup:
<script type="text/javascript">
var clientProperty = <%= MyServerProperty %>;
</script>
ASP.NET WebForms will substitute the value of MyServerProperty above when it renders the page, and then you can access clientProperty as a global variable from the script in the masterpage.
Another approach is to use a Hidden field and set its value on the server. It will be rendered as an <input type="hidden">, whose value you can then get from any script by ID.
A third option is to load the actual client script only from the ASCX control (when it makes sense), rather than put it in the masterpage (when it will be loaded everywhere in the site).
You could add something like this to the bottom of your master page:
<asp:PlaceHolder runat="server" ID="javascriptSection" />
Then in your page's code behind or in a <% %> tag:
var scriptTag = new HtmlGenericControl("script");
scriptTag.Attributes["type"] = "text/javascript";
scriptTag.InnerHtml = #"function () { ... }";
var javascriptSection = this.Page.Master.FindControl("javascriptSection");
if (javascriptSection != null)
javascriptSection.Controls.Add(scriptTag);

ScriptManager globalization issue

I'm having a problem with the asp:ScriptManager not working correctly under SharePoint. I have set EnableScriptGlobalization="true" to be able to use the Sys.CultureInfo object of ASP.NET AJAX, but because of the script is being output in the wrong order, it's not working. The same code works correctly under a plain ASP.NET site, so the issue seems related to SharePoint somehow. Though it did work correctly under SharePoint 2007.
To reproduce the issue, I created this simple usercontrol CultureControl.ascx.
<%# Control Language="C#" %>
Server side culture: <%= System.Threading.Thread.CurrentThread.CurrentCulture.Name %><br />
Client side culture: <script type="text/javascript">document.write(Sys.CultureInfo.CurrentCulture.name);</script><br />
When I include this control on a regular ASP.NET web site .aspx page, and set the culture to sv-SE on the server side, the output is as expected.
Server side culture: sv-SE
Client side culture: sv-SE
But when I put the same usercontrol on a page in a SharePoint 2010 site, set the site locale to Swedish and set EnableScriptGlobalization="true" in the master page, I still get the output
Server side culture: sv-SE
Client side culture: en-US
When I started digging in to this problem, I noticed that the reason was that the script blocks that the ScriptManager outputs are in the wrong order. When EnableScriptGlobalization is set to true, the ScriptManager outputs a script block that defines a __cultureInfo variable, which is then used to initialize the Sys.CultureInfo. This variable has to be defined before MicrosoftAjax.js is loaded, otherwise it will default to en-US culture.
In the ASP.NET site, this works correctly. The __cultureInfo variable is defined first, then MicrosoftAjax.js is loaded from WebResource.axd. But under SharePoint, MicrosoftAjax.js is loaded a lot earlier and the __cultureInfo variable is defined too late.
Does anyone know of a solution or workaround for this issue?
One solution i am doing and its working perfectly. Add the following at the end of your page (Master Page)
<script>
if (__cultureInfo)
{
Sys.CultureInfo.CurrentCulture = Sys.CultureInfo._parse(__cultureInfo);
delete __cultureInfo;
}
</script>
UPDATE: I wrote a wrong solution before, now the above one is properly updated.

Asp.Net Add UserControl to a Page

I have an ASP.Net web site and on one of the pages I'm using a repeater to render several iterations of a UserControl, that UserControl contains a second UserContol that has two text boxes that my User must enter information into. I want to be able to have my user push a button and add another instance of the second UserControl (with the two textboxes) to my original UserControl, so that the user has 4 textboxes displayed on the screen for the first UserControl. The problem I am seeing if I try to add the second UserControl to a given iteration of the first, is that the page postback causes any other of these second user controls to be deleted from the page.
Does anyone know of a way to do this using JQuery? I've had three posts that describe how to solve this problem using server side dynamic controls, and/or AJAX, but we've decided to focus on a JQuery solution because this server side mechanism is too costly in terms of resources for us.
I've been working on the suggestion by Zincorp below, and now have the JQuery working to clone a textbox, but having trouble using the server side Request.Form collection to iterate over the controls. Can anyone give adivce on how to iterate over the Request.Form collection?
OK, I think the problem with iterating over the controls using the Request.Form.AllKeys collection turned out to be that I was using an HTML Textbox, rather than an ASP TextBox Control. Apparently the Request.Forms.AllKeys collection only contains ASP controls, not HTML controls.
The problem I am seeing now is that when I clone the control in JQuery, and then submit my page with the submit button, the 2 controls have the same ID, and so are combined (I think) by http into one ASP TextBox Controls containing both values, with a comma delimiter (e.g.- 40,20). Anyone know how to get a new ID assigned to the cloned ASP TextBox?
Here is the updated markup in a sample ASP.Net web appliction:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="ProjectDisplay" Text="Project" />
<asp:TextBox ID="ProjectValue" runat="server" ></asp:TextBox>
<div id="mydiv" ></div>
<br />
<br />
<br />
<input id="AddProject" type="button" value="Add Project" />
<br />
<br />
<asp:Button ID="Submit" runat="server" Text="Submit" onclick="Submit_Click" />
</div>
</form>
</body>
</html>
<script language="jquery" src="js/jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript" >
$(document).ready(function() {
$("#AddProject").click(function() {
var x = $("#ProjectValue").clone();
x.appendTo("#mydiv");
});
});
</script>
And here is the updated server side code where I'm trying to iterated over items in the Request.Form collection to get information from it:
public partial class _Default : System.Web.UI.Page
{
protected void Submit_Click(object sender, EventArgs e)
{
foreach (string s in Request.Form.Keys)
{
object x = Request.Form[s];
}
}
}
Before choosing a solution for the problem, consider the problem first:
You effectively need to:
1) Duplicate controls/markup on the client-side
2) Obtain these values on the server-side on a postback
3) For each of the added "user controls" on the client-side, add them as children of the first user control on the server side.
I'm not going to give the code, but here are some ideas:
1) Use jQuery's .clone() method (http://api.jquery.com/clone/) to duplicate the markup being rendered by the usercontrol containing the textboxes. Perhaps wrap them in a div without runat="server" to so that you can easily obtain it by ID. You'll probably need to then recursively iterate through the children in the cloned element and append a # to them to avoid any conflicting identifiers. (eg. nameTextBox_1, nameTextBox_2, etc.)
2) On a postback, use the Request.Form collection on the server side to obtain the textbox values. Iterate through it and snag all of the items whose keys start with "nameTextBox_".
3) For each added "user control" on the client side, create the actual user control on the server side, assign to it the values entered in the textboxes, and then add it to the child controls of the first one. This way the state is maintained upon returning to the user.
The short answer is that you would have to add the controls before the ViewState is initialized.
The video on this site has a nice guide on adding dynamic controls. http://www.asp.net/%28S%28wnmvzu45umnszm455c1qs522%29%29/learn/ajax-videos/video-286.aspx
This is probably not a ViewState issue. That may be the first place to look, but after that you need to make sure that your dynamic controls are actually being CREATED on each load.
Generally speaking, the ViewState is only responsible for restoring STATE to existing controls (hence the name). It is not responsible for recreating controls.
I believe you are experiencing a viewstate problem. Your dynamic controls are not being persisted. If you haven't already, read Dave Reed's excellent article Truly Understanding Viewstate. Pay particular attention to the section "5. Initializing dynamically created controls programmatically" which applies to the trouble you are experiencing.

Including Javascript and css in Master pages and User Controls in asp.net

I am trying to include the js and css in my code behind in my master pages and user controls in Page_Load event.
But apparently, js breaks since Page_Load of user controls loads BEFORE Page_Load of a master page. I include my jquery libs used across the site in my master pages, but scripts used in user control are included in user control only. The thing is that user control js scrips uses jquery functionality (as it should), but when it tried to run, it breaks since jquery libs are not loaded yet.
Anwyays, is there a way around this frustrating mess?
I use this to include js in my code behind. relativeResolvedPath is basically ResolveUrl(url)
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes.Add("type", "text/javascript");
js.Attributes.Add("src", relativeResolvedPath);
Page.Header.Controls.Add(js);
First, Page.Controls.Add will add the script block at random places in your html document. You should really only be adding script files in the header, or at the bottom of the page (searching online will tell you that the bottom of the page is preferred for better UI performance).
In order to ensure proper order of your javascript files, I would look into the ASP.Net AJAX Script Loader and the Script Manager.
The Script Manager will work if you add the scripts in the proper order. Your problem would be better solved using the AJAX Script Loader
Use a ScriptManager. It's very easy:
<asp:ScriptManager ID="SM1" runat="server">
<Scripts>
<asp:ScriptReference Name="Script.js" />
</Scripts>
</asp:ScriptManager>
To make it even easier, you just add an empty script manager control to your masterpage:
<asp:ScriptManager ID="MasterPageScriptManager" runat="server" />
And a script manager proxy on all your pages/controls which will have custom scripts loaded:
<asp:ScriptManagerProxy ID="SM1" runat="server">
<Scripts>
<asp:ScriptReference Name="Script.js" />
</Scripts>
</asp:ScriptManagerProxy>
Edit: if this isn't what you're looking for, I apologize. It sounds like you're just trying to work out the proper location of your scripts. Another thing you can do if this is the case is to include jQuery in your masterpage and any future scripts as:
<script type="text/javascript" src='<%= ResolveUrl("~/js/script.js") %>'></script>
I would recommend not loading javascript from the codebehind unless you're either dynamically building the javascript code, or you're building the application and releasing it to someone to tweak the UI and you don't want that functionality tampered with.
If you're loading alerts or something similar, use something like:
string myScript = "$(document).ready(function() { alert('message');});";
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "alerting", myScript, true);
Use Page_Init in your MasterPage to make code run before child page and UserControl Page_Load events.
Don't forget to do the occasional "View Source" as a sanity check. You gotta make sure that stuff goes into the response in the order you expect.
The client script used in your UserControls will not execute until the response is sent to the user agent, so as long as the jQuery libraries are included earlier in the response, you should be ok. This is not actually a lifecycle issue unless you have server code mixed in with the client code that you are sending.
I have this in the head of my MasterPage
<link rel="shortcut icon" href="<%#ResolveUrl("~/favicon.ico" ) %>" />
<script type="text/javascript">
var applicationRoot = "<%# webController.AppUrl %>";
</script>
<asp:Literal ID="litJqueryMinified" runat="server" /><asp:PlaceHolder id="phjQuery" runat="server"><script src="Script/jquery-1.3.2.js" type="text/javascript"></script></asp:PlaceHolder>
<asp:Literal ID="litJqueryUIMinified" runat="server" /><asp:PlaceHolder id="phjQueryUI" runat="server"><script src="Script/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script></asp:PlaceHolder>
<script src="<%#ResolveUrl("~/Script/jquery.fancybox-1.2.1.pack.js" ) %>" type="text/javascript"></script>
<script src="<%#ResolveUrl("~/Script/jquery.maskedinput-1.2.2.min.js" ) %>" type="text/javascript"></script>
<script src="<%#ResolveUrl("~/Script/jquery.cycle.all.min.js" ) %>" type="text/javascript"></script>
and this in the codebehind:
// Swap the jQuery inlcude for the minified version
phjQuery.Visible = false;
phjQueryUI.Visible = false;
string jQueryAddress = currentSettings != null && currentSettings["jQueryLocation"] == "local" ? ResolveUrl("Script/jquery-1.3.2.min.js") : "https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
string jQueryUIAddress = currentSettings != null && currentSettings["jQueryLocation"] == "local" ? ResolveUrl("Script/jquery-ui-1.7.2.custom.min.js") : "https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js";
litJqueryMinified.Text = "<script src=\"" + jQueryAddress + "\" type=\"text/javascript\"></script>";
litJqueryUIMinified.Text = "<script src=\"" + jQueryUIAddress + "\" type=\"text/javascript\"></script>";
This allows me to have it minified or hosted by google in production, but use the full version in dev, with intellisense.
Don't forget this in your Page_Load to get ResolveUrl to work:
Page.Header.DataBind();
Hope this helps. :)

Resources