简体   繁体   English

在Gridview中的下拉列表

[英]Drop Down List In A Gridview

I have a GridView , inside the GridView I have a template field and inside that, a drop down list. 我有一个GridView ,里面GridView我有一个模板领域和内部的,一个下拉列表中。

<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="Hello" runat="server">
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

I want to databind the GridView but how do I make the drop down list change its value to according to the information I gave it while databinding? 我想对GridView进行数据绑定,但是如何使下拉列表根据数据绑定时提供的信息将其值更改为?

Im used to using DataField in bound fields 我曾经在绑定字段中使用DataField

<asp:BoundField HeaderText="Hello" DataField="HelloDB" />

All you have to do is tap into the OnRowDataBind event of the GridView . 您所要做的只是点击GridViewOnRowDataBind事件。 Within that, you can use FindControl() to get the drop down, cast it as a DropDown , then set the value. 在其中,您可以使用FindControl()获取下拉列表,将其DropDownDropDown ,然后设置值。

This event is called when each row is databound, so each dropdown would be updated. 当每一行都是数据绑定时,将调用此事件,因此每个下拉列表都会被更新。

Microsoft provides a walk-through on this. Microsoft为此提供了一个演练

and quick Bing search comes up with many other articles and how-to's. Bing快速搜索附带了许多其他文章和操作方法。

Example: 例:

protected void MethodName(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
    {
     DropDownList Hello = e.Row.FindControl("Hello") as DropDownList;
     //here you can bind the dropdown list.

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM