| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1189 人关注过本帖
标题:DataGrid控件测试问题,急!求救!
只看楼主 加入收藏
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
 问题点数:0 回复次数:14 
DataGrid控件测试问题,急!求救!
我用Visual 设计了一个名为gds1.aspx页面,页面含有一个DataGrid控件,名为DataGrid1,控件设置了OnEditCommand, OnCancelCommand ,OnUpdateCommand在后台文件中编写了Grid1_Edit, Grid1_Cancel, Grid1_Update代码,测试时出现了这样的问题,有急用,求救!

编译器错误:gds1.aspx不含有Grid1_Edit定义!

请帮我解决这一问题,谢谢!
搜索更多相关主题的帖子: DataGrid 控件 
2007-12-14 09:34
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
事件要匹配绑定的,不知道你是怎么实现的

Flying without wings
2007-12-14 23:20
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
DataGrid有一个功能是设按钮列,比如编辑列,点 DataGrid一行中的编辑文字(实为按钮),就变为两个按钮,按钮文字为更新与取消。在后台文件中编写编辑、更新、取消的逻辑代码,可测试时老出现上面没有定义的编译器错误。
2007-12-17 11:47
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
看DataGrid的事件属性,双击edit、cancel、update的事件进行绑定

Flying without wings
2007-12-17 22:15
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
去了好多论坛,还是这里好,人气足,斑竹很负责.
2007-12-18 12:02
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
我的主文件代码如下:容易看!

