我无法绑定edititem模板中的下拉列表.当我尝试访问它时,我得到空引用.
我的设计:
<asp:TemplateField HeaderText ="Category">
<ItemTemplate >
<asp:Label ID="drpcategory" Text ='<%#Bind("category") %>' runat ="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="drpcategory1" AppendDataBoundItems="True" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
我的代码背后:
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv_table1.EditIndex = e.NewEditIndex;
DropDownList drpcategory1 = ((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1"));
//BindDropDown(drpcategory1);
dt = con.GetData("Select category_name from category");
String str = gv_table1.Rows[e.NewEditIndex].FindControl("drpcategory1").GetType().ToString();
//((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1")).DataSource = dt;
drpcategory1.DataSource = dt;
drpcategory1.DataTextField = "category_name";
drpcategory1.DataValueField = "category_name";
drpcategory1.DataBind();
this.setgrid();
}
Run Code Online (Sandbox Code Playgroud)
我试过在网上看,试了很多东西是徒劳的.我是asp新手.提前致谢.我希望只有当用户进入编辑模式时才会绑定下拉列表.
我对asp.net比较陌生,所以如果我的查询幼稚我的网格设计,请耐心等待:
<asp:GridView runat ="server" GridLines = "Both" DataKeyNames="book_id"AutoGenerateColumns ="false" CellPadding ="5" CellSpacing ="5" allowpaging="True" allowsorting="True"
ID="gv_table1" EmptyDataText ="No data exists" OnRowEditing="gv_RowEditing"
OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:BoundField HeaderText="Book ID" DataField="book_id">
</asp:BoundField>
<asp:BoundField DataField="book_name" HeaderText="Book Name">
</asp:BoundField>
<asp:BoundField DataField="author_name" HeaderText="Author Name">
</asp:BoundField>
<asp:BoundField DataField="publisher" HeaderText="Publisher">
</asp:BoundField>
<asp:BoundField DataField="year_edition" HeaderText="Year/Edition">
</asp:BoundField>
<asp:BoundField DataField="total_no" HeaderText="Total No">
</asp:BoundField>
<asp:BoundField DataField="available" HeaderText="Available">
</asp:BoundField>
<asp:BoundField DataField="tags" HeaderText="Tags">
</asp:BoundField>
<asp:BoundField DataField="fare" HeaderText="Fare">
</asp:BoundField>
<asp:BoundField DataField="state" HeaderText="State">
</asp:BoundField>
<asp:templatefield HeaderText ="Options">
<itemtemplate >
<asp:linkbutton id="btnEdit" runat="server" commandname="Edit" text="Edit" /> …Run Code Online (Sandbox Code Playgroud)