vs 2005的GridView 编辑功能实现 方便和我一样的人

Dcx_UPdate.aspx 代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dcx_UPdate.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 runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <br />
        <table cellspacing="0" cellpadding="0" border="1" align="center" style="width: 46%" >
            <tr>
                <td align="right" style="width: 56%; height: 21px">
                    <asp:Label ID="Label_BranchID" runat="server" Text="BranchID"></asp:Label></td>
                <td align="left" colspan="5" style="width: 157px; height: 21px">
                    检出线表</td>
            </tr>
        </table>
        <br />
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
 OnRowDataBound="GridView1_RowDataBound"
 HorizontalAlign="Left"
 OnRowCommand="GridView1_RowCommand"
 CellPadding="4"
 ForeColor="#333333"
 GridLines="None"
 OnRowCancelingEdit="GridView1_RowCancelingEdit"
 OnRowEditing="GridView1_RowEditing"
 OnRowUpdating="GridView1_RowUpdating">
 <Columns>
     <asp:TemplateField HeaderText="No." Visible=False InsertVisible="False">
         <ItemTemplate>
             <asp:Label ID="lblNumber" runat="server"  Text='<%# Bind("id") %>'></asp:Label>
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" Width="5%" />
     </asp:TemplateField>
     <asp:TemplateField HeaderText="项目种类">
         <ItemTemplate>
             <asp:HyperLink ID="hylnkName" runat="server" Text='<%# Bind("ItemClassName") %>'></asp:HyperLink>
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" Width="15%" />
            <ControlStyle Width="150px" />
     </asp:TemplateField>
     <asp:TemplateField HeaderText="项目">    
       
         <ItemTemplate>
             <asp:Label ID="lblStartDate" runat="server" Text='<%# Bind("ItemName") %>' ></asp:Label>
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" Width="15%" />
            <ControlStyle Width="100px" />
     </asp:TemplateField>
     <asp:TemplateField HeaderText="值">        
         <EditItemTemplate>
             <asp:TextBox ID="TBEndDate" runat="server" Width="95%"  Text='<%# Bind("Jcx_Value") %>'></asp:TextBox>
         </EditItemTemplate>
         <ItemTemplate>
             <asp:Label ID="lblEndDate" runat="server" Text='<%# Bind("Jcx_Value") %>'></asp:Label>
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" Width="15%" />
            <ControlStyle Width="100px" />
     </asp:TemplateField>
      <asp:TemplateField HeaderText="编辑" ShowHeader="False">
             <EditItemTemplate>
                 <asp:LinkButton ID="LinkUpdate" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "jcx_value") %>' CommandName="Update" Text="更新"></asp:LinkButton>
                 <asp:LinkButton ID="LinkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
             </EditItemTemplate>
             <ItemTemplate>
                 <asp:LinkButton ID="LinkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>             
             </ItemTemplate>
             <ItemStyle Width="8%" HorizontalAlign="Center" />
     </asp:TemplateField>
 </Columns>
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#E3EAEB" />
        <EditRowStyle BackColor="#7C6F57" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
</asp:GridView>
      
    </div>
    </form>
</body>
</html>
 

 

Dcx_UPdate.aspx.cs代码

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;
using System.Data.Sql;

public partial class _Default : System.Web.UI.Page
{
    public System.Data.SqlClient.SqlConnection sqlConn;  ///数据库连接
    public string strBranchID = null;
    protected void Page_Load(object sender, EventArgs e)
    {
      
        SQLCONNECTION();///连接数据库
        strBranchID = "1";   //这里是编号 在这里给值即可 
        Label_BranchID.Text = strBranchID+"   ";
        if (!IsPostBack)
        {
            DoDataBind();

        }
    }
    public void SQLCONNECTION()///连接数据库
    {
        try
        {
            sqlConn = new System.Data.SqlClient.SqlConnection();
            sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MSSQL2000"].ConnectionString;
            sqlConn.Open();
        }
        catch (Exception ex)
        {
            DataError(ex);
        }
    }
    public DataSet DataError(Exception ex)
    {
        DataSet errDS = new DataSet("Errors");
        DataTable errTable = errDS.Tables.Add("Error");
        errTable.Columns.Add("Message");
        errTable.Rows.Add(new Object[] { ex.Message });
        return errDS;
    }
  
    protected void DoDataBind()
    {

        SqlDataAdapter sda = new SqlDataAdapter("select id,BranchID,ItemClassName,ItemName,Jcx_Value from WQ_JCX where BranchID='" + strBranchID + "' order by BranchID", sqlConn);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        this.GridView1.DataSource = ds;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem == null)
            {
                return;
            }

            LinkButton lkEdit = (LinkButton)e.Row.FindControl("LinkEdit");     
       
        }
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       // Response.Write(e.CommandName);
       switch (e.CommandName)
        {
            case "Update":
              
                break;

            default:
                break;
        }
     
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;

        GridView1.EditRowStyle.BackColor = System.Drawing.Color.FromName("#F7CE90");
        DoDataBind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      
        GridViewRow row = GridView1.Rows[e.RowIndex];
        int LB_FORUMID = Convert.ToInt16(((Label)row.FindControl("lblNumber")).Text);
        LinkButton linkB = (LinkButton)GridView1.Rows[e.RowIndex].FindControl("LinkUpdate");
        string TXB_MASTER = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBEndDate")).Text;
        //处理代码
      
        string strupdate = null;
        strupdate = "update WQ_JCX set Jcx_Value='" + TXB_MASTER + "' where id="+LB_FORUMID;
     
       // Response.Write(strupdate);
        try
        {
            SqlCommand com_update = new SqlCommand(strupdate, sqlConn);
            com_update.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }

      

        GridView1.EditIndex = -1;
        DoDataBind();

    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        DoDataBind();
    }
}
Web.config代码

<?xml version="1.0" encoding="utf-8"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    /Windows/Microsoft.Net/Framework/v2.x/Config 中
-->
<configuration>
    <appSettings/>
      <!-- 数据库连接 -->
  <connectionStrings>
    <add connectionString="Server=192.168.0.31;UID=sa;PWD=sa;Database=ln" name="MSSQL2000" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <!-- 数据库连接 -->
    <system.web>
        <!--
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。
        -->
        <compilation debug="false" />
        <!--
            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
        <authentication mode="Windows" />
        <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
</configuration>
表结构

 


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[WQ_JCX]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[WQ_JCX]
GO

CREATE TABLE [dbo].[WQ_JCX] (
 [id] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
 [BranchID] [int] NULL ,
 [ItemClassName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [ItemName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Jcx_Value] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Jcx_memo] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

 

 

大家是不是遇到过这种情况: 页面很长,当拖动到页面底部进行编辑的时候,控件选项改变,postback之后,页面总是又回到了top, 又得拖到后面才能操作.一句话, “很烦”下面有了解决办法,很简单,在页面的@Page中加一个属性SmartNavigation=”true”, 也可在BehindCode中写 Me.SmartNavigation=true哈哈,一切烦恼都没了!
<%@ Page Language="VB" MasterPageFile="~/FinanceManagement/FinanceMaster.master"    AutoEventWireup="false" CodeFile="Organization.aspx.vb" Inherits="FinanceManagement_Organization"    Title="SP China 网上费用报销" SmartNavigation="true" %>


加这个就ok 了
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dcx_UPdate.aspx.cs" Inherits="_Default" SmartNavigation="true" %>


SmartNavigation="true"   这个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值