EventArgs VS GridViewRowEventArgs - c#

I am trying to access the button click event from my asp.net gridview. If I leave my syntax like so
protected void Remove(Object sender, GridViewRowEventArgs e)
{
}
Their is no compile error but I get a debug error. of
No overload for '' matches delegate 'System.EventHandler'
Well if I change my syntax to this
protected void Remove(Object sender, EventArgs e)
{
var command = ((Button)sender).CommandArgument;
if (command.CommandName == "Remove")
{
DataGridItem gr = (DataGridItem)command.NamingContainer;
string abcd = gr.Cells[0].Text;
}
}
I get multiple compile errorrs of:
'string' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'string'
could be found (are you missing a using directive or an assembly reference?)
'string' does not contain a definition for 'NamingContainer' and no extension method 'NamingContainer' accepting a first argument of type 'string'
could be found (are you missing a using directive or an assembly reference?)
What I want to do is from the button press event in my grid view access the value and run a stored procedure.
And here is the aspx markup
<asp:GridView ID="gvwEditDashboard" runat="server" AutoGenerateColumns="False" ShowFooter="true" CssClass="DataGrids"
Width="500px" HorizontalAlign="Center" GridLines="Both" >
<Columns>
<ItemTemplate>
<asp:LinkButton ID = "btnDelete" runat = "server" CssClass="ButtonLink" Text = "[Delete]" OnClick = "Remove" />
</ItemTemplate>
</Columns>
</asp:GridView>

Firstly, understand that GridViewRowEventArgs is meant for events: RowCreated and RowDataBound. These events are not a button click event.
Secondly, when the LinkButton is clicked, there are possibly two events raised: LinkButton.Click and GridView.RowCommand.
My advice is to use/handle the RowCommand event of GridView.
There are many techniques to get the data of the Row where the button was clicked inside this event.
Specifically to your situation, declare the LinkButton as :
<ItemTemplate>
<asp:LinkButton ID = "btnDelete" runat = "server"
CssClass="ButtonLink" Text = "[Delete]" OnClick = "Remove"
CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
Notice the CommandArgument property of LinkButton.
And the RowCommand event. Notice that the second param is GridViewCommandEventArgs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// The RowIndex of the Row where LinkButton was clicked
int rowIndex = Convert.ToInt32(e.CommandArgument);
// Get the GridView Row
GridViewRow row = GridView1.Rows[rowIndex];
// Read the Column values
// e.g. for BoundFields use row.Cells[]
string Column_1_Value = row.Cells[0].Text;
// for Columns defined using <itemTemplate> etc...
// row.FindControl("<Control_ID>")
string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
}
Alternative to LinkButton, you can use ButtonFields as well. Check the row command event at this link. The MSDN sample explains nicely on reading rows data.

protected void Remove(Object sender, EventArgs e)
{
var command = ((Button)sender).CommandArgument;
if (command.CommandName == "Remove")
{
DataGridItem gr = (DataGridItem)command.NamingContainer;
string abcd = gr.Cells[0].Text;
}
}
The hazards of var. CommandArgument is of type string on a button. You're getting that error because string has no CommandName or NamingContainer property.
Try the following:
protected void Remove(Object sender, CommandEventArgs e)
{
if (e.CommandName == "Remove")
{
DataGridItem gr = e.NamingContainer;
string abcd = gr.Cells[0].Text;
}
}

Related

Attaching a click event onto an image within a grid view

I am having trouble attaching a click event onto an image that I have stored within a grid view. Basically it is a delete button that will allow the user to delete a specific row depending on where the button is. I have the code in c# ready for it, however, I cannot seem to attach a click event to it.
This is the markup code of the button
<asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="imgbDeleteP" runat="server" BORDER="0" CausesValidation="false" ImageUrl="~/img/Del.png" Height="25px" ImageAlign="Middle"
onClick ="gv_Quals_RowCommand" CommandArgument="<%#Container.DataItemIndex%>" CommandName="Remove" />
</ItemTemplate>
onClick ="gv_Quals_RowCommand"
Here is the code in c# for the click event
protected void gv_Quals_RowCommand(object sender, GridViewCommandEventArgs e)
{
if ((e.CommandName == "Remove"))
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gv_Quals.Rows[index];
DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
dtCurrentTable.Rows[index].Delete();
if ((dtCurrentTable.Rows.Count < 0))
{
}
else if ((row.Cells[0].Text != "*New*"))
{
int appId = 5000;
//int appId = 1;
string insProg = ("delete from projectunitassignment where UnitId =" + int.Parse(row.Cells[0].Text));
SqlCommand cmd = new SqlCommand(insProg, conn);
cmd.Connection.Close();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
RebindCat(appId);
}
}
}
This is the compilation error that I keep getting
CS0123: No overload for 'gv_Quals_RowCommand' matches delegate 'ImageClickEventHandler'
I cannot set the click event through the properties as it is stored within the grid view so I cannot access it through there. Also the click event does not run as I have tested with debugging
The problem is with GridViewCommandEventArgs should be just EventArgs
public void imgbDeleteP_Click(object sender, EventArgs e)
Edit:
I see that in your code you use the Command Argument, so if you want to use that you should see this post
Basically use onCommand instead of onClick or cast the sender to button to get the command argument, something like:
var argument = ((ImageButton)sender).CommandArgument;
Did you try to associate the click event for that grid during page load ?
I think that is because of GridViewCommandEventArgs which commonly used for RowCommand , change it to EventArgs, so that event should be something like this:
protected void gv_Quals_RowCommand(object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
string cmName= btn.CommandName;
string cmArgument= btn.CommandArgument;
if ((cmName == "Remove"))
{
.....
}
}
or to get row index:
GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
int index = gvRow.RowIndex;
The first parent is the GridView Cell and the second parent of the GridView Cell is the GridView Row.

