开发自定义图片按钮,实现类似ASP提交

类别:.NET开发 点击:0 评论:0 推荐:

        在开发时如果要使用多个图片按钮时,我们经常必须为每个按钮制作一个图片(如:新增,修改,删除
等,其实只是文字不一样而已,我们却必须制作3个图片,一句话麻烦),如果能开发一图片按钮控件,
实现只须更改文字就可以生成按钮,那就好了。以下是实现方式(采用将图片分割成三部分进行实现)
/// <summary>
/// 图片按钮控件
/// </summary>
public class Buttoner : Button
{
    //
    // 常量
    //
    private const string HTML1 = "<TABLE ID=\"";
    private const string HTML3 = "\" HEIGHT=\"";
    private const string HTML4 = "\" BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\"";
    private const string HTML5 = " STYLE=\"cursor:hand\" onclick=\"javascript:";
    private const string HTML6 = "\"";
    private const string HTML7 = ">\n\t<TR>";
    private const string HTML8 = "\n\t\t<TD Valign=\"middle\" Align=\"center\" WIDTH=\"";
    private const string HTML9 = "\" background=\"";
    private const string HTML10 = "\">";
    private const string HTML11 = "</TD>";
    private const string HTML12 = "\n\t</TR>\n</TABLE>";
    private const string HTML13 = " disabled=\"true\"";
    private const string HTML14 = " TabIndex=\"";
    private const string HTML15 = " onkeydown=\"javascript:";
    private const string HTML16 = " style=\"padding-top:";

    private const string KEY_FORM_SUBMIT_JS = "FormSubmitJs";
    //
    // 变量
    //
    private string leftImagePath;
    private string middleImagePath;
    private string rightImagePath;

    private Unit length;
    private Unit height = 20;
    private Unit width = 6;
    private Unit topPadWidth = 3;

    private string action = null;
    private string target = null;

    private bool defaultButton = false;

    /// <summary>
    /// 构造函数
    /// </summary>
    public Buttoner()
    {
        leftImagePath = ApplicationUrl.GetButtonImagePath("btnLeft");
        middleImagePath = ApplicationUrl.GetButtonImagePath("btnMiddle");
        rightImagePath = ApplicationUrl.GetButtonImagePath("btnRight");
    }

    /// <summary>
    /// 左图片路径
    /// </summary>
    public string LeftImagePath
    {
        get
        {
            return leftImagePath;
        }
        set
        {
            leftImagePath = value;
        }
    }

    /// <summary>
    /// 中间图片路径
    /// </summary>
    public string MiddleImagePath
    {
        get
        {
            return middleImagePath;
        }
        set
        {
            middleImagePath = value;
        }
    }

    /// <summary>
    /// 右图片路径
    /// </summary>
    public string RightImagePath
    {
        get
        {
            return rightImagePath;
        }
        set
        {
            rightImagePath = value;
        }
    }

    /// <summary>
    /// 中间图片长度
    /// </summary>
    public Unit Length
    {
        get
        {
            return length;
        }
        set
        {
            length = value;
        }
    }

    /// <summary>
    /// 图片高度
    /// </summary>
    public override Unit Height
    {
        get
        {
            return height;
        }
        set
        {
            height = value;
        }
    }

    /// <summary>
    /// 图片宽度
    /// </summary>
    public override Unit Width
    {
        get
        {
            return width;
        }
        set
        {
            width = value;
        }
    }

    /// <summary>
    /// 图片距顶部宽度
    /// </summary>
    public Unit TopPadWidth
    {
        get
        {
            return topPadWidth;
        }
        set
        {
            topPadWidth = value;
        }
    }

    /// <summary>
    /// 提交的页面
    /// </summary>
    public string Action
    {
        get
        {
            return action;
        }
        set
        {
            action = value;
        }
    }

    /// <summary>
    /// Action的目标框架
    /// </summary>
    public string Target
    {
        get
        {
            return target;
        }
        set
        {
            target = value;
        }
    }

