How to pass CommandName of LinkButton in GridView to OnClick event - c#

Is it possible to pass the CommandName on my LinkButton to the OnClick event within my GridView in Asp.net Webforms (C#)?
<asp:LinkButton ID="DownloadFile" runat="server" Text="Download" OnClick="OpenModal" CommandArgument='<%# Eval("Customer_ID") %>' CommandName="PassThisName"></asp:LinkButton>
I know it's possible in the RowCommand of the GridView like this:
if (e.CommandName == "PassThisName")
{
}
But I need to get this value in the OnClick event.

It's quite easy, you can do it like this:
Markup
<asp:GridView runat="server" ID="grdCustomer">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DownloadFile" runat="server" Text="Download" OnClick="DownloadFile_Click" CommandArgument='<%# Eval("Customer_ID") %>' CommandName="PassThisName"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
protected void DownloadFile_Click(object sender, EventArgs e)
{
LinkButton lnkDownloadFile = (LinkButton)sender;
string commandName = lnkDownloadFile.CommandName;
}

Related

how will i transfer a value from a gridview of a previous page to another web form?

how can i pass the value from the gridview to the next webform? here is my gridview code below:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?RowIndex=" + Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate
</asp:TemplateField>
but i can't seem to make it work. it goes to the next form but does not get the value. my id has a format "1000001" and it does read it.
here is my code for the next page. i created a new gridview just to store the value from.
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView gv1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
GridViewRow row = GridView1.Rows[rowIndex];
lblID.Text = row.Cells[0].Text;
Add OnRowCommand="GridView_RowCommand" like
<asp:GridView ID="GridView" OnRowCommand="GridView_RowCommand"
Update your Columns as
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" CommandName= "Redirect" CommandArgument='<%#Eval("ID")%>' ></asp:LinkButton>
</ItemTemplate
In Code-Behind file
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Redirect") {
//Get Command Argument
Response.Redirect("Details.aspx?RowIndex="+e.CommandArgument.ToString(),true);
}
}

Grid View Row Command Event Not Working

I am having GridView control inside Wizard control, and I am having a linkbutton inside grid, clicking on which will change active index of wizard.
I have three GridViewControls and I am using same Event for RowCommand of these Grids, but its not working, I tried applying breakpoint but its not hitting the break point.
This is my code
w ID="GVUsers" runat="server" OnRowDataBound="GVUsers_RowDataBound" OnRowCommand="GVUsers_RowCommand"
OnRowDeleting="GVUsers_RowDeleting" AutoGenerateColumns="false" CssClass="table">
<Columns>
<asp:TemplateField HeaderText="Crimes" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Username" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Username") %>'></asp:Label>
<asp:Label ID="gender" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Gender") %>'></asp:Label>
<asp:Panel ID="divmsg" runat="server">
<asp:LinkButton
ID="btnlnkpg18" runat="server" Text="Click here" CommandName="pg18"></asp:LinkButton>
</asp:Panel>
</ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="delbtn" runat="server" Text="Delete" OnClientClick="return confirm('Do you really want to delete?');"
CommandName="delete" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "UserId") %>'
CssClass="DeleteBtn"></asp:LinkButton>
</ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
protected void GVUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
var lblgender= e.Row.FindControl("gender") as Label;
var divlnk=e.Row.FindControl("divmsg") as Panel;
if(lblgender.Text=="M")
divlink.Visible=true;
else
divlink.Visible=false;
}
}
protected void GVUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Delete
}
if (e.CommandName == "pg18")
{
Wizard1.ActiveStepIndex = 16;
}
}
I also tried setting CommandName in RowDataBound but no luck, Also the Delete Button is not working.
I am databinding GridView like this
if(!Page.IsPostBack)
{
//Bind GridView
}
Syed, try doing this:
1) Rename your RowDataBound event to this:
protected void temp()
2) Go to the aspx page and delete the RowDataBound property from the gridview.
3) With the gridview still highlighted, click design view.
4) In properties click on 'Events' (the lightning bolt).
4) Double click inside the box for RowDatabound to create the event handler in the .cs
5) With in the RowDatabound inside the .cs add a reference to temp();
6) Add a breakpoint just about the temp() within RowDatabound and run your code.
See if it hits the code, if it does then copy everything from test into the RowDataBound.

How to get gridview column value in codebehind

