asp.net 2.0中用GRIDVIEW插入新記錄_.Net教程
推薦:在ASP.NET程序中創(chuàng)建唯一序號(hào)如果在程序中需要?jiǎng)?chuàng)建全局唯一的序號(hào),那么必須對(duì)創(chuàng)建序號(hào)的過程進(jìn)行同步處理,防止多個(gè)并發(fā)訪問時(shí)出現(xiàn)相同序號(hào)的情況。下面列出幾種方法供大家參考。 利用數(shù)據(jù)庫的方法 后面的例子都基于MS
看了下國外某巨牛的BLOG,大概是講asp.net 2.0中用GRIDVIEW插入新記錄的,方法比較特別,但效果一般,故將程序轉(zhuǎn)之,較為簡(jiǎn)單,不做解釋等。
以下為引用的內(nèi)容: <%@ Page Language="C#" ClassName="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> GridView1.ShowFooter = false; } GridView1.ShowFooter = true; } void Button1_Click(object sender, EventArgs e) { 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(); }
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div>
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" /> </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=xxx;database=northwind" EnableCaching="True"> </asp:SqlDataSource> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" Runat="server" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]" ConnectionString="server=localhost;uid=sa;password=xxxxx;database=northwind"> <DeleteParameters> <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter> </DeleteParameters> <UpdateParameters> <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter> <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter> <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter> </UpdateParameters> <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> </div> </form> </body> </html> |
分享:一個(gè)功能齊全的DataGrid分頁例子以下為引用的內(nèi)容: C#版本 DataGridPaging.aspx <%@ Page language="c#" EnableViewState
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過程實(shí)現(xiàn)分頁示例代碼
- 相關(guān)鏈接:
- 教程說明:
.Net教程-asp.net 2.0中用GRIDVIEW插入新記錄。