带有 DropDownList 的 Gridview RowCommand 事件不起作用

Gridview RowCommand event with DropDownList is not working

我已经创建了一些 DropDownLists,它们填充了来自数据库的数据。下拉列表的前 ListItem:

<asp:DropDownList ID="ddlChange_Requestor" runat="server" AppendDataBoundItems="True"  CssClass="ddlChange_Requestor">
    <asp:ListItem>Change Requestor</asp:ListItem>

我还有一个 GridView,它在 Select 按钮上有一个 RowCommand 事件。

当我按下 Select 时,DropDownLists 将取回受尊重的 column/row 具有的任何值:

protected void gwActivity_RowCommand(object sender, GridViewCommandEventArgs e)
{
    {
        GridViewRow row = ((e.CommandSource as Control).NamingContainer as GridViewRow);
        txtActivity.Text = row.Cells[2].Text;
        ddlChange_Requestor.SelectedValue = row.Cells[10].Text;
    }
}

这在 GridView 中的 Change Request column/row 有值时有效,但在 "white-space" / "Empty" / [=47= 时无效].我真的不知道如何解决它?

我希望能够做类似的事情:

ddlChange_Requestor.SelectedValue = isnullOrwhitespace(row.Cells[10].Text , "Change Requestor");

但是我只想在后台使用它,因为我想在 GridView 中有空行但是在 RowCommand Select 上应该明白空意味着 ListItem值。

这可能吗?

不就是检查单元格10中的值是否为空吗?

if (!string.IsNullOrEmpty(row.Cells[10].Text) && row.Cells[10].Text != "&nbsp;")
{
    ddlChange_Requestor.SelectedValue = row.Cells[10].Text;
}
else
{
    ddlChange_Requestor.SelectedIndex = 0;
}

假设 ddlChange_Requestor 是 GridView 之外的 DropDown。

如果您不确定DLL中是否存在该单元格值,您可以先进行检查。

if (!string.IsNullOrEmpty(row.Cells[10].Text))
{
    string value = row.Cells[10].Text;

    if (ddlChange_Requestor.Items.Cast<ListItem>().Any(x => x.Value == value))
    {
        ddlChange_Requestor.SelectedValue = value;
    }
    else
    {
        ddlChange_Requestor.SelectedIndex = 0;
    }
}
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-2343935067532705",
          enable_page_level_ads: true
     });
</script>