Set focus inside an Update Panel

本文介绍了一种在ASP.NET页面中使用C#代码为特定TextBox控件设置焦点的方法。通过RadioButton的选择触发事件,利用客户端脚本实现TextBox获得焦点的功能,并在UpdatePanel中保持良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//***************************************************************************************

    //Code behind

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        SetMyFocus(ref TextBox1);
        TextBox1.Focus();
    }

    private void SetMyFocus(ref TextBox txtBox)
    {
        string focusString = "setTimeout(/"$('" + txtBox.ClientID + "').focus(); /", 100);";
        ClientScript.RegisterStartupScript(this.GetType(), "focusString", focusString, true);
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        SetMyFocus(ref TextBox2);
        TextBox2.Focus();
    }

 
//***************************************************************************************

 The following code is from the source view of the page
 
<%@ Page Language="C#" MasterPageFile="~/Test.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" Title="Untitled Page" %>
<%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="CPHTest" Runat="Server">
    
   <cc1:ScriptManager ID="ScriptManager2" runat="server" EnablePartialRendering="true">
   </cc1:ScriptManager>
    <cc1:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
          <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" OnCheckedChanged="RadioButton1_CheckedChanged" GroupName="Group1" /><br />
          <asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="Group1" OnCheckedChanged="RadioButton2_CheckedChanged" /><br />
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
          <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
          <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
           
      </ContentTemplate>
    </cc1:UpdatePanel>
</asp:Content>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值