如何以編程方式設置ASP。NET Web Forms下拉列表selecteditem和selectedindex

[英]How to programmatically set a ASP.NET Web Forms dropdownlist selecteditem and selectedindex


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:

我看過很多關於這個話題的帖子,答案幾乎是一樣的。要以編程方式設置selecteditem,請使用以下代碼:

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.

我的情況略有不同。我試圖在gridview中設置下拉列表的值。

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

我可以填充dropdownlist,但不能設置selecteditem或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().

lblSubject由GetControlData()中的另一個查詢填充。

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

調試器到達ddlSubject.Items時。FindByValue代碼,我得到了NullReferenceException,即使lblSubject有一個值。

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

我想知道是否需要更改加載數據的gridview事件。

1 个解决方案

#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.

我認為這里你可能需要的是FindByText,而不是FindByValue。項目的值將是subject列,而項目的文本將與標簽的文本匹配。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/04/10/f9993dc9174f30fd1561f1227091f5c1.html



 
  © 2014-2022 ITdaan.com 联系我们: