ct100_ query

Hi I am using jquery extensively in my app, when I wish to target a control I use something like ...
$("#ctl00_MainContent_hdnEmailId").val();

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

Thanks
LVL 1
SolugaAsked:
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.

Or:

$('input[id$=hdnEmailId]').val();

Meaning an input whose id ends with "hdnEmailId".
0
 
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?
0
 
SolugaAuthor Commented:
Julian,

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.
0
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).

 
GaryCommented:
Are you using runat="server"
0
 
AnuTijiCommented:
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"
0
 
SolugaAuthor Commented:
Anu,

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

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() {
        alert($("#txtBox1").val());
    }
</script>
<asp:ScriptManager ID="sptMgr" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updPnl" runat="server">
<ContentTemplate>
text
<asp:TextBox ClientIDMode="Static" runat="server" ID="txtBox1"></asp:TextBox>
<asp:Button ID="btnSubmit" OnClientClick="Display()" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

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

Open in new window

0
 
SolugaAuthor Commented:
Anu,

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.
0
 
AnuTijiCommented:
Hi

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

http://connect.microsoft.com/VisualStudio/feedback/details/584991/clientidmode-static-in-updatepanel-fails-to-do-async-postback#
0
 
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:

$('#ct100_MainContent_ContentPlaceHolder1_updPnl_textBox1').val();

If it renders like this:

$('#').val();

Then you know you got it wrong and asp.net could not interpret correctly.
0
 
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

Thanks
0
 
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.
0
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.