Javascript Unable to GetElementById in ASP.NET C# - 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

Related

AjaxFileUpload & asyncfileupload both causing full page postback

I have googled and have search this forum, i can not find solution to my problem. As stated by the title in my webforms project using vs 2015 both these mentioned controls are causing a full page post back after uploading is done. In my other projects this works fine. But in this particular one i cant figure it out. Is there just a common thing I am over looking, I have created fresh pages with out using the master-page I still get the same results - full post back. I did not post my code because i am using the controls as any other programmer would.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pic1.aspx.cs"
Inherits="SSWebAppV1._1.pic1" %>
<%# Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
Width="500px" AllowedFileTypes="jpg,jpeg,png" MaximumNumberOfFiles="4"
OnUploadComplete="AjaxFileUpload1_UploadComplete" />
</div>
</form>
</body>
</html>
****code-behind***
protected void AjaxFileUpload1_UploadComplete(object sender,
AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string fileName = Path.GetFileName(e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath("~/Eimages/" + fileName));
}

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?

Display simple c# in "sharepoint online"

I am using "SharePoint Online" and I'm new to it. I have descend programming background and I need more Info on customisation of this service. I've never worked with .NET before (WHAT?!), but I would really like to learn more about C# and it's use in the industry. I have full access to SharePoint Designer and I have Visual Studio 2008.
1) How can I implement simple c# code on SharePoint web page?
2) How to import and use c# classes on SharePoint page?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%# Page Language="C#" %>
<%# Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 4</title>
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<SharePoint:CssRegistration Name="default" runat="server"/>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
This is a brand new, empty .ASPX page generated by "SharePoint Designer". How can I import and display simple "Hello World", c# code on it?
Thank you very much in advance
Inside your <form> tag, insert this line:
<form id="form1" runat="server">
<% Response.Write("Hello World"); %>
</form>
Have fun learning C#
You cannot run server-side scripts on your ASP.NET pages in SharePoint online. It is not allowed and will tell you as much when you try to do this.
If you had an on-premises site, you could modify a setting in web.config to enable this, but that is beside the point.

Retrieving page title from master page

To manage the page title on my pages, I have a master page with a ContentPlaceHolder in the header.
<head runat="server">
<asp:ContentPlaceHolder runat="server" ID="headContent">
</asp:ContentPlaceHolder>
</head>
On each of my pages, I add the meta tags and page title as follow:
<asp:content id="Header" contentplaceholderid="headContent" runat="server">
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="page description" />
<%Page.Title = "My page title";%>
</asp:content>
I cannot modify the code on the pages by placing the Page.Title in the OnInit method of the page.
I need access to the page title in the codebehind of the master page, but I always get an empty title when I use Page.Title.
By using <%Page.Title = "My page title";%> you implicitly tell the ASP.NET to execute this embedded code block during the page's render phase.
What does it mean? You won't be able to get this value before the page's render phase. Assuming you're trying to get this value a little bit earlier than during the render. That's why you get the empty string.
The workaround could be in setting your Title property of the <%# Page directive in the beginning of your page e.g.:
<%# Page Title="My Title Goes Here" Language="C#" ... %>
By setting this you'll be able to access the Page.Title property from your master page a little bit earlier than the page's rendering occurs.
Just use
<title>My page title</title>

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