Jon Harding
Jon Harding

Reputation: 4946

How to place a value in a dropdown if the database has a null field C#

I have a repeater that builds a dropdown menu. There is a field that places the URL in the value attribute. The field is nullable in the database, so for items that do not have a URL the value is empty. I need to replace that with something even if it is just '#' so that the validation works.

Mark-Up

<ItemTemplate>
        <option data-siteid="<%# DataBinder.Eval(Container.DataItem, "SiteID") %>" value="<%# DataBinder.Eval(Container.DataItem, "URL") %>">
            <%# DataBinder.Eval(Container.DataItem, "Name") %>
        </option>
</ItemTemplate>

Code Behind:

private void BindMakeList()
    {
        var makeList = this.repository.GetMakes();

        rptDropDown.DataSource = makeList;
        rptDropDown.DataBind();

    }

Upvotes: 0

Views: 238

Answers (2)

Amol Kolekar
Amol Kolekar

Reputation: 2325

Try using String.IsNullOrEmpty in Value field

<%# String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "SiteID")) ? "#": DataBinder.Eval(Container.DataItem, "SiteID")  %>

Upvotes: 0

James
James

Reputation: 82096

How about:

value="<%# DataBinder.Eval(Container.DataItem, "URL") ?? "#" %>"

Upvotes: 2

Related Questions