如何按功能插入值下拉列表项. [英] how to insert value dropdownlist Item by function.

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

问题描述


我是shailendra singh.谁能帮我.

我想按功能在下拉列表项上添加值,但我想按功能在下拉列表的顶部选择关键字.

我的以下功能.

Hi
I am shailendra singh. Can any one help me.

I want to add value on dropdownlist Item by function but I want show on top select key word in dropdownlist, by function.

My following Function.

public void FillReportType()
    {
        con.Open();
        drpregtype.Items.Insert(0, new ListItem("--Select---", "----"));
        drpregtype.SelectedIndex = 0;
        da = new SqlDataAdapter("Select ResultType from tblresultType order by id ", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        drpregtype.DataSource = dt;
        drpregtype.DataValueField = "ResultType";
        //drpregtype.Items.Add.Add(new ListItem("--", "Select"));
        //drpregtype.Items.Insert(0, new ListItem("--Select---", "----"));
        //drpregtype.SelectedIndex = 0;
         drpregtype.DataBind();
        con.Close();
    }


感谢Advance.


Thanks for Advance.

推荐答案

Hello Shailendra,

只需修改您的代码:
Hello Shailendra,

just modifying your code :
public void FillReportType()
    {
        con.Open();
       
        drpregtype.SelectedIndex = 0;
        da = new SqlDataAdapter("Select ResultType from tblresultType order by id ", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        drpregtype.DataSource = dt;
        drpregtype.DataValueField = "ResultType";
        //drpregtype.Items.Add.Add(new ListItem("--", "Select"));
        //drpregtype.Items.Insert(0, new ListItem("--Select---", "----"));
        //drpregtype.SelectedIndex = 0;
         drpregtype.DataBind();
        drpregtype.Items.Insert(0, new ListItem("--Select---", "0"));
        con.Close();
    }



希望这会为您提供所需的解决方案.
如果有帮助,请不要忘记将其标记为答案. :)



Hope this will give you solution, that you want.
Don''t forget to mark as answer if it helps. :)




您有两种选择:

1.将选择"条目添加到数据源(作为第一项),然后将下拉列表绑定到该数据源

或2.手动将项目添加到下拉列表,而不将其绑定到数据源.

您不能添加项目并期望它同时显示并绑定数据源.

除此之外,您还应该删除视图中的sql.

希望这对您有帮助
Hi,

you have two options:

1. Add the "Select" entry to your datasource (as first item) and then bind your dropdown to that datasource

or 2. Add your items manually to your dropdownlist without binding it to a datasource.

You cannot add an item and expect it to be shown und bind a datasource at the same time.

In addition to that you should get rid of the sql in your view.

Hope this helps


我同意上述解决方案

命名列表项--- select ---
并选择为

i agree with above solutions

make listitem of name ---select---
and make it as selected

ListItem l1 = new ListItem("----Select----", "none");
ListItem l2 = new ListItem("abc", "abc");
ListItem l3 = new ListItem("xyz", "xyz");
DropDownList1.Items.Add(l1);
DropDownList1.Items.Add(l2);
DropDownList1.Items.Add(l3);
l1.Selected = true;



一切顺利



all the best


这篇关于如何按功能插入值下拉列表项.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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