<asp:TemplateField HeaderText="Category">
<InsertItemTemplate>
<asp:DropDownList runat="server"
DataField="CategoryID" DataSourceID="SqlDataSource2"
DataTextField="Name" DataValueField="CategoryID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
SelectCommand="SELECT Name, CategoryID FROM [tblCategory]">
</asp:SqlDataSource>
我正在尝试创建一个下拉列表,以提交所选字段的ID。只有提交表单后,我才能获得ID并在数据库中保留NULL值。
答案 0 :(得分:1)
我认为您正在网格视图的OnRowUpdating方法中使用此下拉列表。
首先,您需要给下拉列表一个ID,例如
<asp:DropDownList ID="ddlCategory" runat="server" DataField="CategoryID" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="CategoryID"> </asp:DropDownList>
然后,您将需要获取下拉参考,例如
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList category = GridView.Rows[e.Row.RowIndex].FindControl("ddlCategory") as DropDownList;
//then, use category to get Selected Value
//like, category.SelectedValue
// to submit your value to the database
}