如何用c#验证asp.net中的dropdownlist [英] how to validate dropdownlist in asp.net with c#

查看:68
本文介绍了如何用c#验证asp.net中的dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行模式屏幕如下;





i有一个列表框和一个下拉列表。



在列表框中所有学生姓名将从数据库中显示



将在下拉列表年显示。



在运行模式下,用户从列表框中选择学生姓名。



如果使用没有选择下拉列表,那个时间显示消息选择年份。



为什么我如何使用C验证下拉列表





请帮帮我。



问候,

Narasiman P.

解决方案

绑定下拉列表时添加下面的行

 dropdownlist1.Items.Insert( 0  选择); 



对于验证,您可以在按钮点击下面写下代码

(dropdownlist1.SelectedItem.Text.Trim()== < span class =code-string>选择)
{
lblmsg.Text = 请选择年份;
dropdownlist1.Focus();
return ;
}


用户应从下拉列表中选择意味着您可能会将下拉列表中的第一项显示为选择...并且您将拥有页面中的搜索或显示按钮。



在这种情况下,您可以拥有一个bool变量并在Dropdownlist选择更改事件中更改其值...因此,每当用户点击搜索按钮时,检查bool值并根据您的要求显示警告


Run mode screen as follows;


i have one list box and one drop down list.

in list box all the student name will display from the database

in the drop down list year will display.

in the run mode user select the student name from the listbox.

if the use did not select the dropdown list, that time shows the message select the Year.

for that how can i validate the dropdownlist using C#


please help me.

Regards,
Narasiman P.

解决方案

While binding dropdownlist add the line below

dropdownlist1.Items.Insert(0, "Select");


And for validation you can write below code in button click

if (dropdownlist1.SelectedItem.Text.Trim() == "Select")
        {
            lblmsg.Text = "Please Select Year";
            dropdownlist1.Focus();
            return;
        }


User should select from dropdownlist means you may be displaying the first item in dropdownlist as "Select"... and you will have a "Search" or "Show" button in your page.

In that case, you can have a bool variable and change its value inside Dropdownlist selection changed event... So whenever user clicks the "Search" button check the bool value and display an alert based on your requirement


这篇关于如何用c#验证asp.net中的dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