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.
Related
Hello guys i am using gridview ( from obout ) on my asp.net site. When the user clicks on Edit the griview opens up and shows a template for the clicked row. In there i got a save button. When the user clicks on it i got a simple JS script that pop ups a simple alert.
My problem is that now i need to show a different alert on different situations.
I tried putting this on the update event instead but it wont trigger :
Page.ClientScript.RegisterStartupScript(Me.GetType(), "PopUpWindow", popupscript, False)
I am trying to find how can i have full control on the alert the user will see after the update event.
you can handle GridView's RowDataBound event,Try using this js function onclick
protected void Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete LinkButton
LinkButton db = (LinkButton)e.Row.Cells[3].Controls[0];
db.OnClientClick = "return confirm('Are you certain you want to delete this questionnaire?');";
}
}
(OR)
See HERE Example
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
}
}
I'm registering a postback event for each row in an ASP.NET GridView.
protected void gvLRR_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if (cs == null)
cs = Page.ClientScript;
e.Row.Attributes.Add("onclick",
cs.GetPostBackEventReference((GridView)sender, "Select$" + e.Row.RowIndex.ToString()));
}
}
The event that is called upon the user clicking a row is the SelectedIndexChanged of the GridView gvLRR.
PROBLEM:
All of this works just fine so long as I set the page directive EnableEventValidation to false, but if I do not set it to false then the page blows up when the user clicks on a row in the GridView. However, I see this as a bit of a hack, because I shouldn't have to disable event validation just to get postback events to work when clicking upon a GridView row. So is there a better way to go about doing this? Can I somehow register postback events for a row click and somehow manage to have event validation enabled still?
Thank you in advance for the help.
While searching the net, I found this ... click row on gridview and trigger postback c#
HTH :)
I have a button, which when presses populates a grid with data. If I add an ObjectDataSource, and bind the grid to it, it will populate the grid when the page loads. But I need to populate the grid only if the button is pressed, because it is a lengthy opperation. How should I accomplish this
add an event handler to ObjectDataSource's Selecting event like this:
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
{
e.Cancel = true;
}
}
and put a button on page, when it was clicked a postback will occur and ObjectDataSource will return data successfully.
I have a GridView which is databound to an XML Datasource. For one of the columns I'm using a TemplateField and within it's ItemTemplate I have a CheckBox. I need to programatically add an EventHandler to the CheckBox. I was wondering if anyone can tell me which EventHandler from the GridView to use to add a CheckedChanged EventHandler to the CheckBox?
I've tried RowCreated and DataBound and haven't been able to get the CheckBox to postback with the CheckChanged EventHandler.
void gridPartnerSelection_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckbSendEmail = row.Cells[2].FindControl("ckbSendEmail") as CheckBox;
ckbSendEmail.CheckedChanged += new EventHandler(ckbSendEmail_CheckedChanged);
}
}
Thank you.
Turns out I had to set the AutoPostBack property of the CheckBox to True :)
I'm willing to delete this question if it won't be helpful to anyone else.