如何在我的asp页面中的'OnClick'事件中传递脚本中的参数?(How can pass a parameter in script on 'OnClick'

编程入门 行业动态 更新时间:2024-10-11 23:20:28
如何在我的asp页面中的'OnClick'事件中传递脚本中的参数?(How can pass a parameter in script on 'OnClick' event in my asp page?)

我需要在我的脚本子中传递一个参数,我在asp页面上尝试这个:

<asp:LinkButton ID="LinSoftWares" OnClick="ProductChange(product)" runat="server" >Softwares</asp:LinkButton>

这个脚本:

    <script runat="server">
        Sub ProductChange(ByVal StrCommand As String)
            If StrCommand = "product" Then
                If Session("BlnProductShow") = 1 Then
                    Session("BlnProductShow") = 0
                Else
                    Session("BlnProductShow") = 1
                End If
                DoRefresh()
            End If

        End Sub

        Sub DoRefresh()
            If Session("BlnProductShow") = 1 Then
                DivProduct.Style.Value = "visibility: visible"
            Else
                DivProduct.Style.Value = "visibility: hidden"
            End If

        End Sub

    </script> 
  
 

但我得到了这个错误:

'产品'未申报。 由于其保护级别,它可能无法访问

I need to pass a parameter in my script sub, i try this on asp page :

<asp:LinkButton ID="LinSoftWares" OnClick="ProductChange(product)" runat="server" >Softwares</asp:LinkButton>

and this for script :

    <script runat="server">
        Sub ProductChange(ByVal StrCommand As String)
            If StrCommand = "product" Then
                If Session("BlnProductShow") = 1 Then
                    Session("BlnProductShow") = 0
                Else
                    Session("BlnProductShow") = 1
                End If
                DoRefresh()
            End If

        End Sub

        Sub DoRefresh()
            If Session("BlnProductShow") = 1 Then
                DivProduct.Style.Value = "visibility: visible"
            Else
                DivProduct.Style.Value = "visibility: hidden"
            End If

        End Sub

    </script> 
  
 

But i got this error :

'product' is not declared. It may be inaccessible due to its protection level

最满意答案

我在VB.Net没有太多经验,但是在C#我会按照以下方式进行操作,添加CommandArgument属性并为其分配默认值,然后在其单击中我将得到该值。

ASPX:

<asp:LinkButton ID="LinSoftWares" runat="server" CommandArgument="Product" OnClick="LinSoftWares_Click" > Softwares </asp:LinkButton>

C#

protected void LinSoftWares_Click(object sender, EventArgs e) { LinkButton lnkSender= (LinkButton)(sender); string value = lnkSender.CommandArgument; // do what you need here }

编辑:

如果您的参数“product”是动态的,那么如果您的控件在某些数据绑定控件中,则可以使用Eval("Product") 。

I don't have much experience in VB.Net but in C# I would do in following way, add CommandArgument property and assign it with a default value and then in its on click I would get the value.

Aspx:

<asp:LinkButton ID="LinSoftWares" runat="server" CommandArgument="Product" OnClick="LinSoftWares_Click" > Softwares </asp:LinkButton>

C#

protected void LinSoftWares_Click(object sender, EventArgs e) { LinkButton lnkSender= (LinkButton)(sender); string value = lnkSender.CommandArgument; // do what you need here }

Edit:

In case your argument "product" is dynamic then you can use Eval("Product") if your control is in some data binder control.

更多推荐

asp,Sub,If,product,script,电脑培训,计算机培训,IT培训"/> <meta name="des

本文发布于:2023-07-31 08:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   参数   页面   如何在   事件中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!