c#中的简单翻转图像

时间:2012-05-25 09:24:07

标签: c# html rollover-effect

我正在尝试将翻转图像添加到我正在处理的测试DotNetNuke页面,但似乎无法使语法正确(对于c#来说是全新的)。对于任何有经验的人来说,这应该是一个简单的,我敢肯定。

到目前为止,我尝试了几种不同的东西,其中大多数会导致对象引用错误。这是我尝试过的最后一件事(请注意下面的代码,我放置了这个,这条线只是在这里,因为这是添加其他属性的地方,我想有一个更合理的地方放线?):

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

页面加载时没有错误,但翻转不起作用。该链接使用以下属性进行渲染:

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

显然不正确。

整个页面代码如下:

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);
        }
    }

}
}

我已经做了很多搜索,有些方法似乎突然出现了一些使用其他JavaScript的方法。非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我使用这个来实现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);
        }
    }
}

您可以像在ImageButton中一样在标记中使用它,但您可以另外将ImageOverUrl设置为它。

要使Control全局可用而不在每个页面中包含命名空间,您只需添加对web.config的引用,如下所示:

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

然后,您可以在标记中使用My.Control命名空间中定义的所有控件,如下所示:

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

如果您不希望它成为Button控件,只需覆盖Image类,而不是ImageButton类,并将其命名为RolloverImage。您应该拥有一个可以在普通HyperLink中显示的图像标记。

此致 杰拉德