“组合式”Server控件中包含的DataGrid对象,无法响应一些事件

sinzy 2003-09-01 06:08:25
我要以创建“组合式”控件的方式,给DataGrid控件增加一些功能,我的控件的重载的CreateChildControls()方法中写了如下的代码(大致):
this.m_dataGrid1 = new DataGrid()

// “编辑”列
EditCommandColumn eccCol = new EditCommandColumn();
eccCol.HeaderText = "编辑";
eccCol.EditText = "[E]";
eccCol.UpdateText = "[U]";
eccCol.CancelText = "[C]";
m_dgrdShow.Columns.Add(eccCol);

// “删除”列
ButtonColumn btnCol = new ButtonColumn();
btnCol.HeaderText = "删除";
btnCol.CommandName = "delete";
btnCol.Text = "[D]";
m_dgrdShow.Columns.Add(btnCol);

// DataGrid 事件……
m_dgrdShow.ItemCommand += new DataGridCommandEventHandler(m_dgrdShow_ItemCommand);
m_dgrdShow.CancelCommand += new DataGridCommandEventHandler(m_dgrdShow_CancelCommand);
m_dgrdShow.EditCommand += new DataGridCommandEventHandler(m_dgrdShow_EditCommand);
m_dgrdShow.UpdateCommand += new DataGridCommandEventHandler(m_dgrdShow_UpdateCommand);
m_dgrdShow.ItemDataBound += new DataGridItemEventHandler(m_dgrdShow_ItemDataBound);

BuildDataGrid(); // 生成一些显示数据的BoundColumn列对象并添加到DataGrid的列集合中
BindDataGrid(); // 绑定数据

this.Controls.Add(this.m_dgrdShow);

现在,我可以正常的显示数据,并能够响应EditCommand事件,但是UpdateCommand和CancelCommand事件则无法响应(用断点测试得知)……

郁闷无比,请各位高人指点~

我觉得问题原因可能是页面事件引发顺序的问题,我的自定义控件少了一道什么工序……
...全文
24 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwgj 2003-09-02
  • 打赏
  • 举报
回复
学习
sinzy 2003-09-02
  • 打赏
  • 举报
回复
谢谢大家!
现在问题基本解决,原因是我在派生WebControl时实现了一个INamingContainer接口,去掉这个接口实现就可以响应事件了。实现接口的原因是看了一本参考书上关于“组合式”控件的说明……ft...

不知道有没有人愿意详细说明一下这个接口的实际应用?
saucer 2003-09-02
  • 打赏
  • 举报
回复
here is a contrived example

1. TestGrid.aspx:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="WebApplication5.WebForm2"%>
<%@ Register TagPrefix="cc1" Namespace="CustomControls" Assembly="CustomControls" %>
<form runat=server>
<cc1:MyDataGrid id="MyDataGrid1" runat="server" Width="249px" Height="79px"></cc1:MyDataGrid>
</form>

...
2. WebForm2.aspx.vb:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
MyDataGrid1.DataBind()
End If
End Sub


3. MyDataGrid.cs: (compile it into CustomControls.dll)
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;

namespace CustomControls
{
[ToolboxData("<{0}:MyDataGrid runat=server></{0}:MyDataGrid>")]
public class MyDataGrid : System.Web.UI.WebControls.WebControl
{
Button m_btn;
DataGrid m_dg;

protected override void CreateChildControls()
{
m_btn = new Button();
m_btn.Text = "click me";
m_btn.Click += new EventHandler(Button_Clicked);
Controls.Add(m_btn);

m_dg = new DataGrid();
Controls.Add(m_dg);

EditCommandColumn ecc = new EditCommandColumn();
ecc.HeaderText = "Command";
ecc.EditText = "Edit";
ecc.UpdateText = "Update";
ecc.CancelText = "Cancle";
m_dg.Columns.Add(ecc);

m_dg.EditCommand += new DataGridCommandEventHandler(MyDataGrid_Edit);
m_dg.UpdateCommand += new DataGridCommandEventHandler(MyDataGrid_Update);
m_dg.CancelCommand += new DataGridCommandEventHandler(MyDataGrid_Cancel);

}

void Button_Clicked (Object o, EventArgs e)
{
Page.Response.Write(String.Format("button in MyDataGrid is clicked at {0}", DateTime.Now));
}

public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
Page.Response.Write("**** in MyDataGrid_Edit ****");
m_dg.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}

public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
Page.Response.Write("**** in MyDataGrid_Cancel ****");
m_dg.EditItemIndex = -1;
BindGrid();
}

public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
Page.Response.Write("**** in MyDataGrid_Update ****");
m_dg.EditItemIndex = -1;
BindGrid();
}

void BindGrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from authors", "server=(local);database=pubs;uid=sa;pwd=;");

DataTable dt = new DataTable();
da.Fill(dt);

m_dg.DataSource=dt.DefaultView;
m_dg.DataBind();
}

public override void DataBind()
{
EnsureChildControls();
BindGrid();
}

protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
EnsureChildControls();
}
}
}

webdiyer 2003-09-01
  • 打赏
  • 举报
回复
点击一个编辑按钮后,不要点击取消或更新,再点击另一个编辑按钮,这个编辑按钮还会不会正确响应事件??
steveson 2003-09-01
  • 打赏
  • 举报
回复
检查一下
EditCommand、UpdateCommand和CancelCommand事件
中执行了:
m_dataGrid1.DataBind()没有

另外:
UpdateCommand和CancelCommand事件
需要指定
m_dataGrid1.EditItemIndex = -1;

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