0

我试图在用户单击后更改超链接的文本。这是超链接:

   <asp:hyperlink id="OpenClose" runat="server" onclick="OpenClose_Click" AutoPostBack="true">Close</asp:hyperlink> 

这是我背后的代码:

     protected void Page_Load(object sender, EventArgs e)
{
    OpenClose.Attributes.Add("onclick", "OpenClose_Click");
}
protected void OpenClose_Click(object sender, EventArgs e)
{
    if (OpenClose.Text == "Close")
        OpenClose.Text = "Open";
    else
        OpenClose.Text = "Close";
}

问题是它似乎没有看到函数 OpenClose_Click。我不确定为什么。有没有其他方法可以做到这一点,或者我错过了什么?

编辑

这是整个aspx代码

   <%@ Page Title="" Language="C#" MasterPageFile="../MasterPageLite.master"                  AutoEventWireup="true" CodeFile="testPageLoad2.aspx.cs" Inherits="BuilderPages_testPageLoad2" %>

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="left_side">
<form id="form1" runat="server">

 This is the second test page I am making. Practice collapse and expand panels!

<div class="msg_list">
<h3 class="msg_head">Header-1</h3>
<div class="msg_body">
Collapse this panel!!
<asp:button runat="server" text="Can you see me?" />
 </div>
<h3 class="msg_head">Header-2</h3>
<div class="msg_body">
Congratulations you opened the panel!!
</div>
<h3 class="msg_head">Header-3</h3>
<div class="msg_body">
The third panel has been opened!!
</div>
</div>

</form>
</div>

<div class="right_side">
<div class="lBorder">
<asp:Panel ID="OpenClosePanel" runat="server"></asp:Panel>
<asp:HyperLink id="OpenClose" runat="server" AutoPostBack="true" style="cursor:pointer; text-decoration:underline;">Show/Hide</asp:HyperLink>

 </div>

 <div class="rscontent">
 <p>
 Lorem ipsum... 
 </p>
 <p>
 Nulla... 
 </p>
 <p>
 Vivamus... 
 </p>
 <p>
 Phasellus... 
 </p>
 <p>
 Aenean... 
 </p>
 </div>

 </div>
 </asp:Content>
4

2 回答 2

2

您应该使用 LinkBut​​ton 而不是 HyperLink 控件,如下所示:

标记

<asp:LinkButton id="OpenClose" 
                runat="server" 
                OnClick="OpenClose_Click" 
                AutoPostBack="true" 
                Text="Close"></asp:LinkButton>

代码隐藏

protected void OpenClose_Click(object sender, EventArgs e)
{
    if (OpenClose.Text == "Close")
    {
        OpenClose.Text = "Open";
    }
    else
    {
        OpenClose.Text = "Close";
    }
}

该类LinkButton派生自Button该类,因此它具有与按钮类似的事件,这是您想要的效果,但它呈现为超链接。

于 2013-06-24T21:07:43.343 回答
0

<asp:hyperlink ...不是有效的控件类型,因为 .NET 区分大小写。尝试将其更改为:

<asp:HyperLink ...

我还将摆脱页面加载事件中的代码。

于 2013-06-24T21:07:18.863 回答