Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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 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?
Posted
Updated 1-May-11 21:32pm
v2

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
 
Share this answer
 
// Generate Row Delete Event and then write following Code

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

     }
 
Share this answer
 
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

Edit:
C#
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
C#
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);
        }
    }


you need to bubble the event.

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

Thanks,
Hemant
 
Share this answer
 
v3
Comments
Mada Naga Sankar 2-May-11 4:48am    
I used one ImageButton for delete option. it is in templatefield. i need to write code in custom control only. But its not firing rowcommand event whenever i clicked on imagebutton.is it possible to fire that event?
Hemant__Sharma 2-May-11 5:41am    
My question remains same have you implemented TemplateField and custom control class with INamingContainer?

you dont have to implement any method of INamingContainer bcz it doesn't contain any.

Now you must be aware of event bubbling your ImageButton should raise the event to gridview so it can understand that something has raised and need to catch up.

Pleae do one thing try adding a grid view to some aspx page and in the code behind of that page add your template field to any column and see if it is raising. don't forget to implement TemplateField class with INamingContainer. put some point in click event of imagebutton.

provide code for templatefield class also
Mada Naga Sankar 2-May-11 6:18am    
[DefaultProperty("Text")]
[ToolboxData("<{0}:GridViewWithCommands runat="server">")]
public class GridViewWithCommands : WebControl, INamingContainer
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
GridView gvData = new GridView();
LinkButton lnk = new LinkButton();
private string connectionString = "";

public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}

private string query = "";

public string Query
{
get { return query; }
set { query = value; }
}

private string test = "initial";

public string Test
{
get { return test; }
set { test = value; }
}

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 void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
{
test = "ok";
}
protected override void RenderContents(HtmlTextWriter output)
{
gvData.RenderControl(output);
}
protected override void OnLoad(EventArgs e)
{

}
}

This is the test code i done. In this i taken one test variable and changing the value on rowcommand. In a webpage i am displaying the test property of this control for testing purpose. But its not changing the test value.means its not firing.Any modification is required?
Hemant__Sharma 2-May-11 7:20am    
See the updated answer it has the code.
Hemant__Sharma 2-May-11 9:36am    
Whoever has down-voted can give a proper solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900