2

我在 gridview 模板字段中有一个下拉菜单。

<asp:templatefield headertext="Bill Period">
<itemtemplate>
<asp:dropdownlist runat="server" id="cboBillPeriod"></asp:dropdownlist>
</itemtemplate>
</asp:templatefield>

我想填充下拉列表我能做到吗?任何人都可以帮助我。

4

4 回答 4

4

您可以在 GridView 事件中绑定下拉列表,OnRowDataBound如下所示:

网格视图:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="ContactName" />
        <asp:TemplateField HeaderText = "Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible = "false" />
                <asp:DropDownList ID="ddlCountries" runat="server">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后:

在方法的帮助下,FindControl您将能够获得下拉控件,然后您可以使用该控件。

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row
        DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
        ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
        ddlCountries.DataTextField = "Country";
        ddlCountries.DataValueField = "Country";
        ddlCountries.DataBind();

        //Add Default Item in the DropDownList
        ddlCountries.Items.Insert(0, new ListItem("Please select"));

        // Select the Country of Customer in DropDownList
        string country = (e.Row.FindControl("lblCountry") as Label).Text;
        ddlCountries.Items.FindByValue(country).Selected = true;
    }
}
于 2013-06-07T07:10:45.287 回答
1

您可以将其用于您的网格视图中的下拉菜单。

  <asp:TemplateField HeaderText="Item Condition" HeaderStyle-Width="80px" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="40px>
          <ItemTemplate>
            <asp:DropDownList ID="ddlConditions" runat="server" >  
            </asp:DropDownList>
          </ItemTemplate>
        </asp:TemplateField>

在您的网格“RowDataBound”事件下,您将使用下拉列表 ID 将您的下拉列表绑定到后面的代码中。

DropDownList ddlConditions2 = (e.Row.FindControl("ddlConditions") as DropDownList);
                DataTable dt = _reader.GetDataTableByCommandFromStoredProc("getYourDropdownData");
                ddlConditions2.DataSource = dt;
                ddlConditions2.DataTextField = "ConditionName";
                ddlConditions2.DataValueField = "Id";
                ddlConditions2.DataBind();
                ddlConditions2.Items.Insert(0, new ListItem("--Select--", "0"));
于 2017-12-30T10:46:18.420 回答
0

您必须使用RowDataBound网格视图的事件。有关更多信息,请检查该链接

于 2013-06-07T07:09:19.567 回答
-3

如果Rowdatabound出现Gridview,请尝试绑定您的下拉菜单。

于 2013-06-07T07:13:35.557 回答