Command behind a GridView ButtonField - c#

I got a question about the command behind a ButtonField (image type) in a GridView.
Got a GridView called gvIngevuld in wich I show rows of data and 1 ButtonField row.
Now I have to put code behind these buttons (Each of them the same) to put some things in PDF format.
The problem is I don't know how to put code behind these buttons ?
The code I have :
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png" CommandName="pdf_click" />
As you can see I used the CommandName="pdf_click" property but when I click one of the buttons there's only a postback but nothing happens.
Anyone who can help me out here ?
in case needed, code behind :
protected void pdf_click(object sender, EventArgs e)
{
lblWelkom.Text = "Succes!";
}
Thanks.

You should use RowCommand event of gridview. Set the commandArgument to your item's unique key. (By default, it passes the row's index)
void gvIngevuld_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="pdf_click")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = gvIngevuld.Rows[index];
//gbIngevuld is your GridView's name
// Now you have access to the gridviewrow.
}
}
Also, If you have set the DataKeyNames of the gridview, you can access it as follows:
int index = Convert.ToInt32(e.CommandArgument);
int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);

Just an addition, I wrote and tried exactly the same however the button still not working...
...it took some time till I realise that I forgot a thing, which was
OnRowCommand="<YourGridViewName>_RowCommand"
inside your GridView element in the **.cs* file

The CommandName property does not define the method to call. For that you need to create a method that is bound to the command event of the control.
I don't know too much about about GridViews i'm afraid but I would think the easiest way to do this would be to select it in design view in Visual Studio, go to properties, click on the events button (looks like a lightning flash) and double click on the RowCommand event propety. This should automatically create a method that is bound to a command button event.
You can then use the GridViewCommandEventArgs parameter to access the CommandName, CommandArgument and CommandSource properties to work out which buttom caused the event to fire.

Related

C# gridview row onclick

I notice there is a "AutoGenerateSelectionButton" function but it's not really what I want. I want to be able to two things when a row is clicked (anywhere of the row):
change the color of that entire row.
get the value of a specific column and update another table accordingly.
How can I acheive that without writing client-side javascript functions?
Assuming this is a webform, you need to access SelectedIndexChanged event of the gridview using a codebehind file.
from here you can modify properties
protected void ChangedRow(object sender, EventArgs e)
{
this.GridView1.SelectedRow.BackColor = System.Drawing.Color.Red;
....
}

Read a dynamically created textbox in a Gridview

