利用DropDownList与FormView操作数据库
拖入DropDownlist控件,设置AutoPostBack="True",数据源指向数据库某表,选择DataTextField和DataValueField,选择Select语句。
拖入FormView控件,数据源选择同一数据源,Select 设置WHERE字句关键字段指向DropDownlist的相应字段,同时在“高级”中设置允许“编辑、插入、删除”
msdn参考:http://msdn.microsoft.com/zh-cn/library/3fs4k4w4(VS.80).aspx
程序如下:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>利用DropDownList与FormView操作数据库</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
DataTextField="book_name" DataValueField="book_id">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:booksConnectionString %>"
SelectCommand="SELECT [book_id], [book_name] FROM [Code]"></asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="book_id" DataSourceID="SqlDataSource2" AllowPaging="True">
<EditItemTemplate>
book_id:
<asp:Label ID="book_idLabel1" runat="server" Text='<%# Eval("book_id") %>'></asp:Label><br />
book_name:
<asp:TextBox ID="book_nameTextBox" runat="server" Text='<%# Bind("book_name") %>'>
</asp:TextBox><br />
book_num:
<asp:TextBox ID="book_numTextBox" runat="server" Text='<%# Bind("book_num") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="更新">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
book_id:
<asp:TextBox ID="book_idTextBox" runat="server" Text='<%# Bind("book_id") %>'>
</asp:TextBox><br />
book_name:
<asp:TextBox ID="book_nameTextBox" runat="server" Text='<%# Bind("book_name") %>'>
</asp:TextBox><br />
book_num:
<asp:TextBox ID="book_numTextBox" runat="server" Text='<%# Bind("book_num") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="插入">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
book_id:
<asp:Label ID="book_idLabel" runat="server" Text='<%# Eval("book_id") %>'></asp:Label><br />
book_name:
<asp:Label ID="book_nameLabel" runat="server" Text='<%# Bind("book_name") %>'></asp:Label><br />
book_num:
<asp:Label ID="book_numLabel" runat="server" Text='<%# Bind("book_num") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑">
</asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除">
</asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="新建">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:booksConnectionString2 %>"
DeleteCommand="DELETE FROM [Code] WHERE [book_id] = @book_id"
InsertCommand="INSERT INTO [Code] ([book_id], [book_name], [book_num]) VALUES (@book_id, @book_name, @book_num)"
SelectCommand="SELECT [book_id], [book_name], [book_num] FROM [Code] WHERE ([book_id] = @book_id)"
UpdateCommand="UPDATE [Code] SET [book_name] = @book_name, [book_num] = @book_num WHERE [book_id] = @book_id">
<DeleteParameters>
<asp:Parameter Name="book_id" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="book_name" Type="String" />
<asp:Parameter Name="book_num" Type="Int16" />
<asp:Parameter Name="book_id" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="book_id" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="book_id" Type="String" />
<asp:Parameter Name="book_name" Type="String" />
<asp:Parameter Name="book_num" Type="Int16" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
相应的Default.aspx.vb程序如下:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted
DropDownList1.DataBind()
End Sub
Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles FormView1.ItemUpdated
DropDownList1.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
FormView1.Visible = False
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.Items(0).Text = "(Select)" Then
DropDownList1.Items.RemoveAt(0)
End If
FormView1.Visible = True
End Sub
End Class
浙公网安备 33010602011771号