ct100_ query

Hi I am using jquery extensively in my app, when I wish to target a control I use something like ...

But when I run the app on the server the code id has dropped the ct100_ part, but when I run it on my development machine it runs fine.

Would be grateful for any advice

Who is Participating?
Tom BeckConnect With a Mentor Commented:
Maybe inline server code would work here:

$("#<%= hdnEmailId.ClientID %>").val();

That way it will be valid regardless of how it's rendered.



Meaning an input whose id ends with "hdnEmailId".
Julian HansenCommented:
But when I run the app on the server the code id has dropped the ct100_ part, but when I run it on my development machine it runs fine
Dissappears from where? The element id or the javascript code?
What is rendered if the ct100 is left off?
How is the ID being generated?
SolugaAuthor Commented:

Yes when the control is rendered the ct100 is left off, but only when it is on the server, when I run the app on my dev machine it is not removed.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Are you using runat="server"
If you set ClientIDMode="Static" for the controls, the id will be same if you inherit the master page or not. Sample code pasted below:

<asp:TextBox ClientIDMode="Static" runat="server" ID="txtBox1"></asp:TextBox>

In this case, the id of the textbox will be always "txtBox1"
SolugaAuthor Commented:

I have tried that but it throws an error when using update panels!
AnuTijiConnect With a Mentor Commented:

I have tried the above solution and it didnot give any error. Please find the code below:

Content Page

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script language="javascript" type="text/javascript">
    function Display() {
<asp:ScriptManager ID="sptMgr" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updPnl" runat="server">
<asp:TextBox ClientIDMode="Static" runat="server" ID="txtBox1"></asp:TextBox>
<asp:Button ID="btnSubmit" OnClientClick="Display()" runat="server" />

Open in new window

Master Page
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication2.Site1" %>

<!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">
    <script src="jquery-1.9.0.min.js" language="javascript"></script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

Open in new window

SolugaAuthor Commented:

I find if I try a partial postback such as.......

 var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm._doPostBack('ctl00_MainContent_pagePanel_updPage', '');

then it will throw an error saying there is already a control with the id ..... on the page.

Can you try using ClientIDMode="Static" to update panel as well? Please find the link below

Tom BeckConnect With a Mentor Commented:
It's pointless to try to predict how asp.net will name a control id on the rendered page. It will vary depending on the .net framework version. The ClientID method was created to overcome this problem when trying to access a rendered control with client side scripting.

To get the value of an asp.net textbox called txtBox1 that's inside an update panel called updPnl on the client side using jquery you could try something like this:

$('#<%= updPnl.FindControl("txtBox1").ClientID %>').val();

If that doesn't work because the update panel is inside a content placeholder you may have to do this:

$('#<%= ContentPlaceHolder1.FindControl("updPnl").FindControl("txtBox1").ClientID %>').val();

If you include such a line in a script block on the content page then look at how that line is interpreted in the view source of the rendered page you will get and idea if you have it right or not and you can adjust. For example, the above line may render as something like:


If it renders like this:


Then you know you got it wrong and asp.net could not interpret correctly.
SolugaAuthor Commented:
This is a bit like alchemy trying to predict the name of the control and how it will behave.

I think the easiest solution for me would to just take a copy of the app before it is published and just do a find and replace on all objects in the WebUi layer and remove ct100_ before publishing.

Appreciate all the help

Tom BeckCommented:
The "ct100" prefix is added to the ids by asp.net when it builds the page for each and every request so I'm not understanding what you mean by doing a find and replace on the ct100. "ct100" does not exist in the published files, only in the markup returned to the client for each request. If you mean to do the find and replace with client side scripting on page load, then be advised. That may stop working if the .net framework or the IIS version is upgraded.

Thanks for the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.