Row Editing event not firing on Edit button click - asp.net

I have a gridview with edit and delete buttons on each row.
I am trying to change the back color or the fore color of the row in the gridview on which the edit button is clicked.
I am using the Row Editing event .Below is my code, the row editing event is not getting fired when I click the edit button.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
ClearBackColor();
GridView1.SelectedIndex = e.NewEditIndex;
GridView1.SelectedRow.BackColor = System.Drawing.Color.Red;
//GridViewRow row = GridView1.Rows[e.NewEditIndex];
//row.ForeColor = Color.DarkGreen;
}
What could be wrong? Please let me know.

Check http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx. You are probably missing CommandName="Edit" on button, so RowEditing will never fire.

remove this line : GridView1.SelectedIndex = e.NewEditIndex;
right click on gridview, and click properties. Check for "EditRowStyle",expand it, there will be BackColor, choose the color you want, and its done.

Related

Missing selected value from RadioButtonList after PostBack

First the page loads and shows an empty textbox and a button "GO"
Upon clicking the button "GO" a radioButtonList already in the page is loaded from a table based on the text in the textbox.
The radioButtonList is shown with new button "Made my choice".
The user chooses a button and clicks "Made my choice".
Upon checking for selected value or index the radiobuttonList is not checked at all...
That is it
TVM
Ricardo Conte
If you are not using Page.IspostBack property into your Page_Load then Try to use it
if(!Page.IsPostBack)
{
// Your Code..
}
into your Page_Load.Check MSDN
Hope it works for you.
protected void Page_Load(object sender, EventArgs e)
{
// Your code execute always
if(!Page.IsPostBack)
{
// Code which is execute without postback
}
}

Non Gridview event capture edit mode row index

How can I get the index of a gridview row in edit mode from an event that is not a gridview event?
I have a DDL in a cell of the Gridview. When the DDL changes I want to perform an update to a label of another cell in the same row.
So I am using the DDL_SelectedIndexChanged Event and have everything working including the post to the label, but its always the frist row. What is the correct way to get the row index in edit mode from outside of a gridview event?
Thanks,
try this
protected void DDL_SelectedIndexChanged(Object sender, EventArgs e)
{
DropDownList ddlcontrol = (DropDownList) sender;
GridViewRow grrow = (GridViewRow) ddlcontrol.NamingContainer;
int rowindex = grrow.RowIndex;
}

How to move a dynamically created button?

I have a Button1 and a textbox. When i put a value in textbox and click this button then a new button is created with the value filled in textbox but i want that when again i click Button1 and there should be one more button or we can say how to move previous button's position?
This is code for Button1 in .aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
Button btnNew = new Button();
btnNew.ID = "btnNew";
btnNew.Text = textBox1.Text;
form1.Controls.Add(btnNew);
}
Please help me to solve this.
Thanks in advance.
You could probably do something like this with JavaScript/jQuery:
$('#button1').click(function() {
$('#button2').css('position', 'absolute');
$('#button2').css('top', '50px');
});
etc. You should provide more info as suggested by Matt.

Find a GridView row programmatically

The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}

asp.net access delete button of gridview

How to access gridview commandfield delete button on RowDataBound event?
How the cells and controls in griview are accessed
Please try the code below. This is for adding a delete confirmation. But you can use it for anything you want.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[1].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete?');");
}
}
HTH
See:
protected void YourGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Control button = e.Row.FindControl("btnSubmit");
if (button != null && checkBox is Button)
{
// do what you want
}
}
In RowDataBound event you may access row inner controls through FindControl method.
In the example above I assumed that you control is a Button control with btnSubmit identifier.
Edit: after the author's problem additional explanation:
(ButtonType)e.Row.Cells[commandFieldIndex].Controls[controlIndex];
ButtonType is the type of button being used by the CommandField - Button, LinkButton, or ImageButton. By default, the CommandField uses LinkButtons, but this can be customized via the CommandField’s ButtonType property.

Resources