简体   繁体   English

如何避免从下拉列表中选择?

[英]How to avoid select from drop down list?

I don't know how to avoid select text listed when I am populating drop down at run time. 我不知道如何避免在运行时填充下拉列表时选择列出的文本。

    DataRow row = ds.Tables[0].NewRow();
    row["cabTypeId"] = "0";
    row["cabType"] = "Select";
    ds.Tables[0].Rows.InsertAt(row, 0);
    ddlCabSize.DataTextField = ds.Tables[0].Columns["cabType"].ToString();
    ddlCabSize.DataValueField = ds.Tables[0].Columns["cabTypeId"].ToString();
    ddlCabSize.DataSource = ds.Tables[0];
    ddlCabSize.DataBind();

If i will select drop down list means "select" should not show to dropdownlist. 如果我将选择下拉列表,则意味着“选择”不应显示在下拉列表中。 Help me that how to do it? 请帮我该怎么做?

For the need on insert, you can add this code bu using Add Method 如果需要插入,可以使用“ Add Method添加此代码

ddlCabSize.DataBind();
ddlCabSize.Items.Add(new ListItem("Select .."));

link : http://msdn.microsoft.com/fr-fr/library/e7s6873c.aspx 链接: http//msdn.microsoft.com/fr-fr/library/e7s6873c.aspx

Note : You can also use Insert Method 注意:您也可以使用插入方法

You must add Columns to your tables 您必须将列添加到表中

DataColumn dc = new DataColumn ("Name", Type);
dt.Columns.Add(dc);

So : 因此:

DataColumn dc = new DataColumn ("cabType", typeof(string));

Use below code. 使用下面的代码。

ddlCabSize.DataSource = ds.Tables[0];
ddlCabSize.DataTextField = ds.Tables[0].Columns["cabType"].ToString();
ddlCabSize.DataValueField = ds.Tables[0].Columns["cabTypeId"].ToString();
ddlCabSize.DataBind();
ddlCabSize.Items.Insert(0, new ListItem("Select Below","0"));

Not sure what you are asking. 不知道你在问什么。

Try swapping your values around. 尝试交换您的价值。

ddlCabSize.DataTextField = ds.Tables[0].Columns["cabTypeId"].ToString();
ddlCabSize.DataValueField = ds.Tables[0].Columns["cabType"].ToString();

If i get your question correctly, you want t remove the item with the text "Select" or value = "0" from the dropdown list once the selection is changed 如果我正确地提出了您的问题,则一旦更改选择,您就不想从下拉列表中删除文本为“选择”或值为“ 0”的项目

You can use jquery to do this very easily, for that you need to know the clientId of your dropdown list which is very easy to get using the ddlCabSize.ClientID property 您可以使用jquery轻松做到这一点,因为您需要知道下拉列表的clientId,使用ddlCabSize.ClientID属性很容易获得

add jquery reference to your page like this 像这样向页面添加jQuery引用

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

and copy the script code 并复制脚本代码

<script type="text/javascript">
 $(document).ready(function () {
     $("#<#= ddlCabSize.ClientID #>").change(function () {
         $("#<#= ddlCabSize.ClientID #> option[value='0']").remove();
     });
 });
</script>

See the fiddle for more info 查看小提琴以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM