分享
 
 
 

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

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在开发时如果要使用多个图片按钮时,我们经常必须为每个按钮制作一个图片(如:新增,修改,删除

等,其实只是文字不一样而已,我们却必须制作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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有