How to get the AppId from gridView in codebehind, if I clicked the edit image button in second row.
Aspx Code:
<asp:BoundField HeaderText="AppId" DataField="AppID" />
<asp:TemplateField HeaderText="Actions" ControlStyle-Width="20px" ItemStyle-Width="130px">
<ItemTemplate>
<asp:ImageButton ID="imgMailCamp" runat="server" ImageUrl="~/Images/AppSetup/Mail.png"
Height="18px" ToolTip="Send Mail Campaign" CssClass="grdImageAlign" CommandName="SendMail" OnClick="btnMailCamp_Click" />
<asp:ImageButton ID="imgViewApp" runat="server" ImageUrl="~/Images/AppSetup/application-view-list-icon.png"
Height="18px" ToolTip="View Appplication" CssClass="grdImageAlign" CommandName="View" OnClick="btnView_Click" />
<asp:ImageButton ID="imgEditApp" runat="server" ImageUrl="~/Images/AppSetup/Action-edit-icon.png"
Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" CommandName="Edit" OnClick="btnEdit_Click"/>
<asp:ImageButton ID="imgDeleteApp" runat="server" ImageUrl="~/Images/AppSetup/Trash-can-icon.png"
Height="18px" ToolTip="Delete Application" CssClass="grdImageAlign" CommandName="Delete" OnClick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
C# Code:
protected void btnEdit_Click(object sender, EventArgs e)
{
// I need to get the current row appId, I use this appId in next page for sql query
Response.Redirect("/Secured/EditApplication.aspx?AppID="+AppID);
}
Try Like this....Don't Define Click Event of Button....Define Button Like this...
<asp:ImageButton ID="imgEditApp" runat="server"
ImageUrl="~/Images/AppSetup/Action-edit-icon.png"
Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign"
CommandName="Edit"/>
And
Define Your GridView RowEditing event Like this....
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[e.NewEditIndex].Cells[1].Text);
}
Edit:
I think you have problem in definig RowEditingEvent.....ok you can do this...nothing to change just write this code in you Click event...
protected void btnEdit_Click(object sender, EventArgs e)
{
ImageButton ib = sender as ImageButton;
GridViewRow row = ib.NamingContainer as GridViewRow;
Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[row.RowIndex].Cells[1].Text);
}
Edit 2
<asp:ImageButton ID="imgEditApp" runat="server"
ImageUrl="~/Images/AppSetup/Action-edit-icon.png"
Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign"
CommandName="Edit" CommandArgument='<%#Eval("AppID") %>'/>
protected void btnEdit_Click(object sender, EventArgs e)
{
string appid= (sender as ImageButton).CommandArgument;
Response.Redirect("/Secured/EditApplication.aspx?AppID="+appid
}
You can get grid view cell value from this.
GridView.Rows[RowIndex].Cells[CellIndex].Text
Here "RowIndex" is row number from which you want to get data and "CellIndex" is cell number from which you want to get data.
I think event "OnRowCommand" of gridview is best suited for your problem.
use blow link for more details
http://www.codeproject.com/Tips/564619/Example-of-gridview-rowcommand-on-Button-Click
it should be with commandargument
aspx
<asp:ImageButton ID="imgEditApp" CommandArgument='<%# Eval("AppID") %>' runat="server" ... OnClick="btnEdit_Click"/>
code
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
int categoryId = Convert.ToInt32(e.CommandArgument);
Response.Redirect("/Secured/EditApplication.aspx?AppID="+categoryId);
}
or u can use PostBackUrl property of imagebutton and it would be like this:
<asp:ImageButton ID="imgEditApp" PostBackUrl='<%# string.Format("/Secured/EditApplication.aspx?AppID={0}", Eval("AppID")) %>' runat="server" />
Check this code snippet.
This is the code in aspx file having two columns DataBound "AppId" and TemplateColumn "Action" containing Image Button. Observe CommandName and CommandArgument properties of Image Button. Also Define OnRowCommand Event listener for gridview.
<asp:GridView ID="grdDisplayData" runat="server" AutoGenerateColumns="false"
EnableViewState="false" onrowcommand="grdDisplayData_RowCommand">
<Columns>
<asp:BoundField HeaderText="AppId" DataField="AppId" />
<asp:TemplateField HeaderText="Action" >
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="MyEdit"
CommandArgument="<%# ((GridViewRow) Container).RowIndex%>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ImageAction">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Width="15px" Height="15px"
CommandName="ImgEdit" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is the code behind code. The e.CommandArument returns the index of the row in which the image button was clicked.
protected void grdDisplayData_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "ImgEdit")
{
int RowIndex = Convert.ToInt32(e.CommandArgument);
Response.Redirect("/Secured/EditApplication.aspx?AppID=" + grdDisplayData.Rows[RowIndex].Cells[1].Text.Trim());
}
}
Let me know if this fixed your problem.
Cheers!!!
Piyush Deshpande

How to reference a linkbutton thats inside a asp:grid when its clicked

