使用DropDownList的Gridview RowCommand事件不起作用

时间:2022-08-06 14:45:15

I have created a few DropDownLists that get populated with data from a database. The first lines of the Dropdownlists are ListItem:

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

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

使用DropDownList的Gridview RowCommand事件不起作用

I also have a GridView that has a RowCommand event on Select button.

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

使用DropDownList的Gridview RowCommand事件不起作用

When I press Select the DropDownLists will get back whatever value that the respected column/row has:

当我按下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;
    }
}

This works when Change Request column/row in the GridView has value but not when it's "white-space" / "Empty" / "Null". I do not really know how to fix it?

当GridView中的Change Request列/行具有值但不是“white-space”/“Empty”/“Null”时,这种方法有效。我真的不知道如何解决它?

I would like to be able to do something like:

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

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

However I would only want this in the background because I would like to have empty row in GridView but on RowCommand Select should understand that empty means ListItem value.

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

Is this possible?

这可能吗?

1 个解决方案

#1


5  

Would it not simply be checking if the value in Cell 10 is empty?

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

Assuming ddlChange_Requestor is a DropDown outside the GridView.

假设ddlChange_Requestor是GridView之外的DropDown。

And if you are unsure if the cell value exists in the DLL, you can do a check first.

如果您不确定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


5  

Would it not simply be checking if the value in Cell 10 is empty?

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

Assuming ddlChange_Requestor is a DropDown outside the GridView.

假设ddlChange_Requestor是GridView之外的DropDown。

And if you are unsure if the cell value exists in the DLL, you can do a check first.

如果您不确定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;
    }
}