Gridview button not firing its method - c#

I populate a gridview called grdOrder by using a method which uses a series of other classes and stored procedures (it's so ugly that even I don't understand because it's given by our professor and he's confused us on whole new level)
I then want to delete an item from the database based on value (OrderId) which is in the second rows in the GridView once it is populated.
In the row editor of Gridview I created a button and when I double click the gridview in design view it creates the follow
protected void grdOrder_SelectedIndexChanged(object sender, EventArgs e)
So that is what I think should be firing when I hit the button remove on the Gridview. So I put a break underneath that method and it does not fire when I do that on the form in the browser. So it's not working basically.
If you want to see more of what's going on here is the full codes
http://pastebin.com/iSvyJCPQ - Aspx page
http://pastebin.com/LVRvaCnu - Code Behind (C#)

use the Gridview.RowCommand event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

Related

Access GridView inside another GridView from code-behind

I create in ascx page 3 GridViews, like:
<dxwgv:ASPxGridView ID="grid1" ..... >
<dxwgv:ASPxGridView ID="grid2" .... >
<dxwgv:ASPXGridView ID="grid3" ....>
</dxwgv>
</dxwgv>
</dxwgv>
But in code-behind I see only first grid (grid1) ID and can control only it. How to use others?
I think
GridView grid2 = (GridView)grid1.FindControl("grid2")
GridView grid3 = (GridView)grid2.FindControl("grid3")
should work.
You will not see the other gridviews since they are hidden in the first grid view, to access the other gridviews you should do the following:
create by code two grid view controls lets say name them: GVsubone
and GVsubtwo
in the RowDataBound of the first gridview (the one visible for you) make your GVsubone handles the events of your grid2 like this grid2.RowDataBound += new EventHandler(GVsubone.RowDataBound);
and then in the GVsubone RowDataBound you have to do the same logic to handle the events for grid 2
P.S. you can handle any event RowDataBound was an example.
A better solution is to assign unique IDs (and ClientInstanceNames), as well as scripts, to controls at runtime.
This approach is described in the following Knowledge Base article: The general technique of using the Init/Load event handler.
and then another approach is that handle the ASPxGridView.DataBound event of the detail grid and get a reference to the grid via the sender parameter. Here you can call the ASPxGridView.FindDetailRowTemplateControl method of the master grid if you are using Master Details.
If you are using GridView's DataRowTemplate then use the
ASPxGridView.FindRowTemplateControl Method, you just need to get
the visibleIndex of the row and you are able to access the grid with
it's name.
If you are using Coloumn template then use ASPxGridView.FindRowCellTemplateControl Method
protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
if(e.DataColumn.FieldName == "title") {
ASPxTextBox textBox = ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "ASPxTextBox1") as ASPxTextBox;
textBox.Text = Convert.ToString(e.CellValue);
}
}
Reference these:
ASPxGridView - How to access controls inside DetailRow on the client side

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;
....
}

DropDownList not issues in asp.net

I have two DropDownList which are being populated from a database(SQL) on Page_Load. Now I want to take the Text/Value selected from each DropDownList and insert them into a database. But when I click on the button and the OnClick Event is action the DropDownLists go back to the original state and not the selected values get inserted, instead I get the first Text/Value of the DropDownList all the time. How can I prevent this from happening? Any help will really appreciate it.
I'm using C# for this.
Thanks
In page load, load up the dropdowns like this
protected void Page_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropDowns();
}
}
Basically button click causes postback and if you are populating controls in Load event without check for postback, it will repopulate your controls resetting it's state.
Your DDL is getting rebuilt on every page load. You need to wrap your ddl data source calculation in an
if (!IsPostBack)
or put it in a part of the lifecycle that only loads once, like the OnLoad()

GridView RowCount late update updatepanel

I have a gridview in an updatepanel where also my dropdownlist is. From the Trigger of the dropdownlist I am refreshing my gridview with the slected value. All that is working fine. The problem is that I am also displaying the gridview row count on the page which is also inside the updatepanel. The update seems to be happening to it, one selection too late.
protected void Drop_Change(object sender, EventArgs e)
{
String Value = AjaxDrop.SelectedValue;
GridView1.SelectParameters["Target"].DefaultValue=Value;
RowCount.InnerText = GridView1.Rows.Count.ToString();
}
I think its happening one selection behind because the parameter updates the gridview rowcount too late for the RowCount value to have it, what is a work around to get the actual value after parameter passed. Only way I can think of is using javascript and I wonder if that would even work. My desired solution would be to keep it all on server side though.
At the time you're calling this, the GridView hasn't done it's databinding, therefore the value is not valid. Move your RowCount.InnerText updating to somewhere that gets executed after the DataBinding event (e.g. Page_OnPreRender), or force the DataBinding to occur before you update the row count.
It's worth noting that GridView.Rows.Count isn't a reliable source of information if you are using GridView paging - as this will be the number of rows on one page, even if there are more rows returned by the query.

Command behind a GridView ButtonField

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.

Categories