English 中文(简体)
C# 中的简单滚滚翻图像
原标题:Simple rollover image in c#

我试图在测试的 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其他方法来捕捉一些其他方法,但没有任何帮助,非常感谢,谢谢。

最佳回答

我用这个来达到图像按钮上的翻滚图像效果:

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

您可以像图像按钮一样在标记中使用它, 但是您也可以为它设置图像OverUrl 。

将控件的全局化, 而不在每一页中包括命名空间, 您可以简单地在您的 Web 上添加引用。 配置像这样 :

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

然后您可以使用 My 中定义的所有控件。 在您的标记中控制命名空间 :

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

如果您不希望它是按键控制, 请简单地覆盖图像类, 而不是 < code> Imaage button 类, 并称之为 < code> Rolloverimage 。 您不应该有您可以在普通 HyperLink 中显示的图像标签 。

Regards, Gerald

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签