    /// <summary>
    /// 是否默认按钮
    /// </summary>
    public bool DefaultButton
    {
        get
        {
            return defaultButton;
        }
        set
        {
            defaultButton = value;
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        if(this.Visible && !Page.IsClientScriptBlockRegistered(ApplicationKey.FORM_SUBMIT_JS))
        {
            string src = ApplicationUrl.GetJsPath("FormSubmit");
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("<script language=\"javascript\" src=\"");
            strBuilder.Append(src);
            strBuilder.Append("\"></script>");

            if(defaultButton)
            {
                strBuilder.Append("\n<script language=\"javascript\" id=\"ButtonEnterSubmit\" for=\"document\" event=\"onkeydown\">\n\t");
                strBuilder.Append("if(window.event.keyCode == 13)\n\t\t");
                strBuilder.Append("{\n\t\t\t");
                strBuilder.Append("document.getElementById('");
                strBuilder.Append(this.ClientID);
                strBuilder.Append("').focus();\n\t\t\t");

                if(this.Enabled)
                {
                    if(this.CausesValidation)
                        strBuilder.Append("if(typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) ");

                    if(action == null || action == string.Empty)
                        strBuilder.Append(Page.GetPostBackEventReference(this));
                    else
                    {
                        strBuilder.Append("Submit('");
                        strBuilder.Append(action);
                        strBuilder.Append("', '");
                        strBuilder.Append(target);
                        strBuilder.Append("');");
                    }
                }

                strBuilder.Append("\n\t\t}");
                strBuilder.Append("\n</script>");
            }

            Page.RegisterClientScriptBlock(ApplicationKey.FORM_SUBMIT_JS, strBuilder.ToString());
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if(this.Visible)
        {
            writer.Write(HTML1);
            writer.Write(this.ClientID);
            writer.Write(HTML3);
            writer.Write(height);
            writer.Write(HTML4);
            writer.Write(HTML14);
            writer.Write(this.TabIndex);
            writer.Write(HTML6);

            if(this.Enabled)
            {
                // onclick提交
                writer.Write(HTML5);
                writer.Write("{ document.getElementById('");
                writer.Write(this.ClientID);
                writer.Write("').focus(); ");

                if(this.CausesValidation)
                    writer.Write("if(typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) ");

                if(action == null || action == string.Empty)
                    writer.Write(Page.GetPostBackEventReference(this));
                else
                {
                    writer.Write("Submit('");
                    writer.Write(action);
                    writer.Write("', '");
                    writer.Write(target);
                    writer.Write("');");
                }

                writer.Write("} ");
                writer.Write(HTML6);
            }
            else
                writer.Write(HTML13);

            writer.Write(HTML7);

            writer.Write(HTML8);
            writer.Write(width);
            writer.Write(HTML9);
            writer.Write(leftImagePath);
            writer.Write(HTML10);
            writer.Write("&nbsp;");
            writer.Write(HTML11);

            writer.Write(HTML8);
            writer.Write(length);
            writer.Write(HTML9);
            writer.Write(middleImagePath);
            writer.Write(HTML6);
            writer.Write(HTML16);
            writer.Write(topPadWidth);
            writer.Write(HTML10);
            writer.Write(this.Text);
            writer.Write(HTML11);

            writer.Write(HTML8);
            writer.Write(width);
            writer.Write(HTML9);
            writer.Write(rightImagePath);
            writer.Write(HTML10);
            writer.Write("&nbsp;");
            writer.Write(HTML11);

            writer.Write(HTML12);
        }
    }
}

FormSubmit.js

// click提交
function Submit(action, target)
{
    document.forms[0].action = action;
   
    if(target != null && target.length != 0)
        document.forms[0].target = target;
   
    document.forms[0].__VIEWSTATE.disabled = true;
    document.forms[0].submit();
}

// 按Enter键事件
function EnterKeyDown(id)
{
    if(window.event.keyCode == 13 && window.event.srcElement.id == id)
        return true;
    else
        return false;
}

本文地址:http://com.8s8s.com/it/it42527.htm