3

I have seen multiple posts regarding this subject and the answer is pretty much the same. To set the selecteditem programmatically use the following code:

DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;

My scenario is slightly different. I'm trying to set the value of a dropdownlist in a gridview.

I am able to populate the dropdownlist, but not able to set the selecteditem or selectedindex.

Gridview

<asp:GridView ID="gvSubject" runat="server"
    CssClass="table table-striped clientTblEnabled"
    OnRowDataBound="gvSubject_RowDataBound"
    AutoGenerateColumns="false" 
    OnPreRender="gvSubject_PreRender" 
    GridLines="Both" PageSize="50">
       <Columns>
           <asp:TemplateField HeaderText="Subject Date">
               <ItemTemplate>
                    <asp:Label ID="lblSubjectDate" runat="server" Text='<%# Bind("SubjectDateTime", "{0:MM/dd/yyyy}") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Subject">
               <ItemTemplate>
                    <asp:Label ID="lblSubject" runat="server" Text='<%# Bind("SubjectDesc") %>' Visible="false"></asp:Label>
                    <asp:DropDownList ID="ddlSubject" runat="server" CssClass="input-xlarge controls"></asp:DropDownList>
               </ItemTemplate>
           </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
             No Results found
        </EmptyDataTemplate>
</asp:GridView>

Populate dropdownlist

protected void gvSubject_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
                DropDownList ddlSubject = e.Row.FindControl("ddlSubject") as DropDownList;


                if (ddlSubject != null)
                {
                    DataSet ds = GetControlData("ddlSubject");
                    ddlSubject.DataSource = ds.Tables[8];
                    ddlSubject.DataTextField = "SubjectDesc";
                    ddlSubject.DataValueField = "SubjectID";
                    ddlSubject.DataBind();
                    ddlSubject.Items.FindByValue((e.Row.FindControl("lblSubject") as Label).Text).Selected = true;
                }

        }
    }

lblSubject is populated by another query in GetControlData().

When the debugger gets to the ddlSubject.Items.FindByValue code, I get a NullReferenceException even though lblSubject has a value.

I wonder if I need to change the gridview event for which I'm loading the data.

2
  • 2
    Is it (e.Row.FindControl("lblSubject") as Label) that is null, or the result of FindByValue? And are you certain that the text value of lblSubject (which you say is set by another query) does exist in the list of items that the drop down list gets populated by? Commented Apr 10, 2018 at 21:07
  • @MajorRefactoring The result of FindByValue is null. lblSubject does have a value and the value and case matches an item in ddlSubject. Commented Apr 10, 2018 at 21:34

1 Answer 1

2

I think what you're probably after here is FindByText, not FindByValue. The value of the item will be the SubjectID column, whereas the text of the item will match the text of the label.

Sign up to request clarification or add additional context in comments.

3 Comments

I just tried FindByText and I got the same result. I'm wondering if I need to put this in a different event. I'm calling this from the OnRowDataBound event.
Strange. If you break on that line (ddlSubject.Items.FindByValue((e.Row.FindControl("lblSubject") as Label).Text).Selected = true;) and inspect the value of the text property of the label, are you 100% certain that there is an item in the ddlSubject.Items list that has exactly the same text (including case)? I know you mentioned that you had checked that in your other comment, but it seems like that must be the issue - perhaps the items collection has not actually been filled?
This turned out to be a data integrity issue. There were trailing spaces for each item in the dropdownlist. So there wasn't a match. I've trimmed the item values and it works. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.