Gridview on Linkbutton click, Pass complete GridRow information to the next page? What are best ways to do this?

I have Gridview that has 6 other BoundField Columns and one Linkbutton column.
I want to pass all the Row information to the next page when users click on LinkButton.
What are the best ways to do this?
If we pass it as QueryString, it is going to be Too long.
Thanks
You can use the LinkButton Column itself to your advantage.
The idea is to send the RowIndex of the GridViewRow in PostBackUrl property of LinkButton as a QueryString Parameter.
Use the Container.DataItemIndex which represents the Index of the Row.
At the Next/Destination page , get the RowIndex from QueryString to get the GridViewRow and then read the Column values.
Step 1.) The markup corresponding to LinkButton should be as below:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDetails" runat="server" Text="Send Details"
PostBackUrl='<%# "~/NextPage.aspx?RowIndex=" +
Container.DataItemIndex %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Step 2.) In the Page Load event of NextPage.aspx, get first the GridViewRow where LinkButton was clicked and then read the row values
Note that using the PostBackUrl property in essence makes a cross page postback and therefore you can use the property Page.PreviousPage
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
GridViewRow row = GridView1.Rows[rowIndex];
//Since you use Bound Fields, use row.Cells[] to read values
String Column_One_Value = row.Cells[0].Text;
String Column_Two_Value = row.Cells[1].Text;
}
}
Too long is subjective, anyways, if you don't want to use QueryString you can use Session Variables, Session vars are kept across the web site
protected void gvUsers_PageIndexChanging(object sender, EventArgs e)
{
Session["TheSelectedRow"] = ((Control)sender).Parent as GridViewRow;
// Your code to redirect
}
or you can wrap them in Properties, to work better and avoid casting every time in different lines and spelling mistakes
public GridViewRow TheSelectedRow
{
get { return Session["TheSelectedRow"] == null ? null : Session["TheSelectedRow"] as GridViewRow; }
set { Session["TheSelectedRow"] = value; }
}
and then in your click event this
protected void gvUsers_PageIndexChanging(object sender, EventArgs e)
{
TheSelectedRow = ((Control)sender).Parent as GridViewRow;
// Your code to redirect
}

Set SelectedValue of GridDropDownColumn

I'm trying to set the value of a GridDropDownColumn in my RadGrid.
Note that my GridDropDownColumn is not in a template, just part of <columns>.
I'm using the InsertCommand event to do this as my whole experiment is around manipulating data in there.
Frontend:
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView>
<Columns>
<telerik:GridDropDownColumn DataSourceID="MySource" DataField="RowId" UniqueName="RowId" ListValueField="id" ListTextField="Name" SortExpression="RowId" HeaderText="RowId" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
Backend:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e) {
if (e.Item is GridEditFormItem && e.Item.IsInEditMode) {
GridEditFormItem editItem = (GridEditFormItem)e.Item;
DropDownList list = (DropDownList)editItem["GridDropDownColumn"].Controls[0];
list.SelectedValue = HiddenFieldIdToSave.Value;
}
}
This is giving me a rather nasty error though.
Unhandled exception at line 15, column 16485 in http://localhost:55555/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScripts_=;;System.Web.Extensions,+Version=4.0.0.0,+Culture=neutral,+PublicKeyToken=31321323135:en-N:5924cf72drgdrg-a608a92942c5:ea597d4b:b25378d2;Telerik.Web.UI,+Version=2014.2.724.45,+Culture=neutral,+PublicKeyToken=121fae781awdawggcvb31-d2d2285652a6:fghfghfghf:58366029
0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Cannot find cell bound to column with unique name 'GridDropDownColumn'
In your backend C#, you're trying to access a column named GridDropDownColumn, but instead you should be using the row's UniqueName property, which you've set as RowId. Try this:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
{
GridEditFormItem editItem = (GridEditFormItem)e.Item;
// Use the column's unique name as the accessor:
DropDownList list = (DropDownList)editItem["RowId"].Controls[0];
list.SelectedValue = HiddenFieldIdToSave.Value;
}
}
I found out why i was getting this error.
I had 2 dropdown lists which both hooked up on the same datasource, this caused a false value to be passed, this then caused my Insert/Update DataObjectMethod to fire the error.
Also my example was looking at the wrong UniqueName.

