Moving javascript from user control into appropriate master page - c#

I have a scenario like this (simplified for this question):
I have a master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Ads.master.cs" Inherits="ActiveDigitalSignage.Ads" %>
<!DOCTYPE html>
<html>
<head>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div class="wrapper">
<form id="form1" runat="server">
<header>
</header>
<section>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</section>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<asp:ContentPlaceHolder ID="script" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
I have a default.aspx page:
<%# Page Title="" Language="C#" MasterPageFile="~/Ads.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div><p>Test</p></div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="script" runat="server">
</asp:Content>
The way its setup is, any header stuff can go in the "head" section. The main stuff goes in the "ContentPlaceHolder" and any javascript goes into the "script" place holder. Everything is working fine as is right now.
The issue I'm running into is I want to use a User Control (.ascx) in my Default.aspx page. However there is javascript in the user control. Because the jQuery is at the bottom of the page (in the "script" section), the javascript in the user user control is breaking. Is there a way to make the javascript in the user control render inside the "script" placeholder?
I know I can just copy and paste the code into a .js file and load the javascript in my master page. The problem with doing that is there are a bunch of ASP.NET tags (e.g. <%= %>) in the javascript that reference controls in the user controls, so copying and pasting this into a .js file would break this.
I'm thinking, off the top of my head, there might be a way in the global.asax page, during prerender, that can detect javascript in a user control and dynamically move it into the proper section?

Related

Master Page won't display its content on a Web Forms

I am creating a Web Forms project with a Master Page. However, when I put a label on the master page (or any other control or html element) it does not display it on the web form. How can I fix this problem?
MasterPage:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="RWA.MasterPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form" runat="server">
<div>
<asp:ContentPlaceHolder ID="MasterPagePlaceHolder" runat="server">
<asp:Label ID="NameLabel" runat="server" Text="Label"></asp:Label>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Web Form:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master"
AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="RWA.Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MasterPagePlaceHolder"
runat="server">
</asp:Content>
Fixed it, only needed to remove the control from the content placeholder

A page can have only one server side form tag

I have a master page Header.master which contains a logout button, due to this reason I have to include a form with run at server in my master form.
Again I am using a form in my page because I want to use controls and asp.net compiler says that you cant use controls with form tag with run at server.
Header.master
<form runat="server"><asp:ImageButton ID="btn_logout" runat="server" OnClick="btn_logout_Click" Height="22px" ImageUrl="~/img/logout.png" Width="43px" /></form>
My Page
<%# Page Title="" Language="C#" MasterPageFile="~/Header.Master" AutoEventWireup="true" CodeFile="student_registration.aspx.cs" Inherits="SCMS.student_registration" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="frm_sr" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</form>
</asp:Content>
Remove Form tag from your page (My Page)
<form id="frm_sr" runat="server">
Form tag in master page is already enclosing all the code written in page itself
(<asp:Content ID=...>)

Loading User Controls with jQuery .load()

I am using a clean test page, and loading a user control with jQuery's .load() function.
The current error I get is this:
GET http://localhost:27950/OCDB/test.ascx 403 (Forbidden)
Here is my Page code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<%# Register Src="~/test.ascx" TagPrefix="uc1" TagName="test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>This is a test.</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#ucPlaceholder').load('test.ascx');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="ucPlaceholder">
</div>
</form>
</body>
</html>
And my user control code:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="test.ascx.cs" Inherits="test" %>
<input placeholder="test input"/>
As you can see, its super simple. My immediate guess is that the page isn't picking up the Register. Can anyone show me where I'm going wrong?
I don't think you can directly call a user-control using jQuery.
But you can take help of generic http handler for it.
http://blog.ovesens.net/2008/12/dynamically-loading-asp-net-user-controls-with-jquery/
http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery

Javascript Unable to GetElementById in ASP.NET C#

I know this is a very common problem. I was trying to get a "enter key" event to invoke a click on a certain button on the page through javascript. However, I can't for the life of me fetch a single element by its id or name from my asp.net page.
It is my understanding that this issue is related to where the javascript is located and whether the elements have been rendered by the time the javascript is loaded or something of that nature?
Every time I attempt to use var x = document.getElementById('btn_AddAdmin') I end up with a null value.
My asp.net page has SiteMaster page that it inherits from. I have included a trimmed down version of my asp.net page and the SiteMaster page.
ASP.NET Page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="HERMES.Admin" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<input type="text" id="txt_AddAdmin" runat="server" onkeypress="Populate()" />
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs"
Inherits="HERMES.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="./Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
</form>
</body>
Sorry for the formatting, I seem to be doing something wrong...
I have attempted placing the javascript in many different locations -- anywhere from the sitemaster page to the top and bottom of my asp.net page. No luck either way.
var x = document.getElementById('<%= btn_AddAdmin.ClientID %>');
If your button is a Server Control it prepends a naming scheme to the input button so it can be bound back to the appropriate server control during postback. Thus the need to use the ClientID property to retrieve the "rendered id".
To learn more visit:
http://www.asp.net/web-forms/overview/client-script,-jquery,-and-ajax
http://www.asp.net/
Google it with Bing:
clientid
aspnet webforms client side programming

How do I keep the master page from flickering?

I am building an ASP.NET application and would like to use a master page to display branding and navigation info and move through several content pages. However, every time I try to load new content the entire page flickers. Is there anyway to avoid this?
Master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Interface.Site" %>
<%# Register TagPrefix="customControl" TagName="NavigationBar" Src="NavigationControl/NavigationControl.ascx" %>
<!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>Registration</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="Stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="RegistrationMasterForm" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<%-- Contents of this div span the top of the page for branding --%>
<div id="BrandingBanner">
<asp:Image ID="Banner" runat="server"
ImageUrl="Images/BrandingBanner.png"/>
</div>
<%-- Navigation Bar, the contents of this div should remain unchanged --%>
<div id="NavigationBar">
<customControl:NavigationBar ID="navBar" runat="server"/>
</div>
<%-- Contains any forms or controls that may be uniquie to the page --%>
<div id="FormControls">
<div id="FormContent">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</div>
</form>
</body>
</html>
Content:
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Interface.WebForm2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Check me out! I am content for the master page!
<asp:Button ID="Button1" runat="server" Text="Next" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
content code behind:
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm3.aspx");
}
}
The control event triggers a post back to your page, which typically creates the "blanking" effect that you see. To mitigate this, you may want to consider using some sort of Ajax solution to cause a partial postback or async postback to prevent the "blanking".
However in the particular case of your button click, if all you are trying to do is bring the use to another page, you really should just use an <a href="WebForm3.aspx"> tag and avoid using Response.Redirect.
You are redirecting the page in your codebehind.
This causes the browser to change pages, and redraw the whole thing, at least IIRC. I haven't used updatepanels in ages.
Isnt this only possible if you're using frames? As far as I'm aware the master page and content pages bind together on the serverside and push down the the client as a whole.
Where what you want sounds like using an page for branding and navigation with an inline frame to serve up your convent. This would prevent the outer page from "flickering" on the client side.
Edit: Though it is not the new way to do things. You'd want to look at something using maybe an UpdatePannel or other AJAX style design.
You don't need to move the updatepanel, your redirect is causing the browser to load a new page.

Categories