代码之家  ›  专栏  ›  技术社区  ›  tbischel

网格视图下拉框

  •  1
  • tbischel  · 技术社区  · 16 年前

    我正在尝试使用GridView在ASP.NET中显示组件列表。我正在尝试同时对其进行编辑。其中一列是当用户编辑行时应从列表中选择的字符串。

    所以我尝试了以下方法:

    1. 将boundField行转换为itemTemplate
    2. 在网格视图的模板窗口中添加Dropbox
    3. 将SelectedItem绑定到字符串

    此时,我会得到一个错误,因为Dropbox中没有设置列表项。所以我想我想知道的两件事是:

    1. 如何将Dropbox中的项目分配给动态创建的选项列表?
    2. 如何使Dropbox仅在编辑行时显示?

    好的,我在Visual Studio中发现了“EditItemTemplate”字段,它回答了2。

    现在我发现Dropbox有一个数据源字段,它可以链接到数据对象中的属性,并保存选项列表。

    1 回复  |  直到 16 年前
        1
  •  0
  •   Kelsey    16 年前

    在你 DropDownList 您可以指定 OnDataBinding 事件,然后我们的事件来填补 下拉框 使用自定义数据。

    例子:

    <Columns>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:DropDownList ID="yourDropDownList" runat="server"
                    DataTextField="YourTextFieldName" DataValueField="YourValueFieldName"
                    OnDataBinding="yourDropDownList_DataBinding"></asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    

    然后在代码背后实现OnDatabinding:

    protected void yourDropDownList_DataBinding(object sender, System.EventArgs e) 
    { 
        DropDownList ddl = (DropDownList)(sender);
        // GetMyDropDownListData should return cached data so your not hitting your DB
        // each time. You can customize the data for each row here. Use the Eval command
        // to access the current rows databound values.
        ddl.DataSource = GetMyDropDownListData();
        ddl.DataBind();  // Now all the options will be loaded
    
        // Set the current field's selected value
        ddl.SelectedValue = Eval("YourSelectedValueFieldName").ToString();
    }
    

    希望有帮助。

    推荐文章