使用DropDownList的Gridview RowCommand事件无效

时间:2018-03-29 10:49:16

标签: c# asp.net gridview dropdown rowcommand

我创建了一些DropDownLists,它们填充了数据库中的数据。下拉列表的第一个ListItem

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

enter image description here

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

enter image description here

当我按选择时,DropDownLists将返回受尊重的列/行所具有的任何值:

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中的更改请求列/行具有值但不适用于&#34; white-space&#34; /&#34;空&#34; /&#34; Null&#34;。我真的不知道如何解决它?

我希望能够做到这样的事情:

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

但是我只想在后台使用它,因为我想在GridView中有空行,但在RowCommand 选择应该理解空值意味着ListItem值。

这可能吗?

2 个答案:

答案 0 :(得分:6)

不是简单地检查Cell 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;
    }
}

答案 1 :(得分:-1)

<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>
相关问题