如何在弹出窗口显示之前验证锚标记?

时间:2012-07-16 12:47:22

标签: asp.net thickbox

我有一个带有一个图像标签的锚标签,我根据按钮点击动态更改锚标签内的图像。 button1设置Red.png图像,button2设置Green.png图像。我想验证锚标签内的哪个图像,如果是Green.png,我需要显示厚箱,如果它是Red.png,则不应该进行任何操作。我怎么能这样做?

//我的aspx代码 - 带有图片标记的锚标记

<a onclick="validate();" href="PopUpPage.aspx?KeepThis=true&TB_iframe=true&height=150&width=400"
      class="thickbox" id="AnchorImage" >
<img id="ColorImageButton" src="SiteImages/Red.png" runat="server" />
</a>

//两个按钮 - 按钮1设置红色图像,按钮2设置绿色图像

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

    protected void Button1_Click(object sender, EventArgs e)
    {
        ColorImageButton.Src = "~/SiteImages/Red.png";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ColorImageButton.Src = "~/SiteImages/Green.png";
    }

更新:根据答案我添加了一个JavaScript来删除链接,但我仍然有厚盒的黑屏背景

function validate() {

    if (document.getElementById('<%=ColorImageButton.ClientID%>').src.indexOf('Red.png') >= 0) {
                         document.getElementById('AnchorImage').removeAttribute('href');
    }

1 个答案:

答案 0 :(得分:1)

使用Javascript,您可以执行以下操作:

  1. 将onclick处理程序附加到锚点,如下所示:

    <a onclick="validate();" href="PopUpPage.aspx?KeepThis=true&TB_iframe=true&height=150&width=400"
      class="thickbox" id="AnchorImage"  >
    <img id="ColorImageButton" src="SiteImages/Red.png" runat="server" />
    </a>
    
  2. 定义validate函数:

    ​function validate()
    {
       //If image is red; change the href value to a hash sign
       //so it doesn't do anything
       if(document.getElementById('<%=ColorImageButton.ClientID%>').src.indexOf('Red.png')>=0)
       {
          document.getElementById('AnchorImage').href='#';
       }
    }​