views:

60

answers:

2

In my ASP.NET application, I have a GridView. For a particular field in this GridView, I've added an EditItemTemplate with a DropDownList. However, if the value of the field is "X", then I want to just display a label instead of the DropDownList. So how can I programatically check the field value, then decide which control to display?

Here is my EditItemTemplate:

<EditItemTemplate>

<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
    DataSourceID="ODSTechLvl" DataTextField="Level_Name"
    DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>

</EditItemTemplate>

If the value of Level_ID is "X", then I want to use:

<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>

instead of the DropDownList.

I tried embedding an if statement before the DropDownList to check Eval("Level_ID"), but that doesn't seem to work. Any thoughts?

A: 

Here is something that will work for ASP.Net.

You could create an RowDataBound event and hide the label or the DropDownList

<asp:GridView id="thingsGrid" runat="server" OnRowDataBound="thingsGrid_RowDataBound"

... > ...

and in your code behind:

protected void thingsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var boundData = e.Row.DataItem;
            ...
            if (boundDataMeetsCondition)
            {
                e.Row.Cells[4].FindControl("editThingDropDownList").Visible = false;
                e.Row.Cells[4].FindControl("editThingLabel").Visible = true;//*
            }
            else
            {
                ...    
            }
        }
}

*note this is less than ideal because it hard codes the cell index, and the ID of the controls is a string that wont be checked until runtime. There are much more elegant ways to solve this problem in asp.net mvc.

the OnRowDataBound is a sledge hammer that will give you full access to your grid, page methods, and your entire application. In a very simple scenario you could also do it inline without involving the codebehind.

<asp:Label ID="Label1" runat="server" Visible='<%# Convert.ToBoolean(Eval("BooleanPropertyInData"))%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>                           

or

<asp:Label ID="Label1" runat="server" Visible='<%# Eval("PropertyInData").ToString()=="specialValue"%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>  

in the first inline approach, your data source has to expose such a property, and in the second you are hard coding your specialValue business logic into your presentation, which is also ugly and will lead to problems with maintainability.

Tion
A: 

Try this:

<EditItemTemplate>

<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
    DataSourceID="ODSTechLvl" DataTextField="Level_Name"
    DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'
    Visible='<%# Eval("Level_ID") != "X" %>'>
</asp:DropDownList>

<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'
    Visible='<%# Eval("Level_ID") == "X" %>'></asp:Label>

</EditItemTemplate>
Matthew Jones