Click here to Skip to main content
16,016,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Id like to know, if I was to search for a particular album in my asp.net , it will show the album, but if say the text-box where I type in the search for a particular album is blank, how do I code it to say that result you have typed is blank?

All my pages that have the search text box on them , when you type it redirects you to a search result page that has a label to display the error or if it finds the search, then it shows the album on a data list.

Thanks.
Posted
Comments
Sridhar Patnayak 11-Jun-12 4:36am    
Not clear

I am assuming that you want to validate your textbox that whether it is blank or not on click of Search.
You can either use client side Javascript to alert the user or use a server side message to inform that textbox is blank.

Javascript Method:

JavaScript
function validateText()
{
    if(document.getElementById("txtSearch").value == '')
    {
        alert("Please enter album name to search");
        return false;
    }
    else
        return true;
}


Call the above Javascript funtion on the ClientClick of your Search button as below:

ASP.NET
<asp:button id="btnSearch" name="Search" onclientclick="return validateText()" onclick="btnSearch_Click"></asp:button>


ServerSide Method:

Inside btnSearch_Click event, write a code to display required message.

C#
void btnSearch_Click(object sender, EventArgs e) 
{
    if(txtSearch.Text == "" || txtSearch.Text == string.Empty)
    { 
       lblMessage.Text = "Please enter album name to search.";
    }
}


Hope this helps.
 
Share this answer
 
v2
Comments
Sandeep Mewara 11-Jun-12 4:53am    
My 5!
Vani Kulkarni 11-Jun-12 5:17am    
Thanks Sandeep!
Use RequiredFieldValidator control for validation on client side & server side.
 
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