今天在写程序的时候碰到一个问题:需要为不同的TextBox指定不同的激发按钮。重写了一个类(累),哈哈
Code
public class FocusTextBox:TextBox
{
/// <summary>
/// 设置和获取文本框的提交按钮
/// </summary>
public string DefaultButton
{
get
{
object ret = this.ViewState["SubmitControl"];
if (ret != null)
{
return (string)ret;
}
return string.Empty;
}
set
{
this.ViewState["SubmitControl"] = value;
}
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (this.DefaultButton.Length > 0)
{
Control ctrl = FindControl(DefaultButton);
if (ctrl != null)
{
string strScript = "if(event.keyCode == 13){document.getElementById('" + ctrl.ClientID + "').click();event.returnValue = false;}";
writer.AddAttribute("onkeydown", strScript);
}
}
}
public class FocusTextBox:TextBox
{
/// <summary>
/// 设置和获取文本框的提交按钮
/// </summary>
public string DefaultButton
{
get
{
object ret = this.ViewState["SubmitControl"];
if (ret != null)
{
return (string)ret;
}
return string.Empty;
}
set
{
this.ViewState["SubmitControl"] = value;
}
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (this.DefaultButton.Length > 0)
{
Control ctrl = FindControl(DefaultButton);
if (ctrl != null)
{
string strScript = "if(event.keyCode == 13){document.getElementById('" + ctrl.ClientID + "').click();event.returnValue = false;}";
writer.AddAttribute("onkeydown", strScript);
}
}
}
转载请注明出处:http://www.cnblogs.com/zijinguang 谢谢