Solved

ct100_ query

Posted on 2013-06-28
12
339 Views
Last Modified: 2013-07-02
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
0
Comment
Question by:Soluga
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39284794
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
 
LVL 1

Author Comment

by:Soluga
ID: 39284856
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
 
LVL 38

Accepted Solution

by:
Tom Beck earned 334 total points
ID: 39285114
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
 
LVL 58

Expert Comment

by:Gary
ID: 39286041
Are you using runat="server"
0
 
LVL 4

Expert Comment

by:AnuTiji
ID: 39289727
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
 
LVL 1

Author Comment

by:Soluga
ID: 39290202
Anu,

I have tried that but it throws an error when using update panels!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Assisted Solution

by:AnuTiji
AnuTiji earned 166 total points
ID: 39292468
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
 
LVL 1

Author Comment

by:Soluga
ID: 39292513
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
 
LVL 4

Expert Comment

by:AnuTiji
ID: 39292742
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
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 334 total points
ID: 39292750
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
 
LVL 1

Author Comment

by:Soluga
ID: 39293765
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39293967
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now