как да преместя фокуса извън текстовото поле в c# asp.net?

Имам поле за търсене, което взема потребителско име и търси по него в потребителския списък, проблемът е, че искам от програмата да търси директно, тъй като той получава първата буква от потребителя

Преден край

<asp:TextBox ID="TextBox1" 
             class="form-control table-search-input"  
             ontextchanged="TextBox1_TextChanged"  
             AutoPostBack="True"  
             runat="server"  >

Бекенд

protected void TextBox1_TextChanged(object sender, EventArgs e )
{
    int bb=TextBox1.Text.Length;
    adminuserlist2 = adminuserlist2.Where(o => o.AdminUserName.Substring(0,bb) == TextBox1.Text).ToList();

    if (adminuserlist2 != null)
    {

        Table1.Rows.Clear();
        Table1.BorderWidth = 1;
        Table1.BorderStyle = BorderStyle.Groove;

        Table1.GridLines = GridLines.Both;

        TableCell un = new TableCell();
        un.Text = "UserName";
        TableCell pass = new TableCell();
        pass.Text = "Password";
        TableCell email = new TableCell();
        email.Text = "Email";
        TableCell isactives = new TableCell();
        isactives.Text = "IsActive";
        TableCell typename = new TableCell();
        typename.Text = "Type Name";
        TableCell typeid = new TableCell();
        typeid.Text = "Type Id";

        row = new TableRow();
        row.Width = new Unit("120%");
        row.BackColor = System.Drawing.Color.White;
        row.ControlStyle.Font.Size = 25;
        row.ControlStyle.Font.Bold = true;
        row.ControlStyle.ForeColor = System.Drawing.Color.Black;


        row.Controls.Add(un);
        row.Controls.Add(pass);
        row.Controls.Add(email);
        row.Controls.Add(isactives);
        row.Controls.Add(typename);
        row.Controls.Add(typeid);
        Table1.Controls.Add(row);
        row.ControlStyle.Width = 300;
        row.ControlStyle.Height = 30;
        Table1.Width = 500;

        Table1.ControlStyle.Width = 1000;
        Table1.Height = 500;

        Table1.CellSpacing = 50;
        Table1.CellPadding = 50;

        for (int i = 0; i < adminuserlist2.Count; i++)
        {
            TableCell un2 = new TableCell();
            un2.Text = adminuserlist2[i].AdminUserName;
            TableCell pass2 = new TableCell();
            pass2.Text = adminuserlist2[i].AdminUserPassword;
            TableCell email2 = new TableCell();
            email2.Text = adminuserlist2[i].AdminUserEmail;
            TableCell isactives2 = new TableCell();
            isactives2.Text = adminuserlist2[i].IsActive.ToString();
            TableCell typename2 = new TableCell();
            typename2.Text = adminuserlist2[i].AdminUserType;
            TableCell typeid2 = new TableCell();
            typeid2.Text = adminuserlist2[i].AdminUserTypeID.ToString();
            row = new TableRow();
            row.Controls.Add(un2);
            row.Controls.Add(pass2);
            row.Controls.Add(email2);
            row.Controls.Add(isactives2);
            row.Controls.Add(typename2);
            row.Controls.Add(typeid2);
            Table1.Controls.Add(row);
        }
    }

}

Сблъсках се с проблем, че трябва да щракна извън текстовото поле, за да търся, моля, всеки може да помогне


person Bisher Andoura    schedule 08.09.2015    source източник
comment
за това можете да използвате ajax call.   -  person vinodh    schedule 08.09.2015
comment
Можете да посетите тази връзка в противен случай трябва да използвате плъгини за jquery.   -  person शेखर    schedule 08.09.2015


Отговори (2)


Ще бъде по-добре, ако изберете OnKeyPress събитие вместо TextChanged просто сменете събитието и то ще работи

Преден край

<asp:TextBox ID="TextBox1" 
             class="form-control table-search-input"  
             onkeypress="__doPostBack(this.name,'OnKeyPress');" 
             AutoPostBack="True"  
             runat="server"  >

BackEnd

private void TextBox1_OnKeyPress(string ctrlName, string args){
    //your code goes here
}
person Mohit Shrivastava    schedule 08.09.2015

В Asp.Net няма реално събитие textchanged, то ще се задейства само ако текстовото поле загуби фокус и ако сте задали autopostback на true.

Това, което можете да направите е следното:

Добавете ScriptManager към вашата aspx страница:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>

Добавете клиентско събитие onkeypress към вашето текстово поле:

<asp:TextBox ID="TextBox1" 
         class="form-control table-search-input"  
         ontextchanged="TextBox1_TextChanged"  
         AutoPostBack="True"  
         runat="server" onkeypress="CallMyPageMethod();" >

Обадете се на вашия PageMethod:

    <script language="javascript" type="text/javascript">
    function CallMyPageMethod() {            
    PageMethods.MyPageMethod();
    }

Add a PageMethod to your code behind:

    [System.Web.Services.WebMethod]
    public static void MyPageMethod()
    {
        //add your textchanged event code here
    }
person Bgl86    schedule 08.09.2015