0

I have a class derived from WebControls.TableCell. When the Text property is set, I call a method that dynamically adds asp:Panels and asp:LiteralControls to the Cell. I want to reference these controls in Javascript, so naturally I tried using the ClientId of the panels in my JS functions. However, these controls have no ClientId set (the string is empty). Why is this? How do I force the ClientIds to be set?

As a temporary solution, I set the ClientIDMode to "static" and created the IDs on my own, but this is not satisfactory because it's hard to reference those IDs in JS. Why? If you assign, for example, "12345" to one control, it gets changed on client side to something like "MainContent_123456". This is bad because the "MainContent" part is not fixed; thus I never know for sure what the real Id on the client side will be. Currently, I can get the control with jQuery using $ctrl = $('[id$='12345']');, but this is dirty because it would get any control that has '123456' in its id.

So, back to the original question: how do I get my ClientIds set automatically for my panels in my custom TableCells?

Edit: Code added

 protected void Page_Load(object sender, EventArgs e)
    {
        this.ClientIDMode = System.Web.UI.ClientIDMode.Static;
    }

Code in the method that adds the controls to the custom TableCell:

        Panel remainingTextPanel = new Panel();
        remainingTextPanel.ID = Guid.NewGuid().ToString();
        remainingTextPanel.Style["display"] = "none";
        LiteralControl remainingText = new LiteralControl(myText.Substring(initialStringLength, myText.Length - initialStringLength));
        remainingTextPanel.Controls.Add(remainingText);
        this.Controls.Add(remainingTextPanel);

 Panel linkBtnPanel = new Panel(); 
        LinkButton lnkBtn = new LinkButton() {Text = "...", OnClientClick = "toggleDynamicText('" + remainingTextPanel.ID + "'); return false;" };
        lnkBtn.Font.Bold = true;
        linkBtnPanel.Controls.Add(lnkBtn);
        this.Controls.Add(linkBtnPanel);

And the JS Code:

function toggleDynamicText(id) {
$ctrl = $('[id$=' + id + ']');
$(document).ready(function () { 
    $ctrl.toggle(1000);
});

}

Cleo
  • 620
  • 2
  • 10
  • 32
  • When ClientIDMode is static it will NOT add "MainContent_" to your IDs. That's why it's called static, because it doesn't change the ID you assign it. – Rick S May 15 '14 at 18:43
  • I think it does because that's what Firebug displays on client side. I set the ClientIDMode to static in Page_Load and then manually set the "ID" properties of my controls to a random string. And on client side, the "MainContent" part is added, for what reason ever. – Cleo May 15 '14 at 19:39
  • What version of .NET are you using? – Rick S May 15 '14 at 20:00
  • The Version is .NET 4.5. I think the most important question is why my custom table cells (and the controls added to them) don't get any ClientIds assigned when they are dynamically added to a page. – Cleo May 15 '14 at 20:07

1 Answers1

0

Without seeing any code it's difficult to say what's going on but to access your controls using jQuery you can do the following:

$("#<%=myElement.ClientID%>")

This way it doesn't matter what .NET assigns as the ID.

Rick S
  • 6,476
  • 5
  • 29
  • 43
  • The whole point of this thread is that the "myElement.ClientID" is not set (as it should be)... – Cleo May 15 '14 at 20:23
  • The ID property of the controls will not be automatically set, you have to set it. Once you do that, then the ClientID property should also be set. – Rick S May 15 '14 at 20:26
  • Maybe if you showed a few lines of your code then i might be able to help you better. Add them to your original post. – Rick S May 15 '14 at 20:45
  • Done. I haven't tried yet if the ClientId is set after I set the ID field; this might be the problem after all. – Cleo May 15 '14 at 20:59
  • I have tried this now, the problem still exists. Even if I set the ClientIDMode to the default setting (AutoID) and assign values to the "ID" property of a control, the ClientID still is empty. On client side, the ids look something like "MainContent_". – Cleo May 16 '14 at 13:22
  • These two might help: [Question1](http://stackoverflow.com/questions/2359135/asp-net-client-ids-not-being-generated-for-loaded-control) [Question2](http://stackoverflow.com/questions/16481182/wrong-clientid-for-dynamically-added-child-control) – Rick S May 16 '14 at 14:17