无法从C#中的下拉选定项中检索数据

问题描述

我想从存在于ASPxGridview内的下拉列表中检索数据,我有"FindControl"可以检索数据但没有任何变化为什么它对我来说真的很令人困惑?

我的下拉菜单未进行硬编码绑定。我相信所有下拉列表都与web method绑定,因此我无法检索数据。

还有其他方法可以实现它吗?

ASPX

  <dx:ASPxGridView ID="dgDepots" runat="server" ClientInstanceName="grid"
        Width="100%"
        OnRowDeleting="dgDepots_RowDeleting" OnRowInserting="dgDepots_RowInserting" OnRowUpdating="dgDepots_RowUpdating">
        <Columns>
            <dx:GridViewDataColumn FieldName="LocationCode" VisibleIndex="0">
                <EditItemTemplate>
                  <asp:DropDownList ID="ddlLocation" runat="server"></asp:DropDownList>
                </EditItemTemplate>
            </dx:GridViewDataColumn>
</Columns>

.cs

插入事件时

protected void dgDepots_RowInserting(object sender,DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {

      e.Cancel = false;
      DropDownList list = (DropDownList)dgDepots.FindEditRowCellTemplateControl((GridViewDataColumn)dgDepots.Columns["LocationCode"],"ddlLocation") as DropDownList;
      //var Location = dgDepots.FindControl("ddlLocation") as DropDownList; // I tried this way but no change
      sqlCommand cmd = new sqlCommand("SP_Insert_Line_Item",con);
      cmd.Parameters.AddWithValue("@LocationCode",list.Selectedindex); 
      con.open();
      int i = cmd.ExecuteNonQuery();
      con.Close(); }

如我所见,调试时(即使在“选定字段”之后)

list.SelectedItem.Value is null
list.Selectedindex is showing -1

添加

<asp:ListItem Text="Select" Value="Select"></asp:ListItem>

list.Selectedindex is showing 0

我在ASPX中使用内容占位符可能会导致?不确定

这表示其未选择下拉值

我要绑定下拉列表,但是在服务器端调试时(Data SourceDataTextFieldDataValueField所有字段为空)[使用webMethod绑定下拉列表]为什么??

Please click This

Please click (binded all drop down list)

NullReferenceException

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)