I am dynamically adding a textbox to certain rows (one column only) of a gridview. I add the controls with this insdie of a test condition (works fine):
TextBox txtASIN = new TextBox();
txtASIN.ID = "TxtASIN" + e.Row.RowIndex;
e.Row.Cells[4].Controls.Add(txtASIN);
int i = e.Row.Cells[4].Controls.Count; //TEST: This returns 1 correctly
I want the user to be able to enter values into one or more of these textboxes and then update the database with a single button click (not one click for each row). The problem I'm having is how to access those values on a button click event. I did a simple test this way to try to see the value in the second row but get null in temp1 (I am certain there is a value entered in that textbox):
protected void btnUpdate1_Click(object sender, EventArgs e)
{
TextBox temp = (TextBox)GridView2.Rows[1].FindControl("txt1");
string temp1 = temp.Text;
int i = GridView2.Row.Cells[4].Controls.Count; //TEST: This returns 0 incorrectly }
Once I can make this work, I can iterate through the rows and do what I need to do with the values. I don't know if the text entered in the textbox is actually readable without a postback but I'm otherwise stumped. Open to better suggestions on how to do this.
Thanks.
EDIT: Here is where I am now. I can see the textboxes in my column fine. I put a break in on a button that attempts to read them and this is what I'm seeing. If I check GridView2.Rows[0].Controls.Count, I get 8, which is the correect number of columns. If I check GridVeiw2.Rows[0].Cells[4].Controls.Count, I get 0, which is wrong because the textbox is there. I can get a Count of 1 right after I dynamically create it but not when I perform a subsequent button click.
Can anyone explain this? I feel if I can get past this holdup, I can get the rest done.
Thanks again.
You need to assign an ID to the TextBox controls and then access them by that ID in FindControl(). Also, make sure you're adding the controls in the Page's Init() method of the life-cycle. That way it gets added to ViewState.
TextBox txt1 = new TextBox();
txt1.ID = "txt1";
e.Row.Cells[4].Controls.Add(txt1);
EDIT: I just remembered another possible solution. Instead of programatically creating the TextBox controls in the code-behind just create a TemplateField in the GridView.
Add Textbox TemplateField Column To GridView Programmatically
I would try a different approach, putting the text box in the html markup of the page and then control the visible or readonly property of it on the ItemDataBound event. That way, the control will always be there and you don't have to worry about the lifecycle stuff.

How to retrive a Control in a specific ROW in edit mode in a GridView

I have a GridView with many rows.
When a User click the EDIT button in GridView I need to retrieve a Control in that specific row (now in edit mode).
This Logic should work on GridEvent _RowUpdating
At the moment my code (wrong) look inside every Row, so the Control founded is not unique and I receive an error.
// Event handler
protected void uxManageSponsoredContentsDisplayer_RowUpdating(object sender, GridViewUpdateEventArgs e)
// My code (Wrong!!!!):
foreach (GridViewRow row in uxManageSponsoredContentsDisplayer.Rows)
{
TextBox uxStartDate = (TextBox)row.FindControl("uxEffectiveStartDateInput");
}
Hope my question is clear. Any idea how to do it? Thanks
Solution:
TextBox uxStartDate = (TextBox)uxManageSponsoredContentsDisplayer.Rows[e.RowIndex].FindControl("uxEffectiveStartDateInput");
You need to use the GridViewUpdateEventArgs e as it contains index of row being updated.
Use something like
uxManageSponsoredContentsDisplayer.Rows[e.RowIndex].FindControl("uxEffectiveStartDateInput")

Datagrid button column how to bind a function to a button?

Currently i have a datagrid view that displays names in the 1st columnnd in the 2nd column delete buttons.
This datagrid view already has bind data to a function so it can displays the names that are withint a xml file (first column is a hyperlink column).
But next i want to be able to delete the xml values that are in the first column.
(by clicking on the delete button on the second column of it)
<name></name>
But how do i exactly bind a (delete) function to these buttons?
name1 btnDelete
name2 btnDelete
etc...
Thanks in advance.
For datagrid you have handle the ondeletecommand event, see below.
<asp:DataGrid runat="server" ID="dg" ondeletecommand="dg_DeleteCommand">
then define the event handler in your code behind .. see below.
protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
{
}
the DataGridCommandEventArgs has all the properties you'll need to check including e.CommandName etc
Hope this helps.
I don't know about datagrid but in gridView I use
set button's command name and use GridView's RowCommand Event.
if(e.commandName == "delete")
//ur code;
Template field
for default delete of GridView use
girdViewRowDeleting
Hope it will help..

How to add a reference to a method in a custom control to a child control created in that same custom control

I have a custom control that is based off the gridview control located at: here
The control is basically a gridview that automatically creates a column of checkboxes that can be used to "check" individual rows in the gridview.
During the gridview's "CreateColumns" event, the "checkboxcolumn" is created dynamically. The checkboxcolumn also contains another checkbox in the header that is used to "select/deselect all" checkboxes in the column.
Since the gridview does not automatically remember the state of the checkboxes in the checkboxcolumn on postback, I added a method to the control called "SaveCheckBoxState" which stores the indexes of the checked rows in Viewstate, and then I modified the "OnRowDataBound" event to check the Viewstate and reset the checkboxes based on the Viewstate.
I then added a call to "SaveCheckBoxState" in the gridview's OnSorting and OnPageIndexChanging events. This works great so long as I'm sorting or changing pages.
However, I need it to update the viewstate everytime someone clicks or unclicks one of the checkboxes. At this time, the checkboxes are rendered with an onclick event that calls some javascript to highlight the row, or in the case of the checkbox in the header, to select/deselect all checkboxes.
I need to call the "SaveCheckBoxState" method from the javascript used by the customcontrol, or I need to find a way to modify viewstate from javascript and perform the same action as "SaveCheckBoxState".
I've tried adding the "SaveCheckBoxState" to the onclick event declaration in the checkboxes, but when run, it simply tells me that the method is undefined. It doesn't exist in the parent page, and I don't think I should have to make an event for the parent page to pass the click to. It seems to me this should be all self contained within the custom control.
Does anyone know how I can acheive this?
Here is the code for the gridview OnPreRender event where the onclick event of the checkbox is set:
protected override void OnPreRender(EventArgs e)
{
// Do as usual
base.OnPreRender(e);
// Adjust each data row
foreach (GridViewRow r in Rows)
{
// Get the appropriate style object for the row
TableItemStyle style = GetRowStyleFromState(r.RowState);
// Retrieve the reference to the checkbox
CheckBox cb = (CheckBox)r.FindControl(InputCheckBoxField.CheckBoxID);
// Build the ID of the checkbox in the header
string headerCheckBoxID = String.Format(CheckBoxColumHeaderID, ClientID);
// Add script code to enable selection
cb.Attributes["onclick"] = String.Format("ApplyStyle(this, '{0}', '{1}', '{2}')",
SelectedRowStyle.CssClass,
style.CssClass,
headerCheckBoxID);
// Update the style of the checkbox if checked
if (cb.Checked)
{
r.BackColor = SelectedRowStyle.BackColor;
r.ForeColor = SelectedRowStyle.ForeColor;
r.Font.Bold = SelectedRowStyle.Font.Bold;
}
else
{
r.BackColor = style.BackColor;
r.ForeColor = style.ForeColor;
r.Font.Bold = style.Font.Bold;
}
}
}
You should have your custom GridView control subscribe to the CheckChanged event of the checkboxes. It should call its SaveCheckBoxState method on its own. That method should be private.
Parent and child controls should only communicate via properties and events, never through methods and fields.

Categories