当前位置:首页 > ASP.NE技巧:GridView绑定DropdownList

ASP.NE技巧:GridView绑定DropdownList

点击次数:974  更新日期:2010-12-28
\n

绑定下拉菜单,显示的时候是一个label,在点编辑的时候才显示出下拉菜单来


\n

html代码部分:


\n

<asp:TemplateField HeaderText=”所属大类” SortExpression=”goodsType”>
<EditItemTemplate>
<asp:DropDownList ID=”DropDownList1″ runat=”server”></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”Label2″ runat=”server” Text=’<%# Bind(“goodsType”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
CS代码部分


\n


\n

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList goodsType = (DropDownList)e.Row.FindControl(“DropDownList1″);
if (goodsType != null)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProviderConnectionString"].ConnectionString);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(“select * from goodsType”,con);
da.Fill(ds,”gt”);
goodsType.DataSource = ds.Tables["gt"];
goodsType.DataTextField = “goodsType”;
goodsType.DataValueField = “goodsId”;
goodsType.DataBind();
}
}

\n