Create Login Form in ASP.Net, Login Name match with Items of DropDownList control in ASP.Net

Here, we are going to learn match textbox control text value with any Items of DropDownList control,  if match display message login success other wise login fail.

Example: Here we have one DropDownList control which have five Items named “Meera”, “Academy”, “ASP”, “PHP” and “SQL”. and we have one TextBox Control, here we match the TextBox control Text Value with any Items of DropDownLsit  control then message display success other wise fail.

for this ASP.Net Example we need to use FOR Loop her to solve this example.

First Design asp.net web page with HTML code:

<table style=”width: 416px”>
<tr>
<td style=”width: 111px; text-align: right”>
UserName :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”Drpusername” runat=”server” Width=”128px”>
<asp:ListItem>Meera</asp:ListItem>
<asp:ListItem>Academy</asp:ListItem>
<asp:ListItem>ASP</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
<asp:ListItem>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 111px; text-align: right”>
Enter UserName :</td>
<td style=”width: 100px; text-align: left”>
<asp:TextBox ID=”txtusername” runat=”server”></asp:TextBox></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 111px; text-align: right”>
</td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”btnlogin” runat=”server” Font-Bold=”True” Height=”32px” OnClick=”btnlogin_Click”
Text=”Login” Width=”80px” /></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 111px; text-align: right”>
</td>
<td colspan=”2″ style=”text-align: left”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True”></asp:Label></td>
</tr>
</table>

The ASP.Net Example of DropDownList Control output Design output is:

match textbox text with dropdownllist control any item in asp.net
match textbox text with dropdownllist control any item in asp.net

Write the below code on Login Button Click event at asp.net code behind page.

protected void btnlogin_Click(object sender, EventArgs e)
{
for (int i = 0; i < Drpusername.Items.Count; i++)
{
if (txtusername.Text == Drpusername.Items[i].Text)
{
Label1.Text = “Login Successfull !!”;
break;
}
Label1.Text = “Invalid USerName !!”;
}
}

 

If TextBox control will be blank then the error  message like Enter UserName  in below screen.

match textbox text with dropdownllist control any item in asp.net
match textbox text with dropdownllist control any item in asp.net

If your TextBox Text value not int the DropDownList control then error message like “Invalid UserName”.

match textbox text with dropdownllist control any item in asp.net
match textbox text with dropdownllist control any item in asp.net

If the TextBox Text Value match with any items of DropDownList control then success message display Login Successfull.

match textbox text with dropdownllist control any item in asp.net
match textbox text with dropdownllist control any item in asp.net

I hope this asp.net Example of DropDownList control will help you……

Leave a Reply

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