C#.net中TextBook回车触发事件
在tbMacID(textbook)中回车执行某个Event(写的方法)方法,并且把光标移到tbSN中,请问前后台代码怎样实现(javascript和C#)?新手,请给一...
在tbMacID(textbook)中回车执行某个Event(写的方法)方法,并且把光标移到tbSN中,请问前后台代码怎样实现(javascript和C#)?
新手,请给一个实例写具体一点. 展开
新手,请给一个实例写具体一点. 展开
展开全部
直接用这个方法 让指定的文本框按回车后触发指定的按钮
/// <summary>
/// 指定文本框触发指定按钮
/// </summary>
/// <param name="page">当前页面</param>
/// <param name="TextBoxToTie">文本框</param>
/// <param name="ButtonToTie">指定按钮</param>
public static void TieButton(Page page, Control TextBoxToTie, Control ButtonToTie)
{
string jsString = "";
if (ButtonToTie is LinkButton)
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
+ page.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
}
else if (ButtonToTie is ImageButton)
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
+ page.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
}
else
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."
+ "forms[0].elements['" + ButtonToTie.UniqueID.Replace(":", "_") + "'].click();return false;} else return true; ";
}
if (TextBoxToTie is HtmlControl)
{
((HtmlControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
else if (TextBoxToTie is WebControl)
{
((WebControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
}
/// <summary>
/// 指定文本框触发指定按钮
/// </summary>
/// <param name="page">当前页面</param>
/// <param name="TextBoxToTie">文本框</param>
/// <param name="ButtonToTie">指定按钮</param>
public static void TieButton(Page page, Control TextBoxToTie, Control ButtonToTie)
{
string jsString = "";
if (ButtonToTie is LinkButton)
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
+ page.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
}
else if (ButtonToTie is ImageButton)
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
+ page.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
}
else
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."
+ "forms[0].elements['" + ButtonToTie.UniqueID.Replace(":", "_") + "'].click();return false;} else return true; ";
}
if (TextBoxToTie is HtmlControl)
{
((HtmlControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
else if (TextBoxToTie is WebControl)
{
((WebControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
textbox的属性里面,本身就带了keyup keydown之类的事件,在GUI界面直接可以找到,然后双击,生成如下代码:
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
textBox1.Text = "ok";
}
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
textBox1.Text = "ok";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
TextBox1.Atributes.Add("onkeypress","<script language='javascript'>document.all."+Button1.ClientID+".click;</script>");
然后你在btton事件中写个tbSN.Focus();就能将光标移到tbSN里了!
然后你在btton事件中写个tbSN.Focus();就能将光标移到tbSN里了!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是在网站项目中 没有那么多事件
可以双击文本框生成:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("<script>alert('11111');</script>");
tbSn.Focus();
}
可以双击文本框生成:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("<script>alert('11111');</script>");
tbSn.Focus();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询