Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! i have one textbox and a label. Here my query is that whenever i"ll start writing in the textbox immediately label will show a message "This username is unavailable" or "This username is available"(textbox data will search in database for username). I've written the following code. But it's working only whenever I"m Pressing "Enter" switch in the textbox. I want that should happen without using of "Enter" switch.

I have enabled AutoPostBack=true for textbox.

Source Code:
XML
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" CausesValidation="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


C# Code:
C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            SqlCn.Open();
            SqlCommand SqlCmd = new SqlCommand("SELECT Manufacturer FROM MOBILE WHERE Manufacturer='" + TextBox1.Text + "'", SqlCn);
            SqlDataAdapter adapter = new SqlDataAdapter(SqlCmd);
            SqlDataReader SqlDr = SqlCmd.ExecuteReader();
            if (SqlDr.Read())
            {
                Label1.Text = "This username is unavailable.";
            }
            else
            {
                Label1.Text = "This username is available";
            }
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }
Posted

1 solution

First write this code in your .ASPX.
XML
<div id="divMessage" runat="server" class="" style="display: none;">
       <asp:Literal runat="server" ID="ltlMessage" Text="test"></asp:Literal>
   </div>

Then just write this code behind you .CS page.
C#
public static void ShowMessage(string _class, string msg, System.Web.UI.HtmlControls.HtmlGenericControl divMessage, Literal ltlMessage)
       {
           divMessage.Style.Add(&quot;display&quot;, &quot;block&quot;);
           divMessage.Attributes.Add(&quot;class&quot;, _class);
           ltlMessage.Text = msg;
       }</pre>

C#
try
               {
                   UISettings.ShowMessage("alert alert-success", "Insert Successfully", divMessage, ltlMessage);
               }
               catch (Exception ex)
               {
                   ShowMessage("alert alert-error", ex.InnerException.Message, divMessage, ltlMessage);
               }
 
Share this answer
 
Comments
Chiklu.Soumya 22-Apr-13 11:27am    
Thanks for reply. But couldn't understand your solution. Please read my query carefully.
Chiklu.Soumya 22-Apr-13 11:38am    
My query is like creating an account in gmail or yahoo for first time where a username is suggested by a lable whether typed username is available or not.
Maksud Saifullah Pulak 22-Apr-13 11:52am    
yes i understand you problem.By my solution is just resolved the message information.But you can apply your own logic to see the message.You may call function on textbox text change event or onblur.You also call web service to get the current exists data.

Thanks,
Chiklu.Soumya 22-Apr-13 12:01pm    
I need your help. Can you get me the solution. I don't know javascript. Thanks in advance.

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