Loading User Controls with jQuery .load() - c#

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

Related

Register alternative tagprefix for standard controls

I want to create a tag prefix for controls in System.Web assembly.
<%# Page Language="C#" CodeBehind="SimpleCustomControl.aspx.cs" Inherits="SimpleCustomControl" %>
<%# Register TagPrefix="CC" Namespace="System.Web.UI.WebControls" Assembly="System.Web" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CC:TextBox runat="server"></CC:TextBox>
</div>
</form>
</body>
</html>
But getting "Unrecognized tag prefix or device filter CC. Can not resolve symbol CC" error in VS. And no autocomplete is available after entering <CC:.
Is it possible to register such kind of tag prefix for standard controls?

jquery not working while using <form> tag

Jquery is not wrking when I am using form tag in asp.net controls.so whats the solution .I am trying multiple techniques but it does not work.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#bt1").click(function () {
$("#p1").hide(2000);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p runat="server" id="p1">this is asp.net</p>
<asp:Button ID="bt1" Text="click" runat="server"/>
</div>
</form>
</body>
</html>
As you are using ASP.NET and bt1 is a button server control you need to use Control.ClientID.
<%= bt1.ClientID %> will Gets the control ID for HTML markup that is generated by ASP.NET.
Use
$("#<%= bt1.ClientID %>").click(function () {
$("#<%= p1.ClientID %>").hide(2000);
});
OR
You can use ClientIDMode.Static mode then you can continue with your existing code. However I will not recommend it.

Moving javascript from user control into appropriate master page

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?

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

Error Creating Control vs2010 MasterPage

Someone can explain this error?
Error Creating Control - head
Object reference not set to an instance of an object.
<%# Page Title="" Language="C#" MasterPageFile="~/Controls/Master1.Master"
AutoEventWireup="true" CodeBehind="GrupoUsuario.aspx.cs" Inherits="GrupoUsuario" %>
<asp:Content ID="Content1" runat="server" contentplaceholderid="head">
</asp:Content>
I Think this is a bug of visual studio 2010 in design view. I'not using any event to manipulate session object in the method OnInt(). The "PlaceHolderTopo" is an placeholder in the web user control Topo.ascx. It's work normaly. I Don't have any code inside the content place holder in the page tha inherits from the master page and get this error.
Below is the code of the masterpage:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="PrincipalSeguranca.Master.cs" Inherits="PrincipalSeguranca" %>
<%# Register Src="Topo.ascx" TagName="Topo" TagPrefix="uc1" %>
<%# Register src="MenuAdmin.ascx" TagName="MenuAdmin" TagPrefix="uc2" %>
<%# Register src="Rodape.ascx" tagname="Rodape" tagprefix="uc3" %>
<!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>Sistema</title>
<script language="jscript" type="text/javascript" src="Scripts/Geral.js"></script>
<link rel="shortcut icon" href="../layout/ico/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="../layout/css/styles.css" type="text/css" />
<link href="../layout/css/menu_tabbed.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../layout/css/contents.css" type="text/css" />
</head>
<body>
<form id="form1" ClientInstanceName="form1" runat="server">
<uc1:Topo ID="Topo1" runat="server" />
<div id="corpo">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="rodape">
<uc3:Rodape ID="Rodape1" runat="server" />
</div>
</form>
</body>
</html>
You have a bug in your Rodape control at design time.
When you open your page in Design view, it creates an instance of your custom control in Visual Studio's process. Since your website is not actually running, the code in your control is probably accessing some static member which hasn't been initialized.
You should launch a second copy of Visual Studio, attach its debugger to the first copy, set Break on All Exceptions from the Debug menu, and find your bug.

Categories