asp.net gridview onrowcommand -->( GridViewCommandEventArgs)

I want to ask how to get a text or value of a button inside the gridview?
but i want to get the text value from this onrowcommand which use GridViewCommandEventArgs as its parameter.
as if im using onrowdatabound is (GridViewRowEventArgs), which makes it easy for me to get the button.text inside the gridview
string example = ((Button)e.Row.FindControl("btnStop")).Text;
I want to get the button.text to do an if else loop inside the onrowcommand.
Anyone know how?
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="2" CellSpacing="2" HorizontalAlign="Center" PageSize="5" Width="133%" DataKeyNames="SurveyID" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" onrowcommand="stop_survey"
onrowdatabound="filter_select" onselectedindexchanging="selected"
>
code behind
public void filter_select(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{....
((Button)e.Row.FindControl("btnStop")).Text = "Start";
}
}
public void stop_survey(object sender, GridViewCommandEventArgs e)
{
//i want to get the "btnStop" button text which is nested on the gridview.
}
i want to get the btnStop text, as I want to have different sqlstatement depending on its text (eg. Start or Stop)
the problem is i cant do e.Row inside stop_survey.
Please guide me.
Create a object of GridViewRow from refrence of command source row.
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
But keep in mind that which type of object control is you are using to call event in above code is LinkButton. But if You Calling Rowcommand event from Simple Button then You need to Write this:
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
After Creating Row Object You can simply find any control from Gridview using rowindex.
Label LabelEmail = (Label)GridView1.Rows[row.RowIndex].FindControl("LabelEmail");
Finally You Can Access that Properties of that Control.
LabelResult.Text = LabelEmail.Text;
Same as Above You can also find Datakeys of gridview using GridViewRow rowindex.
int code = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0].ToString());

How can I access DataGridRow from a textbox on that row?

In a DataGrid, when text in a textbox changes I want to add the value of another field in that row to an array.
public void txtTitle_TextChanged(object sender, EventArgs e)
{
TextBox titleBox = (TextBox)sender;
DataGridItem myItem = (DataGridItem)titleBox.Parent.Parent;
string test = DataBinder.Eval(myItem.DataItem, "prod_id").ToString();
}
However myItem.DataItem evaluates as null. I was expecting it to evaluate as DataRowView?
You can get the TextChanged event to fire if you do the following:
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
onitemdatabound="DataGrid1_ItemDataBound">
<Columns>
<asp:TemplateColumn HeaderText="Test">
<ItemTemplate>
<asp:TextBox OnTextChanged="txtBox_TextChanged" ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Name" HeaderText="Test 1"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
You will notice that i have the following properties set:
AutoPostBack="True"
I have also manually added the OnTextChanged="txtBox_TextChanged" to the text box as well.
In my code behind i have:
protected void txtBox_TextChanged(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
Label1.Text = txtBox.Text;
}
The only way the event will fire is when you lose focus on the text box after typing.
Key points to consider:
This will cause a post back, so Ajax might be a good way to keep the user experience nice.
You will need to make sure you wrap your DataBind() in a if (!IsPostBack)
Hope this helps!
Effectively, I solved this by adding an autonumber column to the table, and using the value of this to determine the row's positino in the table, then using the value of this to affect the appropriate row in the datagrid.
I'm now merely changing the color of the row rather than adding values in that row to an array, as stated in the original question.
public void txtPrice_TextChanged(object sender, EventArgs e)
{
TextBox txtPrice = (TextBox)sender;
DataGridItem myItem = (DataGridItem)txtPrice.Parent.Parent;
markRows(myItem, true);
}
public void markRows(DataGridItem myItem, bool toSave)
{
// Prepeare to save this record?
CheckBox thisSave = (CheckBox)myItem.FindControl("chkSave");
thisSave.Checked = toSave;
// Establish the row's position in the table
Label sNo = (Label)myItem.FindControl("SNo");
int rowNum = Convert.ToInt32(sNo.Text) - 1;
CheckBox rowSave = (CheckBox)grid.Items[rowNum].FindControl("chkSave");
// Update background color on the row to remove/add highlight
if (rowSave.Checked == true)
grid.Items[rowNum].BackColor = System.Drawing.Color.GreenYellow;
else
{
Color bgBlue = Color.FromArgb(212, 231, 247);
grid.Items[rowNum].BackColor = bgBlue;
// some code here to refresh data from table?
}
}

Categories