views:

1030

answers:

3

Hello

I have a button inside an ascx inside an update panel inside aspx content page. When the button is clicked i want it to run a JS function that causes to show a panel. Here is my Code.

    <pre>


    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ABC.ascx.cs" Inherits="App.ABC" %>
    <script type= "text/javascript" language="javascript">
    var val1=0;
    var val2=0;


    function ShowPanel(val2)
    {
        if(val2 != 0)
        {   
            switch(val2)
            {
            case 1 : 
                document.getElementById('<%=pnl1.ClientID%>').style.visibility = 'visible';
            break;           
            }
        }
        return false;
    }
    </script>


    <asp:LinkButton ID="lbl1" runat="server" OnClick="return ShowPanel(1);">count</asp:LinkButton>

I am not sue how to do this. Please help

Update #1 - ABC.ascx is in updatepanel in the aspx page XYZ.aspx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ABC.ascx.cs" Inherits="App.ABC" %>
    <script type= "text/javascript" language="javascript">
    var val1=0;
    var val2=0;

    function ShowPanel(val2) {
            if (val2 != 0) {
                switch (val2) {
                    case 1:
                        document.getElementById("<%= this.pnl1.ClientID%>").style.display = "none";
                        break;
                }
            }
            return false;
        }
    </script>
    <div>

    <div style="text-align:center">

    </div>

    <table style="width:100%; text-align:center; border-color:#99CCFF" border="3">
      <tr style="text-align:left">
      <td><asp:LinkButton ID="lbl1" runat="server" OnClientClick="return ShowPanel(1);">count</asp:LinkButton>
      </td>
      <td style="text-align:right"><asp:Button ID="btnHide1" runat="server" Text="hide" 
              Height="18px" Width="32px"/>
      </td>
      </tr>
      <tr>
      <td colspan="2"><asp:Panel ID="pnl1" runat="server" Visible="false"> </asp:Panel>
        </td>
      </tr>
    </table>

    </div>
A: 
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>

<script type= "text/javascript" language="javascript">
    var val1 = 0;
    var val2 = 0;


    function ShowPanel(val2) {
        if (val2 != 0) {
            switch (val2) {
                case 1:
                    document.getElementById("<%= this.pnl1.ClientID%>").style.display = "none";
                    break;
            }
        }
        return false;
    }
    </script>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel runat="server" ID="pnl1"><div>pepe</div></asp:Panel>
    <asp:LinkButton ID="lbl1" runat="server" OnClientClick="return ShowPanel(1);">count</asp:LinkButton>
    </div>
    </form>
</body>
</html>
andres descalzo
I tried that but it threw me an error when I was dynamically loading the user control.ABC.ascx(51): error CS1041: Identifier expected; 'return' is a keyword
there I put the completed example
andres descalzo
sorry , this line generate a error "<%=this.pepe %>"
andres descalzo
Updated code : Thanks for helping me out, I tried your code, and it threw me, "Microsoft JScript runtime error: Object expected" on click the count linkbutton - lbl1.
pleace, write the complete code here, with this example
andres descalzo
placed the complete code with your suggested changes
haaaaaa, 'Visible="false"', you put this property in the asp:Panel.jeje, then not generates in the HTML code. Always look at the HTML generated
andres descalzo
That is what i am trying to achieve. Hide panel at the first go and then show panel at the click on Count.
ASP.NET does not write HTML code in the panel with this property, you should do an OnClick and code in C#/VB.NET for see the panel, but not by JS, as it does not exist in HTML.
andres descalzo
I did this Easily using this ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), "", "showwait();");ScriptManager.RegisterClientScriptBlock(this,this.GetType(), "", "stopwait();", true);
A: 

In your example, you're using LinkButton.OnClick, which is a server-side event, so it won't fire the client-side JavaScript. If you change OnClick to OnClientClick, it should cause ASP.NET to spit out a client-side click handler for the LinkButton (which will probably be rendered as an onclick="return ShowPanel(1);" attribute on the anchor tag.)

Also, I noticed that in your ShowPanel() function, you use pnl1.ClientID. I can't tell from your code snippet whether that Panel is in the host page (ASPX) or the user control (ASCX) but if it's not in the user control, you'll need to establish some sort of reference back to it (i.e. DirectCast(Me.Page, MyHostPageWithPanel).pnl1). Or you might decide that it doesn't really need to be a server-side control, and then you could just hardcode a div with an id that is easily grabbed through JavaScript.

Jordan, i changed to onclientclick. Panel is in the ABC.ascx. ABC.ascx is dynamically render by updpnl1 which is in XYZ.aspx.
A: 

did this Easily using this

ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), "", "showwait();");

ScriptManager.RegisterClientScriptBlock(this,this.GetType(), "", "stopwait();", true);