GridView绑定DropdownList
我现在做的是在页面加载的时候,就绑定DropdownList,然后也可以更新,可是我想要的效果是加载的时候,显示的是一个label,点编辑的时候这列才显示DropdownList,我现在这个如果显示的时候是label就出错。请问怎么做,我的代码如下:
------------------------------------------
HTML
<asp:GridView ID= "GridView1 " runat= "server " OnRowCancelingEdit= "GridView1_RowCancelingEdit " OnRowUpdating "GridView1_RowUpdating " OnRowEditing= "GridView1_RowEditing " AutoGenerateColumns= "False " DataKeyNames= "EpId " OnRowDataBound= "GridView1_RowDataBound ">
<Columns>
<asp:TemplateField HeaderText= "所属大类 " SortExpression= "goodsType ">
<EditItemTemplate>
<asp:DropDownList ID= "DropDownList1 "runat= "server "> </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID= "DropDownList1 " runat= "server "/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "操作 " ShowHeader= "False ">
<EditItemTemplate>
<asp:LinkButton ID= "LinkButton1 " runat= "server " CausesValidation= "True " CommandName= "Update "Text= "更新 "> </asp:LinkButton>
<asp:LinkButton ID= "LinkButton2 " runat= "server " CausesValidation= "False " CommandName= "Cancel "
Text= "取消 "> </asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID= "LinkButton1 " runat= "server " CausesValidation= "False " CommandName= "Edit "Text= "编辑 "> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
------------------------
CS代码中:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList goodsType = (DropDownList)e.Row.FindControl( "DropDownList1 ");
con = new SqlConnection(ConfigurationManager.ConnectionStrin