I have two update panels. The first contains a textbox. When text is entered, it calls a stored procedure to populate a listbox, which is contained in the second update panel, with the returned table data based on a LIKE match against what is entered into the text box. It's not a big table, and rather than store the data in a local table, I just make the SQL call. Eventually, I will store it in a local table. When an item is selected from the listbox, the textbox is populated with the selected value. My problem is that with each character typed into the textbox, the focus is lost. And even if I do get the focus back using textbox.focus() the cursor is at the beginning of the text, rather than the end, and the page scrolls to the top.
Is there a way to keep focus at the end of the entered string, without the page scrolling to the top.
Here is my page code:
<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>
Here is the code behind:
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;
}
}
Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire