简体   繁体   English

C#中的简单翻转图像

[英]Simple rollover image in c#

I'm trying to add a rollover image to a test DotNetNuke page I'm working on but can't seem to get the syntax right (totally new to c#). 我正在尝试将悬停图像添加到我正在处理的DotNetNuke测试页面中,但似乎语法不正确(对C#来说是全新的)。 This should be a simple one for anyone with any experience of it, I'm sure. 我敢肯定,对于任何有经验的人来说,这应该是一个简单的方法。

I have so far tried several different things most of which result in an object reference error. 到目前为止,我已经尝试了几种不同的方法,其中大多数方法都会导致对象引用错误。 This is the last thing I tried (note below in the code where I have placed this, the line simply went here as this is where other attributes are added, I imagine there is a more logical place to put the line?): 这是我尝试过的最后一件事(请注意,在下面的代码中,我将该行放置在此处,因为这是添加其他属性的地方,我想这里有一个更合乎逻辑的位置?):

loginLink.Attributes.Add(" onmouseover", "this.src=(http://localhost/portals/_default/skins/BSAVA/Images/Nav/log_in_link_h.png);");

The pages loads without an error but rollover does not work. 页面加载没有错误,但是过渡不起作用。 The link renders with this attribute: 链接使用以下属性呈现:

onmouseover="this.src=(http://localhost/portals/_default/skins/BSAVA/Images/Nav/log_in_link_h.png);"

Clearly not correct. 显然不正确。

The whole page code is as follow: 整个页面代码如下:

using System;
using System.Web;
using System.Web.UI;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Modules;

namespace DotNetNuke.UI.Skins.Controls
{

public partial class Login : SkinObjectBase
{

    private const string MyFileName = "Login.ascx";

    public string Text { get; set; }

    public string CssClass { get; set; }

    public string LogoffText { get; set; }

    public string login_link_img = "http://localhost/portals/_default/skins/BSAVA/Images/Nav/log_in_link.png";
    public string login_link_img_hover = "http://localhost/portals/_default/skins/BSAVA/Images/Nav/log_in_link_h.png";

    public string logout_link_img_hover = "http://localhost/portals/_default/skins/BSAVA/Images/Nav/logout_link_h.png";
    public string logout_link_img = "http://localhost/portals/_default/skins/BSAVA/Images/Nav/logout_link.png";

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        try
        {

            if (Request.IsAuthenticated)
            {

            loginLink.CssClass = "logoutLink"; 
            loginLink.ImageUrl = logout_link_img;

                if (!String.IsNullOrEmpty(LogoffText))
                {
                    if (LogoffText.IndexOf("src=") != -1)
                    {
                        LogoffText = LogoffText.Replace("src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);
                    }
                    loginLink.Text = LogoffText;
                }
                else
                {
                    loginLink.Text = Localization.GetString("Logout", Localization.GetResourceFile(this, MyFileName));
                }
                loginLink.NavigateUrl = Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Logoff");
            }
            else
            {

            loginLink.CssClass = "loginLink"; 
            loginLink.ImageUrl = login_link_img;


                if (!String.IsNullOrEmpty(Text))
                {
                    if (Text.IndexOf("src=") != -1)
                    {
                        Text = Text.Replace("src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);
                    }
                    loginLink.Text = Text;
                }
                else
                {
                    loginLink.Text = Localization.GetString("Login", Localization.GetResourceFile(this, MyFileName));
                }

                string returnUrl = HttpContext.Current.Request.RawUrl;
                if (returnUrl.IndexOf("?returnurl=") != -1)
                {
                    returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?returnurl="));
                }
                returnUrl = HttpUtility.UrlEncode(returnUrl);

                loginLink.NavigateUrl = Globals.LoginURL(returnUrl, (Request.QueryString["override"] != null));

                if (PortalSettings.EnablePopUps && PortalSettings.LoginTabId == Null.NullInteger)
                {
                    loginLink.Attributes.Add(" onclick", "return " + UrlUtils.PopUpUrl(loginLink.NavigateUrl, this, PortalSettings, true, false, 200, 550)); 
                    loginLink.Attributes.Add(" onmouseover", "this.src=(http://localhost/portals/_default/skins/BSAVA/Images/Nav/log_in_link_h.png);");

                }
            }

        }
        catch (Exception exc)
        {
            Exceptions.ProcessModuleLoadException(this, exc);
        }
    }

}
}

I've done plenty of searching around and several methods seem to crop up some using JavaScript others not. 我已经做了很多搜索,似乎有几种方法不是使用JavaScript而是使用JavaScript。 Any help on this is greatly appreciated, thanks. 非常感谢任何帮助,谢谢。

I use this one to achieve the RolloverImage effect on an Image button: 我使用此代码在Image按钮上实现RolloverImage效果:

namespace My.Controls
{
    /// <summary>
    /// Summary description for RolloverImageButton
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RolloverImageButton runat=server></{0}:RolloverImageButton>")]
    public class RolloverImageButton : ImageButton
    {
        [DefaultValue("")]
        [UrlProperty]
        [Bindable(true)]
        public virtual string ImageOverUrl
        {
            get
            {
                if (null == ViewState["ImageOverUrl"]) return string.Empty;
                else return Convert.ToString(ViewState["ImageOverUrl"]);
            }
            set { ViewState["ImageOverUrl"] = value; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute("onmouseover", "this.src='" + base.ResolveClientUrl(ImageOverUrl) + "'");
            writer.AddAttribute("onmouseout", "this.src='" + base.ResolveClientUrl(ImageUrl) + "'");
            base.AddAttributesToRender(writer);
        }
    }
}

You can use it in your markup as you would the ImageButton, but you can additionaly set the ImageOverUrl to it. 您可以像在ImageButton中一样在标记中使用它,但是也可以将ImageOverUrl设置为它。

To make the Control globaly available without including the namespace in in every page you can simply add a reference to your web.config like this: 要使控件全局可用而不在每个页面中都包含名称空间,您可以像这样简单地添加对web.config的引用:

<system.web>
  ...
  <pages theme="..." controlRenderingCompatibilityVersion="..." clientIDMode="...">
    <controls>
      <add tagPrefix="mycontrols" namespace="My.Controls" />
    </controls>
  </pages>
</system.web>

You can then use all controls defined in the My.Control namespace in your markup like this: 然后,您可以使用标记中My.Control命名空间中定义的所有控件,如下所示:

<mycontrols:RolloverImageButton runat="server" ImageUrl="~/Images/image1.png" ImageOverUrl="~/Images/image1_h.png" ... />

If you do not want it to be a Button control, simply override the Image class, not the ImageButton class and call it RolloverImage . 如果您不希望它成为Button控件,则只需重写Image类而不是ImageButton类,并将其称为RolloverImage You should than have an image tag you can display in an ordinary HyperLink. 您应该拥有一个可以在普通HyperLink中显示的图像标签。

Regards, Gerald 此致杰拉尔德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM