如何防止C#中DropDownlist中的重复 [英] How to prevent duplication in DropDownlist in C#

查看:108
本文介绍了如何防止C#中DropDownlist中的重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0下来投票最爱





当我点击搜索按钮时我有一个网页表单DropDownList必须选择Dataitems中的文本在下拉列表中可用但我的代码的问题是它插入重复的Dataitem文本而不是选择原始的Dataitem。以下是我的代码



 sqlChk = s.callingSP( < span class =code-string> SP_SearchCase,txtOfficeSerialNO.Text.Trim()); 
if (sqlChk.Tables [ 0 ]。Rows.Count > 0
{
divEmpMast.Style.Add( display block );

lblMsg.Visible = false ;

txtSNO.Text = sqlChk.Tables [ 0 ]。行[ 0 ] [ SNo]。ToString();

txtSending.Text = sqlChk.Tables [ 0 ]。行[ 0 ] [ DateOfSending]。ToString();
if string .IsNullOrEmpty(sqlChk.Tables [ 0 ]。行[ 0 ] [ FrmWhere]。ToString()))
{
// 编写自定义代码
sqlChk.Tables [ 0 ]。行[ 0 ] [ FrmWhere] = ;
}
else
ddlSubdistrict.SelectedItem.Text = sqlChk.Tables [ 0 ].Rows [ 0 ] [ FrmWhere ]。ToString();





请帮助

解决方案

你应该这样做...

  if (ddlSubdistrict.Items.Contains(ddlSubdistrict) .Items.FindByText(sqlChk.Tables [ 0 ]。行[ 0 ] [  FrmWhere]。ToString())))
{
ddlSubdistrict.Items.FindByText(sqlChk .Tables [ 0 ]。行[ 0 ] [ FrmWhere]。ToString())。Selected = true ;
}


嗨ankitsrist,



您正在尝试设置文本当前所选项目,如果当前所选项目不同,则会更改所选项目的显示文本。



而是需要为其设置SelectedValue如果你有下拉菜单。



看来你只有选择下拉列表的文本值,所以这里是你的问题的解决方案: -



<预郎= 使用Javascript> ddlSubdistrict.SelectedIndex = ddlSubdistrict.Items.IndexOf(ddlSubdistrict.Items.FindByText(sqlChk.Tables [<跨度类= 代码位> 0 ]。行[ 0 ] [ FrmWhere]。ToString()));





希望这对您有所帮助。


您好,



您还可以使用 dataview 在那里存储表格数据然后您就可以为数据选择不同的值。请点击此处的此链接



谢谢

Sisir Patro


0 down vote favorite


I have a web form when i click on search button DropDownList must select the text among the Dataitems available in dropdownlist but problem with my code is that it insert the duplicate Dataitem text instead of selecting original Dataitem. Following is my code

sqlChk = s.callingSP("SP_SearchCase", txtOfficeSerialNO.Text.Trim());
    if (sqlChk.Tables[0].Rows.Count > 0)
    {
        divEmpMast.Style.Add("display", "block");

        lblMsg.Visible = false;

        txtSNO.Text = sqlChk.Tables[0].Rows[0]["SNo"].ToString();

        txtSending.Text = sqlChk.Tables[0].Rows[0]["DateOfSending"].ToString();
        if (string.IsNullOrEmpty(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString()))
        {
            // Write your Custom Code
            sqlChk.Tables[0].Rows[0]["FrmWhere"] = "None";
        }
        else
           ddlSubdistrict.SelectedItem.Text=sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString();



Please help

解决方案

You should do like this...

if(ddlSubdistrict.Items.Contains(ddlSubdistrict.Items.FindByText(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString())))
{
    ddlSubdistrict.Items.FindByText(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString()).Selected = true;
}


Hi ankitsrist,

You are trying to set the text for the current selected item so if the current selected item is something different then it will change the display text for that selected item.

Rather you need to set the SelectedValue for the dropdown if you have that.

It seems you have the text value only to select the dropdown, so here is the solution for your problem :-

ddlSubdistrict.SelectedIndex = ddlSubdistrict.Items.IndexOf(ddlSubdistrict.Items.FindByText(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString()));



Hope this will definitely of help to you.


Hi,

You can also use a dataview for storing the table data there and then you can able to select the distinct values for the data. Please follow this link here.

Thanks
Sisir Patro


这篇关于如何防止C#中DropDownlist中的重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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