如何验证asp.net中的下拉列表 [英] how to validate the dropdownlist in asp.net

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

问题描述

我有两个单选按钮如下



ByWord(radiobutton1)ByCategory(radiobutton2)



然后我有一个文本框和一个下拉列表和一个搜索按钮





当我点击radiobutton1时,textbox1可见true,当我点击radiobutton2 dropdonwlist visible true。







如果假设用户选择radiobutton2而无需选择下拉列表并单击搜索按钮。



i想要将消息显示为请选择类别。





我该怎么办。





我的搜索按钮代码如下



protected void Button1_Click(object sender,EventArgs e)

{

try

{

if(radiobutton1.Checked == true)

{

Sql =select Category,Quest离子,答案来自Tb_Staff_Doubt_Register问题如'%'+ Txtsearch.Text +%'或答案如'%'+ Txtsearch.Text +%';

Dr = SCon.ReadSql(Sql );

GridView1.DataSource = Dr;

GridView1.DataBind();

GridView1.Visible = true;

}



if(dropdownlist.selecteditem.Text.ToString()。Trim()==选择)

{

LblErr.Text =请选择类别;

返回;

}



if(radiobutton2.Checked == true)

{

Sql =选择类别,问题,答案来自Tb_Staff_Doubt_Register Where Category ='+ ddlcategory.SelectedItem。文字+';

Dr = SCon.ReadSql(Sql);

GridView1.DataSource = Dr;

GridView1.DataBind();

GridView1.Visible = true;

}







}

catch(例外情况)

{

LblErr.Text = ex .ToString();

返回;

}





但是我的上面的代码不起作用。



当我运行并点击radiobutton1(ByWord)并点击搜索按钮



消息显示请选择类别。



i想要消息,请在用户选择radiobutton2时选择类别,不选择下拉列表并单击搜索按钮那个时候只有留言显示请选择类别。



我该怎么办请帮帮我。



问候,

narasiman P.

i have two radio button as follows

ByWord(radiobutton1) ByCategory(radiobutton2)

Then i have one textbox and one dropdownlist and one search button


When i click the radiobutton1 the textbox1 visible true and when i click the radiobutton2 dropdonwlist visible true.



if suppose user select the radiobutton2 witout selecting the dropdownlist and click the search button.

i want to show the message as "please select the category".


for that how can i do.


My search button code as follows

protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (radiobutton1.Checked == true)
{
Sql = "select Category,Questions,Answers from Tb_Staff_Doubt_Register Where Questions like '%" + Txtsearch.Text + "%' or Answers like '%" + Txtsearch.Text + "%' ";
Dr = SCon.ReadSql(Sql);
GridView1.DataSource = Dr;
GridView1.DataBind();
GridView1.Visible = true;
}

if(dropdownlist.selecteditem.Text.ToString().Trim() == "Select")
{
LblErr.Text = " please Select the category";
return;
}

if (radiobutton2.Checked == true)
{
Sql = "select Category,Questions,Answers from Tb_Staff_Doubt_Register Where Category = '" + ddlcategory.SelectedItem.Text + "'";
Dr = SCon.ReadSql(Sql);
GridView1.DataSource = Dr;
GridView1.DataBind();
GridView1.Visible = true;
}



}
catch (Exception ex)
{
LblErr.Text = ex.ToString();
return;
}


but my above code is not working.

when i run and click the radiobutton1(ByWord) and click the search button

the message shows please Select the category.

i want he message, please select the category when user select the radiobutton2 and without selecting the dropdownlist and click the search button that time only message shows please Select the category.

for that how can i do please help me.

regards,
narasiman P.

推荐答案





请找到修改后的代码

Hi,

Please find the modified code
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (radiobutton1.Checked == true)
{
Sql = "select Category,Questions,Answers from Tb_Staff_Doubt_Register Where Questions like '%" + Txtsearch.Text + "%' or Answers like '%" + Txtsearch.Text + "%' ";
Dr = SCon.ReadSql(Sql);
GridView1.DataSource = Dr;
GridView1.DataBind();
GridView1.Visible = true;
}
else if (radiobutton2.Checked == true)
{
  if(dropdownlist.selectedIndex > 0) // if any value is selected in dropdown then index value will be greater 0
  {
     Sql = "select Category,Questions,Answers from Tb_Staff_Doubt_Register Where Category = '" +   ddlcategory.SelectedItem.Text + "'";
     Dr = SCon.ReadSql(Sql);
     GridView1.DataSource = Dr;
     GridView1.DataBind();
     GridView1.Visible = true;
  }
  else 
  {
     LblErr.Text = " please Select the category";
     return;
  }
 }
}
catch (Exception ex)
{
LblErr.Text = ex.ToString();
return;
}



希望它有所帮助

问候,Mahe


Hope it helps
Regards,Mahe


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

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