【问题标题】:How do I access List items in order to change their values如何访问列表项以更改其值
【发布时间】:2017-07-20 23:36:46
【问题描述】:

我想使用与我的数据库不同的表动态地用文本和值填充下拉列表。我正在使用此代码:

    protected void MachineGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;
        var ddl = MachineGrid.Rows[index].FindControl("OSDropDownList") as DropDownList;
    }

访问下拉列表。然后我尝试使用以下两个代码 sn-ps 来修改列表项之一,但都给了我以下错误

"System.NullReferenceException: '对象引用未设置为对象的实例。"

代码 1:

        var li = ddl.SelectedItem;
        li.Text = "test";

代码 2:

        var li = ddl.Items[0];
        li.Text = "test";

谢谢!

【问题讨论】:

  • 你的意思是DataTextField和DataValueField?
  • 我不确定...我不这么认为

标签: c# sql asp.net drop-down-menu listitem


【解决方案1】:

使用一些可以 通过查询填充数据表并向下拉列表提供数据源。

yourDropDownList.DataSource= dt;
yourDropDownList.DataTextField="StringColumnName";
yourDropDownList.DataValueField="integerColumnName";
yourDropDownList.DataBind();

【讨论】:

  • 我第一次没有正确地提出我的问题。如果您现在查看它,我将不胜感激。
  • 试试这个 foreach (GridViewRow gvRow in YourGridViewID.Rows) { DropDownList yourDropDownList= (DropDownList)gvRow.FindControl("yourDropDownListID");字符串选择值 = yourDropDownList.SelectedValue; }
  • 这有点帮助,但仍然没有解决核心问题。我不想更改选定的字符串,而是想动态更改下拉列表下列表项中的所有字符串
猜你喜欢
  • 2020-05-02
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 2019-10-14
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多