添加单条数据

来源:互联网 发布:开淘宝店注册公司 编辑:程序博客网 时间:2024/05/18 04:56

Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 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 id="Head1" runat="server">    <title>添加单条数据实例</title></head><body background="image/background.gif">    <form id="form1" runat="server">    <div>        <div style="text-align: center">            <br />            <br />            <table style="width: 668px; height: 230px; background-image: url(image/background1.gif);">                <tr>                    <td colspan="4" style="height: 133px">                        <asp:Image ID="Image2" runat="server" ImageUrl="~/image/head.gif" /></td>                </tr>                <tr>                    <td style="width: 100px; height: 47px;">                    </td>                    <td style="width: 100px; background-image: url(image/background1.gif); height: 48px;" colspan="2" align="center">                        <asp:Image ID="Image3" runat="server" ImageUrl="~/image/mail.gif" /></td>                    <td style="width: 100px; height: 47px;">                    </td>                </tr>                <tr>                    <td style="width: 100px; height: 19px;">                    </td>                    <td style="width: 100px; background-image: url(image/background1.gif); height: 48px;" align="center">                        <asp:Image ID="Image4" runat="server" ImageUrl="~/image/Address.gif" /></td>                    <td style="width: 100px; background-image: url(image/background1.gif); height: 48px;">                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>                    <td style="width: 100px; height: 19px;">                    </td>                </tr>                <tr>                    <td style="width: 100px; height: 48px">                    </td>                    <td style="background-image: url(image/background1.gif); width: 100px; height: 48px" align="center">                        <asp:Image ID="Image5" runat="server" ImageUrl="~/image/title.gif" /></td>                    <td style="background-image: url(image/background1.gif); width: 100px; height: 48px" align="left">                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>                    <td style="width: 100px; height: 48px">                    </td>                </tr>                <tr>                    <td style="width: 100px; height: 34px">                    </td>                    <td style="background-image: url(image/background1.gif); width: 100px; height: 34px">                        <asp:Image ID="Image6" runat="server" ImageUrl="~/image/neirong.gif" /></td>                    <td style="background-image: url(image/background1.gif); width: 100px; height: 34px" align="left">                        <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Height="72px" Width="147px"></asp:TextBox></td>                    <td style="width: 100px; height: 34px">                    </td>                </tr>                <tr>                    <td style="width: 100px; height: 48px">                    </td>                    <td style="background-image: url(image/background1.gif); height: 48px" colspan="2" align="center">                        <asp:ImageButton ID="ImgBtnSend" runat="server" ImageUrl="~/image/send.gif" OnClick="ImgBtnSend_Click" />                        <asp:ImageButton ID="ImgBtnCZ" runat="server" ImageUrl="~/image/CZ.gif" /></td>                    <td style="width: 100px; height: 48px">                    </td>                </tr>                <tr>                    <td style="width: 100px">                    </td>                    <td style="background-image: url(image/background1.gif); height: 48px;" colspan="2">                        <asp:Image ID="Image7" runat="server" ImageUrl="~/image/notice.gif" /></td>                    <td style="width: 100px">                    </td>                </tr>                <tr>                    <td colspan="4">                        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE"                            BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="Smaller" ForeColor="Black"                            GridLines="Vertical" Width="720px" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="3">                            <FooterStyle BackColor="#CCCC99" />                            <RowStyle BackColor="#F7F7DE" />                            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />                            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />                            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />                            <AlternatingRowStyle BackColor="White" />                            <Columns>                                <asp:BoundField DataField="收件人地址" HeaderText="收件人地址" />                                <asp:BoundField DataField="标题" HeaderText="标题" />                                <asp:BoundField DataField="内容" HeaderText="内容" />                            </Columns>                        </asp:GridView>                    </td>                </tr>                <tr>                    <td colspan="4">                        <asp:Image ID="Image1" runat="server" ImageUrl="~/image/foot.gif" /></td>                </tr>            </table>        </div>        </div>    </form></body></html>

Default.aspx.cs

View Code
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            this.bindGridview();//调用自定义方法绑定控件数据        }    }    public void bindGridview()    {        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);        con.Open();//打开数据库连接        SqlDataAdapter ada = new SqlDataAdapter("Select * From tb_Send05", con);//创建数据适配器        DataSet ds = new DataSet();//创建数据集        ada.Fill(ds);//填充数据集        GridView1.DataSource = ds;//绑定数据        GridView1.DataBind();        con.Close();    }    protected void ImgBtnSend_Click(object sender, ImageClickEventArgs e)    {        try        {            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);            con.Open();            string InsertSql = "Insert Into tb_send05 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";            SqlCommand com = new SqlCommand(InsertSql, con);//创建执行SQL命令的SqlCommand对象            com.ExecuteNonQuery();//执行添加操作            SqlDataAdapter ada = new SqlDataAdapter("Select * From tb_Send05", con);//创建适配器            DataSet ds = new DataSet();            ada.Fill(ds);            GridView1.DataSource = ds;            GridView1.DataBind();            con.Close();            Response.Write("<script language=javascript>alert('添加成功!')</script>");        }        catch        {            Response.Write("<script language=javascript>alert('添加失败!')</script>");        }    }    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)    {        this.GridView1.PageIndex = e.NewPageIndex;        this.bindGridview();    }}

 

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击