Pergunta

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#). 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. Any help on this is greatly appreciated, thanks.

Foi útil?

Solução

I use this one to achieve the RolloverImage effect on an Image button:

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.

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:

<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:

<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. You should than have an image tag you can display in an ordinary HyperLink.

Regards, Gerald

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top