Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my requirement is in Asp.net page consists of one textbox. when i enter charector on that i search database table and display corresponding records in popup and we select one of then that will be displayed.
My Database Query is
select * from jobs where job_desc like '"+document.getElementById("<%=TextBox2.ClientID%>").value+"'"
Posted
Comments
PSK_ 29-May-10 23:57pm    
What is the issue you are facing?
radix3 30-May-10 11:10am    
code please

hello,

you use javascript with ajax for this.

i will write the code here,you just change ur query.


this is the js file,You can use it as it is.Just change the redirect path in the searchSuggest function.redirect it where you have ur text box.and one thing more you have to download ajaxtoolkit and add it in ur bin folder.




C#
// JScript File
var maxDivId, currentDivId, strOriginal;
//Our XmlHttpRequest object to get the auto suggestvar
searchReq = getXmlHttpRequestObject();
function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        //alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
    }
}
//Called from keyup on the search textbox.//Starts the AJAX request.
function searchSuggest(e) {

    var key = window.event ? e.keyCode : e.which;
    if (key == 40 || key == 38) {
        scrolldiv(key);
    }
    else {
        if (searchReq.readyState == 4 || searchReq.readyState == 0) {
            var str = escape(document.getElementById('txtSearch').value);
            strOriginal = str;
            searchReq.open("GET", 'CreateModifyNewItem.aspx?search=' + str + '&url=' + window.location, true);
            searchReq.onreadystatechange = handleSearchSuggest;
            searchReq.send(null);
        }
    }

}
//Called when the AJAX response is returned.
function handleSearchSuggest() {
    if (searchReq.readyState == 4) {
        var ss = document.getElementById('search_suggest');
        ss.innerHTML = '';
        var str = searchReq.responseText.split("~");
        if (str.length > 1) {
            for (i = 0; i < str.length - 1; i++) {
                //Build our element string.  This is cleaner using the DOM, but
                //IE doesn't support dynamically added attributes.
                maxDivId = i;
                currentDivId = -1;
                var suggest = '<div ';
                suggest += 'id=div' + i;
                suggest += '  '
                suggest += 'onmouseover="javascript:suggestOver(this);" ';
                suggest += 'onmouseout="javascript:suggestOut(this);" ';
                suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
                suggest += document.onclick = function() { HideSuggDiv('this.innerHTML'); };
                suggest += 'class="suggest_link">' + str[i] + '</div>';
                ss.innerHTML += suggest;
                ss.style.visibility = 'visible';
            }
        }
        else {
            ss.style.visibility = 'hidden';
        }
    }

}
//Mouse over function
function suggestOver(div_value) {
    div_value.className = 'suggest_link_over';
}
function scrollOver(div_value) {
    div_value.className = 'suggest_link_over';
    document.getElementById('txtSearch').value = div_value.innerHTML;
}
function HideSuggDiv(value) {
    var txt = document.getElementById('txtSearch');
    var obj = document.getElementById('search_suggest');
    var evt = window.event || arguments.callee.caller.arguments[0];
    var eobj = window.event ? evt.srcElement : evt.target;
    if (eobj.nodeType == 3) eobj = eobj.parentNode;
    while (eobj.parentNode) {
        if (eobj == obj) return;
        eobj = eobj.parentNode;
    }
    obj.style.visibility = 'hidden';
}
//Mouse out function
function suggestOut(div_value) {
    div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
    var ss = document.getElementById('search_suggest');
    document.getElementById('txtSearch').value = value;
    ss.innerHTML = '';
    ss.style.visibility = 'hidden';
}
function scrolldiv(key) {
    var tempID;

    if (key == 40) {
        if (currentDivId == -1) {
            scrollOver(div0);
            currentDivId = 0;
        }
        else {
            if (currentDivId == maxDivId) {
                tempID = 'div' + maxDivId;
                var a = document.getElementById(tempID);
                currentDivId = -1;
                suggestOut(a)
                document.getElementById('txtSearch').value = strOriginal;
            }
            else {
                tempID = currentDivId + 1;
                setScroll(currentDivId, tempID)
            }
        }
    }
    else if (key == 38) {
        if (currentDivId == -1) {
            tempID = maxDivId;
            setScroll(maxDivId, maxDivId)
        }
        else {
            if (currentDivId == 0) {
                tempID = 'div' + currentDivId;
                var a = document.getElementById(tempID);
                currentDivId = -1;
                suggestOut(a)
                document.getElementById('txtSearch').value = strOriginal;
            }
            else {
                tempID = currentDivId - 1;
                setScroll(currentDivId, tempID)
            }
        }

    }
}
function setScroll(Old, New) {
    var tempDivId;
    currentDivId = New;
    tempDivId = 'div' + Old;
    var a = document.getElementById(tempDivId);
    suggestOut(a)
    tempDivId = 'div' + currentDivId;
    var b = document.getElementById(tempDivId);
    scrollOver(b);
}



XML
and in the aspx page you have to use the input textbox type
add ajax toolkit in the register tag and js file reference

