c# - Dynamically change style of panels through javascript -


After clicking on a add button, I am trying to display a new panel, for some reason this work (It refreshes the page too, even if a button is right in my button) I have tried in different ways, but something seems to be doing the same thing.

  & lt; Asp: button id = "btnAdd" runat = "server" text = "add property" CssClass = "btn" OnClientClick = "addProperty (); return back;" / & Gt; & Lt; Script type = "text / javascript" & gt; Var num = 1; Function addProperty () {num ++; Var Panel Name = "& lt;% = pnlProperty" + num.toString () + ".clientID%>"; Warning (panelName); Document.getElementById (panelName) .style.display = "inline"; } & Lt; / Script & gt;  

Anyone will be helpful!

You can use JavaScript and ASP.NET such as ASP.NET Always being sent to the client, where Javascript is executed. You can use generate javascript in the net

In your case, you will execute some more before this.

  & lt;% = pnlProperty "+ Num.toString () +" .clientID%>  

This will not be executed in the loop, because it is generated before the ASP.NET code loop and sent to the customer. No panel of JavaScript variables num can be used because Javascript is not started to execute. Everything within & lt;% , ... %> will be interpreted as a .NET code. Therefore, the .NET bus will throw an error because the above code is not the correct line.

You Yourself NET code should be written so that it can generate meaningful JavaScript.

You can do something like this:

  on the panels = ["from  & gt; "& Lt;% = pnlProperty1.ClientID%>" "& Lt;% = PnlProperty2.ClientID%> & gt;"];  

... javascript that looks like

may occur
  panels = ["ctl00_main_pnlProperty0", "ctl00_main_pnlProperty1", "ctl00_main_pnlProperty2"];  

... which is a completely accurate way of creating an array of strings that you can then use in your JavaScript.

  document.getElementById (Panel [number]) style.display = "inline".  

If you want a dynamic loop, you must

  var panel = "& lt;% String.Join (", ", Page.Controls .OfType & lt; Cells & gt; () .Where (panel = & gt; panel.ID.StartsWith ("pnlProperty")) .Select (panel = & gt; panel.ClientID) .toArray ());% & Gt; "; Can create something like  

... > panels on

  = "ctl00_main_pnlProperty0, ctl00_main_pnlProperty1, ctl00_main_pnlProperty2";  

You can do that again:

  var panelname = panels. Split (','); Document.getElementById (panelNames [NUM]) style.display = "inline".  

Comments