Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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
0
Soluga
Asked:
Soluga
  • 4
  • 3
  • 3
  • +2
3 Solutions
 
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
 
Tom BeckCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
AnuTijiCommented:
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 BeckCommented:
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now