How to compare TextBox value Programmatically in ASP.Net

Compare two TextBox value on Button Clcik Event

- Here we learn compare two textbox value with each other on button click event in ASP.Net.

- We need to use here IF Condition in ASP.Net for comparing the Two Values of TextBox Control.

Write below code in Body Tag:-

<body>
    <form id=”form1″ runat=”server”>
    <table border=”1″ bordercolor=”#000000″ style=”z-index: 101; left: 280px; position: absolute;
            top: 296px”>
        <tr>
            <td colspan=”3″>
                <asp:Image ID=”Image2″ runat=”server” ImageUrl=”~/img/newimg.png” /></td>
        </tr>
        <tr>
            <td colspan=”3″ rowspan=”2″ style=”height: 21px; text-align: center”>
                <table>
                    <tr>
                        <td style=”width: 100px”>
                            Enter Value1 :</td>
                        <td style=”width: 100px”>
                            <asp:TextBox ID=”txtbox1″ runat=”server”></asp:TextBox></td>
                        <td style=”width: 100px”>
                        </td>
                    </tr>
                    <tr>
                        <td style=”width: 100px”>
                            Enate Value2 :</td>
                        <td style=”width: 100px”>
                            <asp:TextBox ID=”txtbox2″ runat=”server”></asp:TextBox></td>
                        <td style=”width: 100px”>
                            <asp:Button ID=”btncompare” runat=”server” OnClick=”btncompare_Click” Text=”Compare” /></td>
                    </tr>
                    <tr>
                        <td style=”width: 100px; height: 21px;”>
                        </td>
                        <td style=”width: 100px; height: 21px;”>
                            <asp:Label ID=”lblanswer” runat=”server” Font-Bold=”True”></asp:Label></td>
                        <td style=”width: 100px; height: 21px;”>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
        </tr>
    </table>
    </form>
</body>

write below code in Button Click Event

protected void btncompare_Click(object sender, EventArgs e)
{
if (txtbox1.Text == txtbox2.Text)
{

lblanswer.Text = “value match”;
txtbox1.Text = “”;
txtbox2.Text = “”;
}
else
{
lblanswer.Text = “dosn’t match”;
}
}

In below asp.net figure we can see the message dosn’t match as the value of both TextBox control are not same.

Compare TextBox value to other TextBox value

Compare TextBox value to other TextBox value

- The Below figure both the Value of TextBox control are same, so we get value match message on click event of Button control in ASP.Net TextBox Example.

Compare TextBox value to other TextBox value

Compare TextBox value to other TextBox value

Leave a Reply

Your email address will not be published. Required fields are marked *

7 + 4 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">