<pre lang="xml"><input type="text" id="Text1" name="txtSearch" onkeyup="searchSuggest(event);"
                                            style="width: 170px" />
                                        <asp:HiddenField ID="HiddenField1" runat="server" />
                                        <div id="Div4" style="z-index: 2; visibility: hidden; position: absolute;
                                            left: 11px; width: 170px; top: 40px">
                                        </div>
                                            <asp:Button ID="Button1" runat="server" Text="Search" CausesValidation="False"
                                            OnClick="btnSearch_Click" CssClass="asset-viewer-btn" OnClientClick="getSearchValue();" />
    <script type="text/javascript" language="javascript">
        function getSearchValue() {
            var getValue = document.getElementById('txtSearch').value;
            var hiddenfield = document.getElementById("<%=txtHiddenFieldSearch.ClientID%>").value = getValue;
        }
    </script>






on the page load in code behind:

C#
if (Request.QueryString["search"] != null)
            {
              string  clientName = Request["search"].ToString();
                Getresult();
            }







private void Getresult()
{
DataTable dt = new DataTable();

//string search = "Select * from assets where AssetType Like Concat ('%','" + clientName + "','%') || AssetSubType Like Concat ('%','" + clientName + "','%') || AssetType Like Concat('%','" + clientName + "','%') Limit 7";
string search = "Select AssetType from assets where AssetType Like '" + clientName + "%' group by AssetType UNION Select AssetSubType from assets where AssetSubType Like '" + clientName + "%' group by AssetSubType UNION Select AssetName from assets where AssetName Like '" + clientName + "%' group by AssetName LIMIT 10";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
command = new MySqlCommand(search, myConnection);
MySqlDataAdapter da = new MySqlDataAdapter(command);
da.Fill(dt);
StringBuilder sb = new StringBuilder();
if (clientName != "")
{
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append(dt.Rows[i].ItemArray[0].ToString() + "~"); //Create Con
//sb.Append(dt.Rows[i].ItemArray[2].ToString() + "~");
//sb.Append(dt.Rows[i].ItemArray[3].ToString() + "~");
}
}
}
Response.Write(sb.ToString());

}



protected void btnSearch_Click(object sender, EventArgs e)
{
string searchValue = txtHiddenFieldSearch.Value;
//txtSearch.Value = txtSearch.Value.Replace(" ", "").Replace(" ", "").Trim();
string search = "Select * from assets where AssetType Like '" + searchValue + "%' || AssetSubType Like '" + searchValue + "%' || AssetName Like '" + searchValue + "%'";

if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//OdbcCommand command = new OdbcCommand(search, myConnection);
command = new MySqlCommand(search, myConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter(search, myConnection);
DataSet ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
myConnection.Close();

}
 
Share this answer
 
v2
Ramesh Reddy11111 wrote:
select * from jobs where job_desc like '"+document.getElementById("<%=TextBox2.ClientID%>").value+"'"


Where did you learnt doing like this? From the syntax, you have made a query in JavaScript. Now what? This is not the way you should code. I would suggest you to buy a book or read beginners tutorials before starting to code. You are not in right direction for now.

Based on your requirement, steps should be:
1. Place search textbox and a button in Parent Page
2. Tie up the button to get the textbox data and pass it on to a new window (which you call a popup) (Querystring can be simple for you)
3. In new window, use the textbox value and get data from database in your code behind
4. Show the data retrieved in a grid.
5. Show the selected record

Try it. It's a simple work flow, and one should be able to do it as long as you know simple and beginner tutorials of ASP.NET.
 
Share this answer
 
Comments
Ramesh Reddy11111 30-May-10 12:07pm    
i want search as Google. i entered some charactor on the google search textbox. when ever i entered chr in to that related information is displayed like dorpdown i selected one of them and the enter. i want that type on serch.
refer: http://www.google.co.in/
Ramesh Reddy11111 wrote:
i want search as Google. i entered some charactor on the google search textbox. when ever i entered chr in to that related information is displayed like dorpdown i selected one of them and the enter. i want that type on serch. refer: http://www.google.co.in/

Thanks for making me refer Google search!!!

I would suggest you to read your question again. Where exactly have you mentioned that. If you cannot frame your question properly, how come you can expect someone to help you?

Now i would ask you to google "AutoSuggest" - you will get number of articles on it. If you want, even CopeProjct has few...

Had it been a normal case, i would had provided the article links, but now i would suggest you to use www.gooogle.com and select the article that suits you.

BTW:
Ramesh Reddy11111 wrote:
select * from jobs where job_desc like '"+document.getElementById("<%=TextBox2.ClientID%>").value+"'"

This still is a very WRONG way to achieve it and still suggest to read it before coding it. You need to pass the text typed to code-behind and do the stuff!
 
Share this answer
 
v2
Hay go for Ajax auto compleate extender control it will solve u r problem
 
Share this answer
 
v2
Amazingly, you ask a stupid question and get a ton of stupid answers.

1 - you do NOT need AJAX. You can use it, but you don't NEED it.
2 - I don't understand why you're building your SQL in javascript, but putting SQL in the page you send to the client is pure idiocy. It's even less secure than simply mashing SQL from the contents of the textbox ( which someone told you to do on this thread ).

Your best bet is to pass the text from the textbox on the URL of the popup, and then in the code behind, pass that text to a stored procedure as a parameter. This protects you from SQL injection, as well as pure SQL replacement. It makes your code organised and designed instead of thrown randomly together. The task is sufficiently trival, and the code you post sufficiently clueless, that I'd suggest if you're being paid for this code, you are stealing, and that you should be doing a course and/or reading some books to learn some very basic ASP.NET before you do anything else.
 
Share this answer
 

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