<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="kaoshi.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>WebForm1</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="380" align="center" border="0">
                
<TR>
                    
<TD></TD>
                
</TR>
                
<TR>
                    
<TD class="red12" height="30">
                        
<asp:label id="lblProcess" runat="server"></asp:label>
                        
<asp:textbox id="txtClssid" runat="server" Visible="False" Width="16px" Height="12px"></asp:textbox></TD>
                
</TR>
                
<TR>
                    
<TD align="center">
                        
<asp:datagrid id="gridRoot" style="FONT-SIZE: 9pt" runat="server" HorizontalAlign="Center" OnDeleteCommand="gridRoot_Delete"
                            OnUpdateCommand
="gridRoot_Update" OnCancelCommand="gridRoot_Cancel" OnEditCommand="gridRoot_Edit"
                            DataKeyField
="id" BorderColor="black" BorderWidth="1" autogeneratecolumns="False" HeaderStyle-BackColor="#67bcce"
                            OnPageIndexChanged
="grid_PageChanged" PageSize="20" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages"
                            AllowPaging
="True" CellSpacing="0" CellPadding="4" width="500px">
                            
<HeaderStyle BackColor="#67BCCE"></HeaderStyle>
                            
<Columns>
                                
<asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="序号">
                                    
<HeaderStyle HorizontalAlign="Center" Height="30px" Width="50px"></HeaderStyle>
                                    
<ItemStyle Font-Names="verdana" HorizontalAlign="Center" BackColor="#EEEEEE"></ItemStyle>
                                
</asp:BoundColumn>
                                
<asp:TemplateColumn SortExpression="Typ" HeaderText="产品型号">
                                    
<HeaderStyle HorizontalAlign="Center" Width="180px"></HeaderStyle>
                                    
<ItemStyle HorizontalAlign="Center" BackColor="#EEEEEE"></ItemStyle>
                                    
<ItemTemplate>
                                        
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Typ") %>'>
                                        
</asp:Label>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<NOBR>
                                            
<asp:TextBox id=Typ runat="server" Width="94px" Text='<%# DataBinder.Eval(Container.DataItem, "Typ") %>'>
                                            
</asp:TextBox>
                                            
<asp:RequiredFieldValidator id="clssReqVal" runat="server" Font-Size="12" Font-Name="Verdana" Display="Dynamic"
                                                ControlToValidate
="Typ">
                    
&nbsp;*
            
</asp:RequiredFieldValidator></NOBR>
                                    
</EditItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:TemplateColumn SortExpression="Factory" HeaderText="工厂名称">
                                    
<HeaderStyle HorizontalAlign="Center" Width="180px"></HeaderStyle>
                                    
<ItemStyle HorizontalAlign="Center" BackColor="#EEEEEE"></ItemStyle>
                                    
<ItemTemplate>
                                        
<asp:Label id="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Factory") %>'>
                                        
</asp:Label>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<NOBR>
                                            
<asp:TextBox id="Factory" runat="server" Width="94px" Text='<%# DataBinder.Eval(Container.DataItem, "Factory") %>'>
                                            
</asp:TextBox>
                                            
<asp:RequiredFieldValidator id="Requiredfieldvalidator1" runat="server" Font-Size="12" Font-Name="Verdana" Display="Dynamic"
                                                ControlToValidate
="Factory">
                    
&nbsp;*
            
</asp:RequiredFieldValidator></NOBR>
                                    
</EditItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="编辑" CancelText="取消" EditText="编辑">
                                    
<HeaderStyle HorizontalAlign="Center" Width="100px"></HeaderStyle>
                                    
<ItemStyle Wrap="False" HorizontalAlign="Center" BackColor="#EEEEEE"></ItemStyle>
                                
</asp:EditCommandColumn>
                                
<asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete">
                                    
<HeaderStyle HorizontalAlign="Center" Width="50px"></HeaderStyle>
                                    
<ItemStyle HorizontalAlign="Center" BackColor="#EEEEEE"></ItemStyle>
                                
</asp:ButtonColumn>
                            
</Columns>
                            
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
                        
</asp:datagrid></TD>
                
</TR>
            
