補(bǔ)充一下 開始先導(dǎo)入 nam......
補(bǔ)充一下 開始先導(dǎo)入
namespace
using System.Text;
using System.Web.UI;
在web 頁面中
通過調(diào)用Focus類中的方法實(shí)現(xiàn)獲取焦點(diǎn)的目的 可以放在Page_Load(,)中實(shí)現(xiàn) 也可以在Button_click事件中實(shí)現(xiàn)
我用的是Button_Click時(shí)間
代碼如下
//TextBox1是要獲得焦點(diǎn)的控件的ID
Focus.SetEnterControl(this.TextBox1);
Focus.SetFocus(TextBox1.Page,"TextBox1");
好了 你自己試試看吧
關(guān)閉網(wǎng)頁調(diào)用javascrip......
關(guān)閉網(wǎng)頁調(diào)用javascript
<script lang="text/javascript">
window.close();
</script>
獲得焦點(diǎn) 在C#中新建一個(gè)類
例如:
sing System;
using System.Text;
using System.Web;
using System.Web.UI;
namespace Application
{
/// <summary>
/// 獲取焦點(diǎn)類
/// </summary>
public class Focus
{
public Focus()
{
}
//設(shè)置成一個(gè)靜態(tài)的方法,這樣在使用的時(shí)候不用創(chuàng)建一個(gè)實(shí)例對(duì)象,直接采用Focus.SetFocus(this, "Button2")的形式進(jìn)行調(diào)用。
public static void SetFocus(System.Web.UI.Page page, String m_focusedControl)
{
//如果控件名稱為空,則返回
if(m_focusedControl == "")
return;
//添加腳本以聲明函數(shù)
StringBuilder sb = new StringBuilder("");
sb.Append("<script language=javascript>");
sb.Append("function ");
sb.Append("setFocusFunctionName");
sb.Append("(ctl) {");
sb.Append("if(document.forms[0][ctl] != null) ");//如果不為空,則設(shè)置焦點(diǎn),這里調(diào)用的Javascript里面的方法
sb.Append(" document.forms[0][ctl].focus();" );
sb.Append("}");
//添加腳本以調(diào)用函數(shù)
sb.Append("setFocusFunctionName");
sb.Append("('");
sb.Append(m_focusedControl);
sb.Append("');");
sb.Append("</");
sb.Append("script>");
if (!page.IsStartupScriptRegistered("SetFocusScriptName"))
page.RegisterStartupScript("SetFocusScriptName", sb.ToString());//將這段javascript代碼寫到頁面中去
}
/// <summary>
/// 設(shè)置在頁面回車時(shí)觸發(fā)事件的控件
/// </summary>
/// <param name="Ctrl">將觸發(fā)事件的控件對(duì)象</param>
public static void SetEnterControl(System.Web.UI.Control Ctrl)
{
Page mPage = Ctrl.Page;
string mScript;
mScript = @"<script language=""javascript"">
function document.onkeydown()
{
var e = event.srcElement;
var k = event.keyCode;
if (k == 13 && e.type != ""textarea"")
{
document.all." + Ctrl.ClientID + @".click();
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>";
if(!mPage.IsClientScriptBlockRegistered("SetEnterControl"))
mPage.RegisterClientScriptBlock("SetEnterControl",mScript);
}
}
}
實(shí)際上就是在codebehide 調(diào)用javascript 來袮補(bǔ)C# 中不能獲得焦點(diǎn)不足
請(qǐng)多指教啊 多多交流啊