I have a ASP:grid which has a link button, i need to some how reference that on the code behind when its clicked but im struggling on the syntax
Heres my ASP:Grid i need to execute code in the code behind when that link button 'Re-Take' is pressed and also be able to know what row it was clicked on as i will need to reference the users emails and name and then send an email with the relevant information....
<asp:GridView ID="GrdViewUsers" runat="server" AutoGenerateColumns="false" GridLines="None"
EnableViewState="false" class="tablesorter">
<AlternatingRowStyle></AlternatingRowStyle>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Re-Take" runat="server" ID="Edit" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<asp:HyperLink ID="HyperFullName" CssClass="gvItem" runat="server" NavigateUrl='<%#Eval("UserID","/ExamPaper.aspx?uid={0}") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.FullName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblSurname" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Taken">
<ItemTemplate>
<asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExamTaken") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Taken">
<ItemTemplate>
<asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DateTaken") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Total">
<ItemTemplate>
<asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExamTotal") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If someone can help me with a snippet i would highly appreciate it
You could approach this slightly different. You see, when a control is placed inside a gridview, any event raised from that control raises also the RowCommand on the GridView.
To get what you want you could then add both CommandName and CommandArgument to your LinkButton and then catch it in the GridView's RowCommand.
<asp:LinkButton id="LinkButton1" runat="server" commandName="LinkButtonClicked" commandArgument='Eval("myObjectID")' />
where myObjectID is the name of the ID column of your object you bind the grid to.
Then
void GridView1_RowCommand( object sender, GridViewCommandEventArgs e )
{
if ( e.CommandName == "LinkButtonClicked" )
{
string id = e.CommandArgument; // this is the ID of the clicked item
}
}
see: ASP.net GridView: get LinkItem's row
FindControl should work in this case.
protected void GrdViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myHyperLink = e.Row.FindControl("Edit") as HyperLink;
}
}
First: You have repeating ID's in your TemplateFields like lblUsername what is not allowed since it's the same NamingContainer.
You can pass the RowIndex as CommandArgument to RowCommand:
on aspx:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton
Text="Re-Take"
runat="server"
ID="Edit"
CommandName="Edit"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Handle the GridView's RowCommand:
void GrdViewUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GrdViewUsers.Rows[index];
// now you can get all of your controls like:
Label lblSurname = (Label)row.FindControl("lblSurname");
String email = lblSurname.Text // you noticed that DataItem.Email is bound to lblSurname?
}
}
Suppose the grid has linkbutton on which we want to get row index
<asp:LinkButton ID="lnkbtnAdd " runat="server" CommandName="cmdAdd" ImageUrl="~/Images/add.gif" ></asp:LinkButton>
In code behind, In OnRowCreated event we attach row number of grid to each button of row to get it back when it is clicked in RowCommand event
protected void gvListing_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Web.UI.WebControls.LinkButton lnkbtnAdd = new System.Web.UI.WebControls.LinkButton();
lnkbtnAdd = (System.Web.UI.WebControls.LinkButton)e.Row.FindControl("lnkbtnAdd");
if (lnkbtnAdd != null)
lnkbtnAdd .CommandArgument = e.Row.RowIndex.ToString();
}
}
In RowCommand event we will get back the current row index and set the selected index of the grid
protected void gvListing_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToString() == "cmdAdd")
{
int RowIndex = int.Parse(e.CommandArgument.ToString());// Current row
}
}

How can I get the BoundField from a GridView

I have the following GridView
<asp:GridView ID="grdImoveis" runat="server" DataSourceID="dsGrid">
<Columns>
<asp:BoundField HeaderText="Nome" DataField="NomeCompleto" />
<asp:ImageButton ID="ibtnAlterar" ImageUrl="../tema/_Internas/icons/edit.png" runat="server" OnClick="btnChange_Click" />
</Columns>
</GridView>
How can i get the value of this field in code behind and pass the value to my btnChange_Click event ?
If your asking what I think, you'd like want to handle the OnRowCommand of your GridView and capture the button action in there.
<asp:GridView ID="grdImoveis" onrowcommand="grdImoveis_RowCommand" ...
Code-Behind:
protected void grdImoveis_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "BUTTON")
{
// Check value of e.CommandArgument and do something here:
}
}
And change your imagebutton to something like so by setting CommandName and CommandArgument to the value you want to pass. You also have to wrap it in a TemplateField:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton CommandName="BUTTON" CommandArgument='<%#Eval("NomeCompleto") %>' ID="ibtnAlterar" ImageUrl="../tema/_Internas/icons/edit.png" runat="server" />
</ItemTemplate></asp:TemplateField>
Use CommandArgument property like this
CommandArgument='<%# Container.DataItemIndex %>'
inside a TemplateField and then in your code behind 'OnRowCommand' event handler use it like this:
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
object dataItem = gv.Rows[int.Parse(e.CommandArgument.ToString())].DataItem;
}

Categories

Resources