Powered By Blogger

Monday, March 15, 2010

Gridview Row Editing Event , Row Deleting Event, Selected Index Changed Event


Get Values in Row Editing Event

string Val= GridView1.Rows[e.NewEditIndex].Cells[1].Text;

Get Values in Row Deleting Event

string Val=GridView1.Rows[e.RowIndex].Cells[1].Text;

Get Values in SelectedIndexChanged Event

string Val=GridView1.SelectedDataKey["Fs_EmpCode"].Tostring();


Hide Columns in a GridView

GridView1RowCreated Event

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].CssClass = "hiddencol";
e.Row.Cells[2].CssClass = "hiddencol";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].CssClass = "hiddencol";
e.Row.Cells[2].CssClass = "hiddencol";
}


StyleSheet:

.hiddencol
{
display:none;
}


Client Click in Gridview command field

Gridview_RowDataBound Event

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}

Important Note:

The important bit here is to use exactly the OnClientClick
content provided (except the wording in the message, of course). If you,
for example, instead, had "return confirm('Are you sure you want to delete the
record')"
like many others have suggested, this would
mean the Delete command would never be posted back to the server. The
actual onclick function call generated/rendered by ASP.NET






No comments:

Post a Comment