Click EventHandler not firing - c#

My newly created button in my gridviewrow is not firing its EventHandler or the RowCmmand Event and then when the page Reload after pressing the Button newly added row is missing probably because I can't call my BindData method. I have removed the Click eventHandler from the Button and just used the OnRowCommand Event and I still am getting nothing
protected void CustomGridView_DataBound(object sender, EventArgs e)
{
int count = ((GridView)sender).Rows.Count;
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
TableCell cell = new TableCell();
Button button = new Button();
button.Text = "Insert";
button.ID = "Insert";
button.CommandName = "Insert";
button.Click += new EventHandler(insertButton_Click);
cell.Controls.Add(button);
row.Cells.Add(cell);
for (int i = 0; i < ((GridView)sender).Columns.Count; i++)
{
cell = new TableCell();
cell.Controls.Add(new TextBox { ID = "Text" + i });
row.Cells.Add(cell);
}
Table table = ((GridView)sender).Rows[0].Parent as Table;
table.Rows.AddAt(count + 1, row);
}
protected void insertButton_Click(object sender, EventArgs e)
{
lblInsert.Text = "Hello World";
}
protected void CustomGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
lblInsert.Text = "Hello World";
}
}

Remove the button.Click += new EventHandler(insertButton_Click); statement from your code. The RowCommand event of GridView controll will be fired when button is pressed. For more info - How to: Respond to Button Events in a GridView Control.

You need to attach to the grid's RowCommand event before the databound. Do it on the constructor or Init event of the page or usercontrol, not in the databound. In the databound you will set the CommandName and CommandArgument properties.
Hope it helps.

Related

Storing value in ASP.NET button

I have this code:
DataTable characterDataTable = character.getAllCharacters();
foreach(DataRow row in characterDataTable.Rows)
{
Button button = new Button();
button.Text = row["character"].ToString();
button.ID = row["character"].ToString() + "_btn";
button.Click = "character_btn_Click";
}
The characterDataTable returns 3 rows with a character and id column for instance char1, char2 and char3. Now I'm trying to create buttons depending on how many rows are retrieved and then trying to set a value to the button so when a button is clicked it will retrieve the value.. I want the value to be set as the ID of the row..
This is what I have at the moment:
protected void character_btn_Click(object sender, EventArgs e)
{
// Get value of clicked button
}
Does anyone know how to store a value in an ASP.NET button and retrieve it when it has been clicked?
To add a click handler to a Button, you should do this:
button.Click += character_btn_Click;
You can use CommandName to pass a "value" to the event handler. So the code becomes:
DataTable characterDataTable = character.getAllCharacters();
foreach(DataRow row in characterDataTable.Rows)
{
Button button = new Button();
button.Text = row["character"].ToString();
button.ID = row["character"].ToString() + "_btn";
button.CommandName = row["id"].ToString();
button.Click += character_btn_Click;
}
The click handler is then:
void character_btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string value = btn.CommandName;
}

how to create button click event in gridview RowDataBound c# asp.net

