在Gridview控制項中,可以實現插入新記錄的操作(見《使用ASP.NET 2.0中的Gridview控制項》)一文,但如果想實現在Gridview中,實現在Gridview控制項的最後一行,提供一個空白行給使用者輸入要輸入的記錄,那無疑是很方便的。下面將介紹其實現方法。
首先,我們打算在讓使用者進行選擇,當使用者需要新增一記錄時,便點擊新增按鈕,之後在Gridview的最後一行裡,顯示一個空白行,讓使用者按欄位進行輸入,如所示:
當使用者決定不輸入新空白記錄時,可以按"cancel"按鈕返回,該空白行消失。要實現這樣的效果,我們可以充分利用Gridview的footer的模版功能進行自訂,因為有3列,所以,在每一列的footer模版中,定義如下:
<asp:Gridview ID="Gridview1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False" ShowFooter="True"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="CustomerIDLabel" Runat="Server"><%# Eval("CustomerID") %></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="CustomerIDTextBox" Runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="CompanyNameLabel" Runat="Server"><%# Eval("CompanyName") %></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="CompanyNameTextBox" Runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField> <FooterTemplate> <asp:DropDownList ID="ContactTitleDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="ContactTitle" DataValueField="ContactTitle"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]" ConnectionString="server=localhost;uid=sa;password=xxx;database=northwind"> </asp:SqlDataSource> <asp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" /> <asp:Button ID="CancelButton1" Runat="server" Text="Cancel" OnClick="CancelButton1_Click" /> </FooterTemplate> <ItemTemplate> <asp:DropDownList ID="ContactTitleDropDown" SelectedValue=’<%# Bind("ContactTitle") %>’ Runat="Server" DataSourceID="SqlDataSource3" DataTextField="ContactTitle" DataValueField="ContactTitle" ></asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]" ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind" EnableCaching="True"> </asp:SqlDataSource> </ItemTemplate> </asp:TemplateField> </Columns> </asp:Gridview> |
以上為Gridview的代碼,可以看到,在第一,二列的<foottemplate>列中,分別提供了customerid和companyname兩個文字框以供使用者輸入,在第三列的<footertemplate>列中,以dropdownlistbox的形式來顯示contracttitle.。其中,請注意第三列的footertemplate中的add和cancel兩個按鈕的,它們的事件代碼如下:
<script runat="server"> void CancelButton1_Click(object sender, EventArgs e) { Gridview1.ShowFooter = false; } void AddButton1_Click(object sender, EventArgs e) { Gridview1.ShowFooter = true; } //點add按鈕時,將新增的記錄更新到資料庫中去 void Button1_Click(object sender, EventArgs e) { TextBox customerID = Gridview1.FooterRow.FindControl("CustomerIDTextBox") as TextBox; TextBox companyName = Gridview1.FooterRow.FindControl("CompanyNameTextBox") as TextBox; DropDownList ContactTitle = Gridview1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList; SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text; SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text; SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue=ContactTitle.SelectedValue; SqlDataSource1.Insert(); } </script> |
其中的cancel按鈕的事件,用來取消顯示Gridview的footer模版,因此設定showfooter屬性為false,而addbutton1按鈕,是當使用者決定新增記錄時點選的,此時將設定showfooter屬性為true,以顯示各列的foottemplate,從而達到顯示新的一個空白行的目的。
而在更新代碼button1_click事件中,將首先使用Gridview1.footerrow.findcontrol的方法,將使用者新增的各欄位的值提取出來,然後分別賦值給sqldatasource的insertparameters集合(注意要一一對應),最後使用sqldatasource的insert方法,就可以成功向資料庫增加一條新記錄了。
另外,為了在表單載入時,顯示資料庫northwind中customers表的資料,需要設定sqldatsource1的屬性,如下代碼:
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)" SelectCommand="SELECT top 5 [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]" ConnectionString="server=localhost;uid=sa;password=XXXXX;database=northwind"> <InsertParameters> <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter> <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter> <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter> </InsertParameters> </asp:SqlDataSource> |