</TABLE>
        
</form>
    
</body>
</HTML>
cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace kaoshi
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label lblProcess;
        
protected System.Web.UI.WebControls.TextBox txtClssid;
        
protected System.Web.UI.WebControls.DataGrid gridRoot;
        SqlConnection thisConnection
=new SqlConnection("server=localhost;Database=stst;uid=sa;pwd=fan");
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!this.IsPostBack)
                BindGrid();
        }

        
public void BindGrid()
        
{
            SqlDataAdapter myCommand 
= new SqlDataAdapter("select * from FactoryType order by Factory",thisConnection);

            DataSet ds 
= new DataSet();
            myCommand.Fill(ds,
"xatop");

            gridRoot.DataSource
=ds.Tables["xatop"].DefaultView;
            gridRoot.DataBind();

        }

        
public void gridRoot_Edit(Object sender, DataGridCommandEventArgs e)
        
{
            gridRoot.EditItemIndex 
= (int)e.Item.ItemIndex;
            BindGrid();
        }

        
public void grid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        
{
            gridRoot.CurrentPageIndex
=e.NewPageIndex;
            BindGrid();
            
        }

        
public void gridRoot_Delete(Object sender, DataGridCommandEventArgs e)
        
{
            String deleteCmd 
= "DELETE from FactoryType where id = @Id";

            SqlCommand myCommand 
= new SqlCommand(deleteCmd, thisConnection);
            myCommand.Parameters.Add(
new SqlParameter("@Id", SqlDbType.NVarChar, 11));
            myCommand.Parameters[
"@Id"].Value = gridRoot.DataKeys[(int)e.Item.ItemIndex];

            myCommand.Connection.Open();

            
try
            
{
                myCommand.ExecuteNonQuery();
                lblProcess.Text 
= "<b>已删除记录</b>";
            }

            
catch (SqlException)
            
{
                lblProcess.Text 
= "错误:未能删除记录";
                lblProcess.Style[
"color"= "red";
            }


            myCommand.Connection.Close();

            BindGrid();
        }

        
public void gridRoot_Cancel(Object sender, DataGridCommandEventArgs e)
        
{
            gridRoot.EditItemIndex 
= -1;
            BindGrid();
        }

        
public void gridRoot_Update(Object sender, DataGridCommandEventArgs e)
        
{
            
string colvalue = ((TextBox)e.Item.FindControl("Typ")).Text;
            String updateCmd 
= "UPDATE FactoryType SET Typ = @Typ,Factory=@Factory where id = @Id";

            SqlCommand myCommand 
= new SqlCommand(updateCmd, thisConnection);

            myCommand.Parameters.Add(
new SqlParameter("@Id", SqlDbType.NVarChar, 11));
            myCommand.Parameters.Add(
new SqlParameter("@Typ", SqlDbType.NVarChar, 40));
            myCommand.Parameters.Add(
new SqlParameter("@Factory", SqlDbType.NVarChar, 40));

            myCommand.Parameters[
"@Id"].Value = gridRoot.DataKeys[(int)e.Item.ItemIndex];
            myCommand.Parameters[
"@Typ"].Value = ((TextBox)e.Item.FindControl("Typ")).Text;
            myCommand.Parameters[
"@Factory"].Value = ((TextBox)e.Item.FindControl("Factory")).Text;

            myCommand.Connection.Open();

            
try//捕捉错误
            {
                myCommand.ExecuteNonQuery();
                lblProcess.Text
= "<b>已更新记录</b>";
                gridRoot.EditItemIndex 
= -1;
            }

            
catch (SqlException b)
            
{
                
if (b.Number == 2627)
                    lblProcess.Text
= "错误:已存在具有相同主键的记录";
                
else
                    lblProcess.Text
= "错误:未能更新记录,请确保正确填写了字段";
                lblProcess.Style[
"color"= "red";

            }


            myCommand.Connection.Close();
//更新数据完毕

            BindGrid();
            
//DdlLst(e);
        }


        
Web 窗体设计器生成的代码
    }

}

posted on 2005-02-01 10:44  fghr  阅读(1233)  评论(2编辑  收藏  举报