IE中控制焦点(asp.net)

使用母版页,Ajax UpdatePanel包含的下拉框,点击下拉框PostBack之后focus不奏效,第二次点击下拉框PostBack之后focus奏效????

JS

function resetFocus(id){
          var obj=document.getElementById(id);
          if(obj) obj.focus();
      }

 

C#

ScriptManager.RegisterStartupScript(this, this.GetType(), "resetFocusJS", "resetFocus(\"" + ddlRegion.ClientID + "\");", true);

 

------------------------------

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="TESTPAGE.master.cs" Inherits="TESTPAGE" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
        <script language="javascript" type="text/javascript">
   function resetFocus(id){
        var obj=document.getElementById(id);
        if(obj) obj.focus();
          //document.getElementById(id).select();
      }    
    </script>
</head>
<body>
    <form id="form1" runat="server">
      <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

------------------------------

<%@ Page Language="C#" MasterPageFile="~/TESTPAGE.master" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
   Region:
                <asp:DropDownList runat="server" ID="ddlRegion" DataValueField="Region_Id" DataTextField="Name"
                    AutoPostBack="true" OnSelectedIndexChanged="ddlRegion_SelectedIndexChanged">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                </asp:DropDownList>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "ResetFilter", "resetFocus(\"" + ddlRegion.ClientID + "\"); ", true);

    }
}

------------------------------

Solution:

ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "fuck", "setTimeout(\"document.all('ctl00$ContentPlaceHolder1$ctl01$Jbyj').focus();\",100);", true);

 

注:Firefox不需要额外控制焦点,自动保留焦点在最后操作的控件上。

 

http://blog.sina.com.cn/s/blog_4473b2d10100arg5.html

http://www.cnblogs.com/njnudt/archive/2008/11/18/1335948.html

posted @ 2010-01-02 09:34  emanlee  阅读(486)  评论(3编辑  收藏  举报