i am trying to create button and click event in rowdatabound in gridview c# asp.net like below code
protected void btnerror_Click(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.RowDataBound += gv_RowDataBound;
gv.RowCommand += gv_RowCommand;
gv.RowCreated += gv_RowCreated;
gv.EnableViewState = true;
gv.DataSource = _dt;
gv.DataBind();
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnUpdate = new ImageButton();
btnUpdate.ID = "btnupdate";
btnUpdate.ImageUrl = "~/SmartAdmin/Images/update.png";
btnUpdate.ToolTip = "Click Update";
btnUpdate.CommandName = "update";
btnUpdate.Click += btnUpdate_Click;
TableCell tc = new TableCell();
tc.Controls.Add(btnUpdate);
e.Row.Cells.Add(tc);
}
}
void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update")
{
}
}
while click that button click event is not firing ...
where i made error...
thank u.......
Do you really want to create the GridView manually? I strongly doubt that. Instead add it declaratively to the aspx-page and make it visible in btnerror_Click.
Don't create the control dynamically and register the event handler in RowDataBound but in RowCreated which is triggered on every postback (as opposed to RowDataBound):
void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnUpdate = new ImageButton();
btnUpdate.Click += btnUpdate_Click;
TableCell tc = new TableCell();
tc.Controls.Add(btnUpdate);
e.Row.Cells.Add(tc);
}
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnUpdate = (ImageButton)e.Row.FindControls("btnupdate")
btnUpdate.ID = "btnupdate";
btnUpdate.ImageUrl = "~/SmartAdmin/Images/update.png";
btnUpdate.ToolTip = "Click Update";
btnUpdate.CommandName = "update";
}
}
So create it in RowCreated but initialize it in RowDataBound where you can also access the datasource (if required).
Also note that you should DataBind the GridView only if(!IsPostBack) not on every postback (if that's the case). So add this check where you assign the datasource.
You need to add function for click event
btnUpdate.Click += btnUpdate_Click;
protected void btnUpdate_Click(object sender, EventArgs e)
{
}

Create buttons dynamically in asp.net page and fire click event to all buttons

I want to create buttons dynamically in asp.net page and wrote the code. Buttons created dynamically through below code.
List<string> category = new List<string>();
category.Add("AAA");
category.Add("BBB");
category.Add("CCC");
category.Add("DDD");
category.Add("EEE");
for (int i = 0; i < category.Count; i++)
{
TableRow tr = new TableRow();
TableCell cl = new TableCell();
TableCell cl2 = new TableCell();
Button button = new Button();
button.ID = "raid" + i;
button.Text = category[i];
button.Click +=button_Click;
private void button_Click(object sender, EventArgs e)
{
Response.Write(sender.ToString());
}
Now I want to add click events for all buttons and want to get which button click event has been fired.
Any idea?
Response.Write(sender.ToString()); OR Response.Write(e.ToString()); returns common properties.
You can use CommandArgument property.
button.ID = "raid" + i;
button.Text = category[i];
button.Click +=button_Click;
button.CommandArgument +=category[i];
private void button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string category = btn.CommandArgument;
}

RowEditing is fired when update button is clicked in GridView

I have created a GridView as below:
protected void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.ID = pId.ToString();
gv.AutoGenerateEditButton = true;
gv.DataKeyNames = ids;
gv.RowEditing += gv_RowEditing;
gv.RowUpdating += gv_RowUpdating;
bindGv(pId, gv);
}
I have also written the following methods:-
RowUpdating:
void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gv = sender as GridView;
GridViewRow row = (GridViewRow)gv.Rows[e.RowIndex];
ProductCategory pc = context.ProductCategories.First(s => s.Name ==gv.ID );
TextBox txtName = row.FindControl("txtName") as TextBox;
pc.Name = txtName.Text;
}
RowEditing:
void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = sender as GridView;
gv.EditIndex = e.NewEditIndex;
bindGv(Convert.ToInt32(gv.ID), gv);
}
But when I ran the codes in debugging mode, clicking on the update button invokes gv_RowEditing method instead of gv_RowUpdating. What is the problem?
The Sequence of firing the events of gridview when you click on edit button to update any record it calls rowediting event and after that when you click on update button it calls rowupdating event.
rowediting event always invokes first then rowupdating.

Event handler not firing on dynamic button click

I have a dynamically created button with an onclick event handler. The problem is that when I click the button it does not hit the event in the code-behind.
protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ds.Tables[0];
DropDownList ddl = new DropDownList();
TextBox txt = new TextBox();
int index = 1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row.FindControl("ddlNewO") as DropDownList;
txt = e.Row.FindControl("txtNewT") as TextBox;
}
foreach (DataRow r in dt.Rows)
{
string listitem = Convert.ToString(index);
ddl.Items.Add(listitem);
index++;
}
ddl.SelectedIndex = e.Row.RowIndex;
if (e.Row.RowIndex == 0)
{
ddl.Enabled = false;
txt.Enabled = false;
}
else if (e.Row.RowIndex != 0)
{
ddl.Items.Remove("1");
//Create ED button
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnED = new Button();
btnED.ID = "btnED";
btnED.CssClass = "buttonsmall";
//btnED.CommandName = "ED";
btnED.EnableViewState = true;
btnED.Click += new EventHandler(btnED_Click);
foreach (DataRow r in dt.Rows)
{
btnED.Attributes.Add("ID", r.ItemArray[2].ToString());
if (r.ItemArray[3].ToString() == "1")
{
btnED.Text = "Disable";
}
else
{
btnED.Text = "Enable";
}
//Add button to grid
e.Row.Cells[5].Controls.Add(btnED);
}
}
}
}
protected void btnED_Click(object sender, EventArgs e)
{
// Coding to click event
}
So the problem here is that when the page is being recreated on post back - there is no more button! Dynamic controls need to be added on the page on every post back to fire events properly. In your case however on the first load when the GridView is binding you add the button to the page. But on the post back after the click the button is not added again, because GridView is not data bound again. Therefore ASP.NET cannot derive the source of the event, and supresses it.
Fix here is to bind GridView with data on every post back. Literally if you had if (!IsPostBack) - remove it. Or you can add the button in the template field and play with visibility - may be an approach as well.
You need to add a click handler on Row Created not on Data Bound I believe.
protected void gvOrderRowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType) {
case DataControlRowType.DataRow:
Button btn = (Button)e.Row.FindControl("btnED");
btn.Command += btnED_Click;
break;
}
}

Categories

Resources