九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
含有dropdownlist的gridview增刪改查
含有dropdownlist的gridview增刪改查
分類:net 2013-06-25 10:27 86人閱讀 (0)
GridView控件修改、刪除例子,修改時(shí)含有DropDownList控件。
示例運(yùn)行效果圖:
GridViewUp.aspx文件代碼:
[html] view plaincopy
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewUp.aspx.cs" Inherits="gridview_GridViewUp" %>
<!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>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" border="0" width="80%" style="font-size: 11px">
<tr>
<td align="center">
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
DataKeyNames="id,name" OnPageIndexChanging="GridView1_PageIndexChanging" DataMember="card,price" OnRowDataBound="GridView1_RowDataBound" GridLines="None">
<Columns>
<asp:BoundField HeaderText="身份證號(hào)" DataField="card" Visible=false />
<asp:BoundField HeaderText="編號(hào)" DataField="id" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="姓名" ReadOnly="True" />
<asp:TemplateField HeaderText="身份證號(hào)">
<ItemTemplate>
<%# Eval("card") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TBCard" Text='<%# Eval("card") %>' runat="server" Width="140px" />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="學(xué)歷">
<ItemTemplate>
<%# Eval("description")%>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="HDFXueli" runat="server" Value='<%# Eval("xueli") %>' />
<asp:DropDownList ID="DDLXueli" runat="server" Width="90px" />
</EditItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="價(jià)格">
<ItemTemplate>
<%# Eval("price") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TBPrice" Text='<%# Eval("price") %>' runat="server" Width="90px" />
</EditItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:BoundField HeaderText="建立時(shí)間" DataField="createdate" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
</Columns>
<PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
<RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
GridViewUp.aspx.cs文件代碼:
[csharp] view plaincopy
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class gridview_GridViewUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "SELECT a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("數(shù)據(jù)庫(kù)錯(cuò)誤,錯(cuò)誤原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
{
DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");
// 生成 DropDownList 的值,綁定數(shù)據(jù)
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "SELECT * from xueli";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "xueli");
if (conn.State.ToString() == "Open") conn.Close();
ddlxueli.DataSource = ds.Tables[0].DefaultView;
ddlxueli.DataTextField = "description";
ddlxueli.DataValueField = "code";
ddlxueli.DataBind();
//
// 選中 DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
//
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "update test01 set card='" + card + "',xueli='" + xueli + "',price='" + price + "' where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("數(shù)據(jù)庫(kù)錯(cuò)誤,錯(cuò)誤原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "delete from test01 where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("數(shù)據(jù)庫(kù)錯(cuò)誤,錯(cuò)誤原因:" + ex.Message);
Response.End();
}
}
}
sql server生成表代碼:
[sql] view plaincopy
CREATE TABLE [dbo].[test01] (
[id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[xueli] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[price] [decimal](18, 0) NULL ,
[createdate] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[test01] ADD
CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[xueli](
[id] [int] IDENTITY(1,1) NOT NULL,
[code] [int] NULL,
[description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_xueli] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
xueli表中的數(shù)據(jù):
id    code    description
1    1    博士
2    2    碩士
3    3    學(xué)士
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
在GridView控件中給數(shù)據(jù)列添加超鏈接并傳值的問(wèn)題解決方法
DataList嵌套GridView實(shí)現(xiàn)文章分類列表顯示
點(diǎn)擊textbox彈出模態(tài)窗口,選擇后返回主頁(yè)面并賦值textbox
ASP.NET2.0中Gridview中數(shù)據(jù)操作技巧
C#(asp.net)實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出Excel表詳細(xì)代碼
c#(Asp.Net) 對(duì) GridView控件操作:實(shí)現(xiàn)綁定特定列、導(dǎo)出Excel、導(dǎo)出WORD
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服