自定义控件中的GridView RowCommand事件 [英] GridView RowCommand Event In Custom Control

查看:97
本文介绍了自定义控件中的GridView RowCommand事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在创建一个自定义控件.我在该控件中添加了一个GridView,如下所示.

Hello,

I am creating a custom control. I added a GridView in that control as below.

        protected override void OnInit(EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(query, con);
            da.Fill(ds);

            TemplateField tf = new TemplateField();
            tf.HeaderTemplate = new GridViewExtension(ListItemType.Header, "To Delete");
            tf.ItemTemplate = new GridViewExtension(ListItemType.Item, "To Delete");

            gvData.Columns.Add(tf);
            gvData.RowCommand += new GridViewCommandEventHandler(gvData_RowCommand);
            gvData.DataSource = ds;
            gvData.DataBind();
            gvData.Columns.Insert(0, tf);
        }


protected override void RenderContents(HtmlTextWriter output)
{
    gvData.RenderControl(output);
}



出现带有删除图像按钮的Gridview.

我需要编写代码以自行删除该控件中的行.我尝试使用RowCommand,但是它不起作用.是否有任何方法可以触发事件以删除记录?



Gridview is appearing with delete image button.

I need to write code for delete the row in that control it self. I tried with RowCommand but its not working.Is there any way to fire an event to delete the record?

推荐答案

似乎是您将gridview与数据源绑定在一起. .
y您不使用''GridView.DeleteRow''方法

http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.gridview.deleterow.aspx

希望对您有帮助

谢谢
looks like you are binding your gridview with data source..
y dont you use ''GridView.DeleteRow'' method

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.deleterow.aspx

hope this helps

thanks


// Generate Row Delete Event and then write following Code

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
        GridView YourGrid= sender as GridView;

     }


我假设您已经使用INamingContainer接口实现了自定义控件.

ASP.net中的事件绑定适用于控件ID,并且INamingContainer将允许您的自定义控件遵循ClientID生成机制.

因此,如果您的自定义控件具有正确的ID,则会引发该控件内的所有事件.

希望这会有所帮助.

谢谢,
赫曼特


I assume you have implemented your custom control with INamingContainer interface.

The event binding in ASP.net works on control IDs and INamingContainer will allow your custom control to follow the ClientID generation mechanism.

So if your custom control has proper ID all events within the control will be raised.

hope this will help.

Thanks,
Hemant


protected void Page_Load(object sender, EventArgs e)
        {
            TemplateField tf = new TemplateField();
            tf.ItemTemplate = new MyTemplateField();
            GridView1.Columns.Add(tf);
            GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
            GridView1.DataSource = NewDataTable();
            GridView1.DataBind();
        }



模板字段



template field

public class MyTemplateField : Control, ITemplate
    {
        Button btn;
        public MyTemplateField()
        {
            btn = new Button();
            btn.ID = "this button";
            btn.Text = "Im the button";
            btn.Click += new EventHandler(btn_Click);
        }
        void btn_Click(object sender, EventArgs e)
        {
            //GridView gridView = (sender as Button).NamingContainer as GridView ;
            CommandEventArgs cea = new CommandEventArgs("cmd",btn);
            //below line will bubble the event and gridview will capture it in RowCommand event handler
            RaiseBubbleEvent(btn, cea);
        }
        
        public void InstantiateIn(Control container)
        {
            container.Controls.Add(btn);
        }
    }



您需要对事件进行冒泡.

希望它能工作.它对我有效;)

谢谢,
Hemant



you need to bubble the event.

Hope it will work. it worked at my end ;)

Thanks,
Hemant


这篇关于自定义控件中的GridView RowCommand事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