<%@ Page language="c#" Codebehind="gds1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.gds1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
  <title>goodsmanager</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="javascript" name="vs_defaultClientScript">
  <meta content="http://schemas. name="vs_targetSchema">
  <STYLE>.us { FONT-SIZE: 20pt; COLOR: #ff0000; FONT-FAMILY: 华文新魏 }
 .ps { FONT-SIZE: 12pt; COLOR: #6600ff; FONT-FAMILY: 宋体 }
 .bt { FONT-SIZE: 10pt; COLOR: blue }
 .st { }
 .pn { }
 .mn { }
 </STYLE>
</HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px; HEIGHT: 331px"
     cellSpacing="0" cellPadding="0" width="70%" border="0" bgColor="#ffffcc">
     <TR>
      <TD style="WIDTH: 639px; HEIGHT: 51px" align="center"><FONT class="us" color="#ff0066">货物信息管理</FONT></TD>
     </TR>
     <TR>
      <TD class="bt" style="FONT-SIZE: 10pt; WIDTH: 639px; COLOR: blue; HEIGHT: 42px" borderColor="#99cccc"
       borderColorLight="aqua" bgColor="#c3aebf" borderColorDark="aqua" colSpan="1" rowSpan="1">  
       输入货物编号
       <asp:textbox id="TextBox1" runat="server"></asp:textbox>
       <asp:button id="Button1" runat="server" Text="search" BorderColor="#FFFFC0" BackColor="#C0C000"></asp:button></TD>
     </TR>
     <TR>
      <TD style="WIDTH: 639px; HEIGHT: 159px">
       <asp:datagrid id="DataGrid1" Width="552px" Height="208px" Font-Name="宋体" Font-Size="10pt" DataSource="<%# dsgoods1 %>" DataMember="goods" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="LightCyan" CellPadding="4" AutoGenerateColumns="False" DataKeyField="goods_id" HorizontalAlign="Center" Font-Names="宋体" ForeColor="Lavender" OnEditCommand="Grid1_Edit" OnCancelCommand="Grid1_Cancel" OnUpdateCommand="Grid1_Update" OnDeleteCommand="Grid1_Delete"  AllowPaging="True" runat="server" AllowCustomPaging="True">
        <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
        <EditItemStyle HorizontalAlign="Center"></EditItemStyle>
        <ItemStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="White"></ItemStyle>
        <HeaderStyle Font-Size="10pt" Font-Names="宋体" Font-Bold="True" HorizontalAlign="Center" ForeColor="#FFFFCC"
         BackColor="#990000"></HeaderStyle>
        <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
        <Columns>
         <asp:BoundColumn DataField="goods_id" SortExpression="goods_id" HeaderText="编号">
          <HeaderStyle Width="30pt"></HeaderStyle>
         </asp:BoundColumn>
         <asp:EditCommandColumn HeaderText="编辑" EditText="编辑" UpdateText="更新" CancelText="取消">
          <HeaderStyle Width="30pt"></HeaderStyle>
         </asp:EditCommandColumn>
         <asp:TemplateColumn HeaderText="货物名称">
          <ItemTemplate>
           <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.goods_name") %>'>
           </asp:Label>
          </ItemTemplate>
          <EditItemTemplate>
           <asp:TextBox runat="server" ID="editgoods_name" Text='<%# DataBinder.Eval(Container, "DataItem.goods_name") %>'>
           </asp:TextBox>
          </EditItemTemplate>
         </asp:TemplateColumn>
         <asp:TemplateColumn HeaderText="参考价格">
          <HeaderStyle Width="44pt"></HeaderStyle>
          <ItemTemplate>
           <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.price") %>'>
           </asp:Label>
          </ItemTemplate>
          <EditItemTemplate>
           <asp:TextBox runat="server" ID="editprice" Text='<%# DataBinder.Eval(Container, "DataItem.price") %>'>
           </asp:TextBox>
          </EditItemTemplate>
         </asp:TemplateColumn>
          <asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete">
          <HeaderStyle Width="30pt"></HeaderStyle>
         </asp:ButtonColumn>
        </Columns>
        <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
       </asp:datagrid>
      </TD>
     </TR>
     <TR>
      <TD style="WIDTH: 639px; HEIGHT: 25px" bgColor="#c3aebf"></TD>
     </TR>
     <TR>
      <TD class="bt" style="WIDTH: 639px; HEIGHT: 38px" borderColor="blue" borderColorLight="aqua"
       bgColor="#c3aebf" borderColorDark="aqua">
       <asp:hyperlink id="HyperLink1" runat="server">添加新货物信息</asp:hyperlink></TD>
     </TR>
    </TABLE>
   </FONT>
  </form>
 </body>
</HTML>
2007-12-18 15:26
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
上面的代码看起来很长,但最重要的是第一行代码和中间的DataGrid控件内容,特别是模板项中的内容。
2007-12-18 15:26
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
另外,后台文件中对编辑、取消的逻辑代码如下:
using System;
using System.Collections;
using
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 WebApplication1
{
    /// <summary>
    /// gds1 的摘要说明。
    /// </summary>
    public class gds1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox editgoods_name;
        protected System.Web.UI.WebControls.TextBox editprice;
                                          protected System.Web.UI.WebControls.TextBox h;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        protected System.Data.SqlClient.SqlCommand sqlEditCommand1;
        protected System.Data.SqlClient.SqlCommand sqlCancelCommand1;
        protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        protected System.Data.SqlClient.SqlConnection sqlConnection1;
        protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        protected WebApplication1.dsgoods dsgoods1;
        protected System.Web.UI.WebControls.HyperLink HyperLink1;
        protected System.Web.UI.WebControls.Button Button1;

  public void Grid1_Edit(Object sender, DataGridCommandEventArgs dgcea)
  {
   DataGrid1.EditItemIndex = dgcea.Item.ItemIndex;
   Bind();
  }

  public void Grid1_Cancel(Object sender, DataGridCommandEventArgs dgcea)
  {
   DataGrid1.EditItemIndex = -1;
   Bind();
  }
2007-12-18 15:29
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
比较完整的后台文件是:

using System;
using System.Collections;
using
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 WebApplication1
{
    /// <summary>
    /// gds1 的摘要说明。
    /// </summary>
    public class gds1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox editgoods_name;
        protected System.Web.UI.WebControls.TextBox editprice;
        protected System.Web.UI.WebControls.TextBox editprovider;
        protected System.Web.UI.WebControls.TextBox editmore;
        protected System.Web.UI.WebControls.TextBox h;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        protected System.Data.SqlClient.SqlCommand sqlEditCommand1;
        protected System.Data.SqlClient.SqlCommand sqlCancelCommand1;
        protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        protected System.Data.SqlClient.SqlConnection sqlConnection1;
        protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        protected WebApplication1.dsgoods dsgoods1;
        protected System.Web.UI.WebControls.HyperLink HyperLink1;
        protected System.Web.UI.WebControls.Button Button1;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (! IsPostBack)
            {
                Bind();
            }
            // 在此处放置用户代码以初始化页面
        }

        public void Bind()
        {
            sqlDataAdapter1.Fill(dsgoods1);
            DataGrid1.DataSource=dsgoods1;
            DataGrid1.DataBind();
        }

        public void Grid1_Edit(Object sender, DataGridCommandEventArgs dgcea)
        {
            DataGrid1.EditItemIndex = dgcea.Item.ItemIndex;
            Bind();
        }

        public void Grid1_Cancel(Object sender, DataGridCommandEventArgs dgcea)
        {
            DataGrid1.EditItemIndex = -1;
            Bind();
        }

        public void Grid1_Update(Object sender, DataGridCommandEventArgs e)
        {
            Textbox h;
            int Key=(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
            String goods_id = " goods_id =" + Key.ToString();
            h=(TextBox)e.Item.FindControl("editgoods_name");
            String goods_name = " goods_name = ' " + h.Text.Trim() +"  ',";
            h=(TextBox)e.Item.FindControl("editprice");
            String price = " price = ' " + h.Text.Trim() +"  ',";
            h=(TextBox)e.Item.FindControl("editprovider");
            String provider = " provider = ' " + h.Text.Trim() +"  ',";
            h=(TextBox)e.Item.FindControl("editmore");
            String more = " more = ' " + h.Text.Trim() +"  ',";
            
            String g = "Update goods set";
            g +=goods_name + price + provider + more ;
            g +="where goods_id = " + goods_id;
            RunSql(g);
            DataGrid1_EditItemIndex = -1;
            Bind();
        }

        public void Grid1_Delete(Object sender, DataGridCommandEventArgs e)
        {
            int Key=(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
            String goods_id = Key.ToString();
            String g = "Delete from goods";
            g +="where goods_id = " + goods_id;
            RunSql(g);
            DataGrid1.EditItemIndex = -1;
            Bind();
        }
    
        public String RunSql (string vsql)
        {
            try
            {
                SqlCommand mycommand = new SqlCommand(vsql,sqlConnection1);
                sqlConnection1.Open();
                mycommand.ExecuteNonQuery();
                sqlConnection1.Close();
            }
            catch(Exception e)
            {
                String cdg = "出现异常:" + e.ToString();
                messages.Text=cdg;
            }
            return("OK");
        }
窗体设计器生成的代码省略
}
2007-12-18 15:35
mao_x_r
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2006-6-6
收藏
得分:0 
清高手指教!
2007-12-18 21:54
快速回复:DataGrid控件测试问题,急!求救!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055643 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved