Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
plz
anyone can tell me
how we can compare the text box and lable value in asp.net
Posted
Comments
Sergey Alexandrovich Kryukov 6-Aug-11 3:49am    
Why it's a problem?
--SA
Monjurul Habib 6-Aug-11 14:02pm    
share your code both markup and code-behind.
Uday P.Singh 6-Aug-11 14:39pm    
post your code to help you better!!

C#
if (TextBox1.Text == Label1.Text)
            {
                //Do Something.
            }
            else
            {
                //Do Something if not equals
            }
 
Share this answer
 
v2
Comments
prdpvts 5-Aug-11 9:50am    
its not making possible by me plz make practicle for it and mail me to prdpvts@gmail.com its an request
Moustafa Safwat 5-Aug-11 10:10am    
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Is 
<asp:Label ID="Label1" runat="server" Text="Label Text" ForeColor="Maroon"> 

<asp:TextBox ID="TextBox1" runat="server">
 
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton 
<asp:Label ID="Label2" runat="server" Text="">
</div>
</form>
</body>
</html>
Moustafa Safwat 5-Aug-11 10:10am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void LinkButton1_Click(object sender, EventArgs e)
{
if (Label1.Text == TextBox1.Text)
{
Label2.Text = "Yes";
Label2.ForeColor = Color.Green;
}
else
{
Label2.Text = "No";
Label2.ForeColor = Color.Green;
}
}
}
}
prdpvts 8-Aug-11 2:55am    
its working only one color..... is i type yes or no...it show one color only......
prdpvts 8-Aug-11 3:33am    
it show only color of "no";;;;;;
They are both controls, so you could just do a TextBox.Text == Label.Text in your code-behind after adding the id and runat="server" attributes to the label control in your markup.
 
Share this answer
 
v2
Comments
prdpvts 5-Aug-11 9:50am    
its not making possible by me plz make practicle for it and mail me to prdpvts@gmail.com its an request
Espen Harlinn 9-Aug-11 10:37am    
Right, my 5
You should use Equals to compare both Strings, and it's also a good practice to specify the StringComparison options.

Read this for more info:
Best Practices for Using Strings in the .NET Framework[^]

C#
if(TextBox.Text.Equals(Label.Text, StringComparison.Ordinal))
{
  //Values are equal
}
else
{
  //Values are not equal
}


Hope it helps
 
Share this answer
 
Comments
RaisKazi 5-Aug-11 10:00am    
Nice link 5.
You can try as below.

For Case Insensitive Compare
C#
//Case Insensitive Compare
if (string.Compare(TextBox1.Text, Label1.Text, true) == 0)
{
  //Matching
}
else
{
 //Not Matching
}


For Case Sensitive Compare
C#
//Case Sensitive Compare
if (string.Compare(TextBox1.Text, Label1.Text, false) == 0)
{
  //Matching
}
else
{
  //Not Matching
}
 
Share this answer
 
Comments
_Zorro_ 5-Aug-11 9:44am    
This is not a best practice. Compare should only be use for sorting.
You should use Equals instead and specify the StringComparisonOptions.

See this for more info:
http://msdn.microsoft.com/en-us/library/dd465121.aspx
RaisKazi 5-Aug-11 9:59am    
_Zorro_ - I gone through your provided link. Its useful and your point is valid too. Take my 5!
_Zorro_ 5-Aug-11 10:01am    
Thanks!
And don't forget, plz make practicle and mail it to him, its an request, got it? :)
RaisKazi 5-Aug-11 10:03am    
:))) CodeProject does sends auto emails for every comment/answer.
prdpvts 5-Aug-11 9:51am    
its not making possible by me plz make practicle for it and mail me to prdpvts@gmail.com its an request
if(textbox1.text== label1.text)
{

statements;
}
else

{
statements;
}
 
Share this answer
 
Comments
Christian Graus 8-Aug-11 1:19am    
What the hell is the point in posting the same answer other people posted two days ago, esp for a user who is obviously too stupid to follow directions ?
Manfred Rudolf Bihy 12-Aug-11 10:57am    
Copy cat! Reported as abuse.
This answer has no added value over the others.
fjdiewornncalwe 16-Aug-11 14:18pm    
Same reason as the above two members. I would suggest that you begin to take the advice of the more senior members. They are trying to help you and you seem to be ignoring their suggestions.

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