C#通过两个更新面板使TextBox保持焦点

时间:2018-09-19 20:51:18

标签: c# asp.net web focus updatepanel

我有两个更新面板。第一个包含一个文本框。输入文本后,它会调用存储过程来填充列表框,该列表框包含在第二个更新面板中,并根据与文本框中输入内容匹配的LIKE匹配返回的表数据。它不是一个大表,我只是进行SQL调用,而不是将数据存储在本地表中。最终,我将其存储在本地表中。从列表框中选择一个项目时,将使用所选值填充文本框。我的问题是,在文本框中键入每个字符后,焦点就会丢失。而且即使我确实使用textbox.focus()获得了焦点,光标也位于文本的开头而不是结尾,并且页面滚动到顶部。

有没有一种方法可以使焦点保持在输入字符串的末尾,而无需将页面滚动到顶部。

这是我的页面代码:

<asp:Content ID="Content1" ContentPlaceHolderID="head1" Runat="Server">
    <link href="/css/usa.css" rel="stylesheet" />
    <link href="/css/formTables.css" rel="stylesheet" />
    <script type="text/javascript">
        function courseTextChanged() {
            var txt = document.getElementById("<%=courseTextBox.ClientID %>");
            txt.setAttribute("onkeyup", txt.getAttribute("onchange"));
            txt.setAttribute("onchange", "");
        }
        window.onload = function () {
            courseTextChanged();
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            if (prm != null) {
                prm.add_endRequest(function (sender, e) {
                    if (sender._postBackSettings.panelsToUpdate != null) {
                        courseTextChanged();
                    }
                });
            }
        };
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="contentBody" Runat="Server">
    <div id="content">
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>            
                <asp:UpdatePanel ID="CourseTextPanel" runat="server" style="width:460px; float:right;">

                    <ContentTemplate>
                        <p id="paraTwo" runat="server">
                            <asp:TextBox ID="courseTextBox" runat="server" CssClass="formTableTextBox" OnTextChanged="course_TextChanged" AutoPostBack="true" /><br /><span class="labelNote">Enter the name of the course you are searching for in this box.</span>
                        </p>
                    </ContentTemplate>

                    <Triggers>

                        <asp:AsyncPostBackTrigger ControlID ="courseTextBox" EventName ="TextChanged" />

                    </Triggers>

                </asp:UpdatePanel>
                <asp:UpdatePanel ID="ListBoxPanel" runat="server" style="width:460px; float:right;" >
                    <ContentTemplate>
                        <div id="crsListDIV" runat="server" style="width:460px; height:241px;" Visible="false">
                            <asp:ListBox ID="crsList" runat="server" Height="240px" Width="460px" style="overflow-x:auto; margin-left:20px;" AutoPostBack="True" OnSelectedIndexChanged="addCourseToTextbox" SelectionMode="Single" EnableViewState="True"></asp:ListBox>
                        </div>

                    </ContentTemplate>

                    <Triggers>

                        <asp:AsyncPostBackTrigger ControlID ="crsList" EventName ="SelectedIndexChanged" />

                    </Triggers>
                </asp:UpdatePanel>
    </div>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class EMICourses_EMI_Out_of_Cycle_Training_Request_Form : System.Web.UI.Page
{

   string courseList;
   protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void course_TextChanged(object sender, EventArgs args)
    {
        if (courseTextBox.Text.Length > 0)
        {
            BindListBox();
        }
        else
        {
            crsList.Items.Clear();
            crsListDIV.Visible = false;

        }
    }
    protected string stripLeadingZero(string textboxKeywordText)
    {
        string pattern = @"(.)(0)(\d+)";
        string replacement = "$1$3";
        string textKeyWord = Regex.Replace(textboxKeywordText, pattern, replacement);

        return textKeyWord;
    }

    protected void BindListBox()
    {
        if (courseTextBox.Text.Length > 0)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings.Get("connString"));
            string textboxKeyword = stripLeadingZero(courseTextBox.Text);
            string sp = "";
            sp = "selAllCourses";
            SqlCommand command = new SqlCommand(sp, con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@keywords", SqlDbType.VarChar).Value = textboxKeyword;
            SqlDataAdapter da = new SqlDataAdapter(command);

            DataSet ds = new DataSet();
            da.Fill(ds, "CourseList");
            ds.Tables[0].Columns.Add("CodeAndTitle", typeof(string), "CourseCode + ' ' + CourseTitle");
            crsList.DataSource = ds;
            crsList.DataTextField = "CodeAndTitle";
            crsList.DataValueField = "CodeAndTitle";
            crsList.DataBind();
            crsListDIV.Visible = true;
        }
        else
        {
            crsList.Items.Clear();
            crsListDIV.Visible = false;
        }

    }

    protected void addCourseToTextbox(object sender, EventArgs e)
    {
        Int32 item = crsList.SelectedIndex;
        courseTextBox.Text = crsList.Items[item].ToString();
        crsListDIV.Visible = false;
    }
}

任何帮助将不胜感激。

0 个答案:

没有答案