in

@Prog! - ASP.NET(C#) AJAX -

ASP.NET(C#) 2.0 & ASP.NET Ajax (ATLAS) のメモ書き

ASP.NET Ajax UpdatePanel 内のコントロールにフォーカスを移動する

最新の投稿は、投稿日時: 2007/02/02 17:05 投稿者: ASANO です。スレッドには 1 件の返答があります。
ページ 1 / 1 (2 アイテム)
投稿の並べ替え: 前へ 次へ
  • 2007/02/02 17:04

    ASP.NET Ajax UpdatePanel 内のコントロールにフォーカスを移動する

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>ASP.NET Ajax UpdatePanel 内のコントロールにフォーカスを移動する</title>
    </head>
    <body>
        <form id="form1" runat="server">
       
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
           
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>

                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
                        OnCheckedChanged="CheckBox1_CheckedChanged"
                        Text="フォーカス移動" />
                       
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                   
                </ContentTemplate>
            </asp:UpdatePanel>
           
        </form>
    </body>
    </html>

  • 2007/02/02 17:05 回答元:

    Re: ASP.NET Ajax UpdatePanel 内のコントロールにフォーカスを移動する

    using System;
    using System.Data;
    using System.Configuration;
    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;

    using System.Text;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("var controlID;\r\n");
                sb.Append("function UpdatePanelSetFocus() {\r\n");
                sb.Append("    try {\r\n");
                sb.Append("         $get(controlID).focus();\r\n");
                sb.Append("         $get(controlID).select();\r\n");
                sb.Append("    } catch(e) { }\r\n");
                sb.Append("}\r\n");
                sb.Append("function UpdatePanelFocusScript(clientID) {\r\n");
                sb.Append("    controlID = clientID;\r\n");
                sb.Append("    setTimeout(\"UpdatePanelSetFocus()\", 500);\r\n");
                sb.Append("}\r\n");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UpdatePanelSetFocus", sb.ToString(), true);

                CheckBox1.Attributes.Add("onclick", "UpdatePanelFocusScript('" + TextBox1.ClientID + "')");
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            TextBox1.Focus();
        }
    }

ページ 1 / 1 (2 アイテム)