【问题标题】:Gridview RowCommand event with DropDownList is not working带有 DropDownList 的 Gridview RowCommand 事件不起作用
【发布时间】:2023-03-18 04:57:01
【问题描述】:

我创建了一些 DropDownLists,这些 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 将取回相关列/行所具有的任何值:

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 列/行具有值时有效,但当它是“空白”/“空”/“空”时无效。我真的不知道如何解决它?

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

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

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

这可能吗?

【问题讨论】:

  • 你的问题是什么?
  • 再做一件事; GridVIew 里面是 DDL 吗?
  • 问题解决了吗?
  • 是的! VDWWD 回答有帮助

标签: c# asp.net gridview dropdown rowcommand


【解决方案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>

【讨论】:

    【解决方案2】:

    不就是检查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;
        }
    }
    

    【讨论】:

    • 嗨,第一个例子没有工作我仍然得到错误:row.Cells[10].Text = " "
    • 第二个示例确实有效,但是如果我已经在更改请求者中选择了一个值,例如“John”,那么 DDL 将不会恢复为“更改请求者”的默认值,而是保持为“John” “?
    • 是的,DDL 在 gridview 之外
    • 我已经更新了我的答案。但是弄清楚您可能需要如何自己稍微调整代码将在未来的编程问题中派上用场。
    • 没问题。但是请记住,使用这样的单元格值会产生奇怪的结果。字符串值不是问题,但是当涉及到日期和小数时,最好使用源数据,因为这些类型会在单元格文本中转换为字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多