1

I'm creating some textboxes and labels dynamically and am trying to hide/show them via JQuery but can't get the JQuery working. What am I doing wrong?

Here's the code behind:

TableCell td4 = new TableCell();

Label l2 = new Label();

l2.ID = "lbSell" + dp.dSellAutoID.ToString();
l2.Text = Math.Round(Convert.ToDecimal(dp.dSellPrice), 2).ToString();
l2.Visible = false;
td4.Controls.Add(l2);

TextBox tb1 = new TextBox();

tb1.ID = "tbSell" + dp.dSellAutoID.ToString();
tb1.Width = 50;
tb1.Text = Math.Round(Convert.ToDecimal(dp.dSellPrice), 2).ToString();
td4.Controls.Add(tb1);
tr.Cells.Add(td4);

And here's the JS:

function editRow(rowID) {
    //alert(rowID);
    $('#' + 'lbSell' + rowID).show();
    $('#' + 'tbSell' + rowID).hide();
}
Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40
Milnelli
  • 55
  • 7

1 Answers1

1

Are you using master pages? In that case, the ID changes while the page is being rendered. To prevent it, you can add ClientIDMode="Static" to the page directive

<%@ Page Title="" Language="C#" ClientIDMode="Static" MasterPageFile="~/epinet.master" %>

Please see: https://stackoverflow.com/a/5494142/5746368

jim1427
  • 140
  • 2
  • 9
  • Yeah, using master pages.. are there any drawbacks in using the ClientIDMode="Static" approach? Or can I count on having 'MAIN' prefixed to controls sitting under the masterpage structure? – Milnelli Aug 28 '17 at 06:16
  • I don't find any drawback in setting ClientIDMode="static". I prefer doing it so that others can understand/debug my Javascript/jQuery without getting confused. – jim1427 Aug 28 '17 at 06:53
  • You may also see https://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx for more details on ClientIDMode Property – jim1427 Aug 28 '17 at 06:58