如何获取文本并将其放在下拉列表中.... [英] How to fetch the text and put it in dropdownlist....

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

问题描述

嗨朋友们......



我有一个ondatabound for dropdownlist总是插入第一项如下....

Hi friends...

I've a ondatabound for dropdownlist to always insert 1st item as follows ....

protected void Combo_Ondatabound(object sender, EventArgs e)
  {
      Combo.Items.Insert(0, new ListItem("Select", "0"));
  }



我有一个下拉列表作为过滤器...当我选择一个项目时,它正确地从数据库中获取并且没有' t落入Combo(下拉列表)。



我的编码,...




and I've a dropdownlist as a filter ... When I choose a item, it fetches from database correctly and doesn't fell into Combo(dropdownlist).

My Coding,...

Combo.SelectedItem.Text=dt.Rows[0]["Combo"].ToString();





它给出了一个错误...对象实例未设置....



It gives an error ... Object instance not set ....

推荐答案

可能很少有可能没有选择项目和 Combo.SelectedItem null dt 可能是 null ,你需要调试应用程序查找哪个对象一片空白。



如果你需要从数据表中绑定下拉列表,请尝试下面

there may be few possibilities like you may not have Selected Item and Combo.SelectedItem is null or dt may be null, You need to debug the application find which object is null.

if you need to bind dropdownlist from data table, try below
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TextCol, ValueCol FROM MyTable", con);
DataTable dt = new DataTable();
adapter.Fill(dt);
combo.DataTextField = "TextCol";
combo.DataValueField = "ValueCol";
combo.DataSource = dt;
combo.DataBind();
Combo.Items.Insert(0, new ListItem("Select", "0"));


可能是我们可以解决您的疑问,但首先要清楚地解释您的尝试以及您的预期输出是什么。

如果错误是没有设置对象实例。

这意味着当你的页面加载时,dropdownlist没有获得它必须显示在顶部的价值。



您可以在下拉列表代码下面尝试此代码



may be we can solve your doubt, but first clearly explain what u r trying and what is your expected output.
if error is Object instance not set.
this means when your page is load then dropdownlist is not getting value whihc it have to display on top.

you can try this code below your dropdown code ends

'Make the current month selected item in the list
            ddlmonth.Items.FindByText(MonthName(DateTime.Now.Month)).Selected = True





这里在findbytext()函数中你可以编辑你的代码。



here in findbytext() function you can edit your code.


这篇关于如何获取文本并将其放在下拉列表中....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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