Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
i have a gridview and a textbox

all i wanna do is a searching textbox that on each onkeyup the gridview contents changed

i did this part although

the problem is when the page postback, the focus on textbox lost.
And when i write code in code-behind as textbox.focus(), all the element inside the text are highlightened, and when i type again the text inside textbox disapper.

is there a way to focus on textbox and unhighlightened it?

please help!

sorry for my poor english

aspx
C#
function __doPostBack(eventTarget, eventArgument)
        {
            document.Form1.__EVENTTARGET.value = eventTarget;
            document.Form1.__EVENTARGUMENT.value = eventArgument;
            document.Form1.submit();
        }


XML
<asp:TextBox ID="txtLike" runat="server" Width="240px"
                 AutoPostBack="True" onkeyup="javascript:__doPostBack('txtLike','')"
             ontextchanged="txtLike_TextChanged"></asp:TextBox>


code-behind
C#
protected void Page_Load(object sender, EventArgs e)
    {
            if (Request.Form["__EVENTTARGET"] == "txtLike")
            {
                txtLike_TextChanged(this,new EventArgs());
            }
    }


C#
protected void txtLike_TextChanged(object sender, EventArgs e)
    {
        GridView1.DataBind();
        //SetFocus(txtLike) or txtLike.Focus();
    }
Posted

 
Share this answer
 
Comments
vonDy 18-Feb-12 10:34am    
this is for webform and not in winform.
is there a way in webform to do it?
Hey This Javascript work fine for me

C#
//for serching gridview on keyup
        function Filter(Obj) {

            var grid = document.getElementById('cntPlcHolder_gvDisplay');
            var terms = Obj.value.toUpperCase();
            var cellNr = 0; //your grid cellindex like name
            var ele;
            for (var r = 1; r < grid.rows.length; r++) {
                ele = grid.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g, "");
                if (ele.toUpperCase().indexOf(terms) >= 0)
                    grid.rows[r].style.display = '';
                else grid.rows[r].style.display = 'none';
            }
        }


//calling of function
<input type='text' onkeyup='Filter(this);' id="txtSearch" class="SmallTextBox" runat="server" />

Best Luck
 
Share this answer
 
Comments
vonDy 18-Feb-12 11:17am    
is there a way using an asp textbox control?

like this
<asp:TextBox ID="TextBox1" runat="server">

and not this one
<input type='text' önkeyup='Filter(this);' id="txtSearch" class="SmallTextBox" runat="server" />
Nilesh Patil Kolhapur 19-Feb-12 7:29am    
hi vonDy i am going to improve this answer for you ok
vonDy 19-Feb-12 8:03am    
ur a big help
there is many things i learned in your first solution even though it is not exactly i'm looking for.
i really need help i'm just a beginner in this, so thank you :)
Nilesh Patil Kolhapur 19-Feb-12 23:26pm    
if u write runAt serer then <input type=text> become a server side control means this same as <asp:TextBox ID="TextBox1" runat="server"> so no need to change if u really need then tell me
Member 13723018 19-Mar-18 5:29am    
it's working but only on current page when gridview allow paging is true
i want to search all pages in gridview when allow paging is true
give me any suggestion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900