How can I get the BoundField from a GridView - c#

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

Related

How to pass CommandName of LinkButton in GridView to OnClick event

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

How to get value of a gridview cell in this scenario

I have a link button inside a gridView as below :
<asp:TemplateField HeaderText="Analyze">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Analyze" runat="server" OnClick="LinkButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
I have the LinkButton1_Click function as below :
protected void LinkButton1_Click(object sender, EventArgs e)
{
testtb.Text = name;
Console.WriteLine(name);
}
This variable "name" is the first column of the GridView.I am obtaining the value for "name" as below:
protected void UnanalysedGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView d = (DataRowView)e.Row.DataItem;
name = d["resultId"].ToString();
}
}
I want that on click of the link button the value of first column in that row of gridview becomes the text for the textbox testtb.
Somehow,the value for name remains null.
EDIT
I found out that probably RowDataBound isn't the correct event for my requirements because I need the value for each row.
So I removed the RowDataBound function.
I guess I have to handle this inside LinkButton1_Click itself.
I added this line to the function :
name = UnanalysedGV.SelectedRow.Cells[1].Text;
Still doesn't work.
Does anyone have any idea ?
You can do it using two approaches as mentioned below.
Handle click event of linkbutton through event bubbling code -
<asp:GridView AutoGenerateColumns="false" runat="server" ID="grdCustomPagging" OnRowCommand="grdCustomPagging_RowCommand">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="RowNumber" />
<asp:BoundField DataField="DealId" HeaderText="DealID" />
<asp:BoundField DataField="Dealtitle" HeaderText="DealTitle" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("DealId") %>'
CommandName="VIEW">View Deal</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void grdCustomPagging_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "VIEW")
{
LinkButton lnkView = (LinkButton)e.CommandSource;
string dealId = lnkView.CommandArgument;
}
}
Handle click event of linkbutton directly click event code -
<asp:GridView AutoGenerateColumns="false" runat="server" ID="grdCustomPagging">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="RowNumber" />
<asp:BoundField DataField="DealId" HeaderText="DealID" />
<asp:BoundField DataField="Dealtitle" HeaderText="DealTitle" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkView" OnClick="lnkView_Click">View Deal</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void lnkView_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string rowNumber = grdrow.Cells[0].Text;
string dealId = grdrow.Cells[1].Text;
string dealTitle = grdrow.Cells[2].Text;
}
Hope this helps.

Grid View selected index

I have a grid which contains a edit button . When i click the edit button and debug it does not hit to the selected index change event . There are no build errors
code behind the grid
public void btnModemDetailsEdit_Click(object sender, EventArgs e)
{
isEdit = true;
}
protected void gridModemDetails_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(GridModemDetails.DataKeys[GridModemDetails.SelectedIndex].Values["gridModemDetails_SelectedIndexChanged"].ToString());
}
<asp:GridView ID="GridModemDetails" runat="server" Width="435px"
DataKeyNames="ModemId" AllowPaging="True"
OnSelectedIndexChanged="gridModemDetails_SelectedIndexChanged"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Edit" Visible="True" >
<ItemTemplate>
<asp:LinkButton ID="btnModemDetailsEdit"
AccessibleHeaderText="Edit"
ButtonType="Button"
Text="Edit"
HeaderText="Edit"
runat="server"
OnClick="btnModemDetailsEdit_Click"/>
</ItemTemplate>
</asp:TemplateField>
The GridView's SelectedIndexChanged event is tied to the RowCommand event.
The simple way to get the SelectedIndexChanged event to fire is to use the AutoGenerateSelectButton property of the GridView, like this:
<asp:GridView AutoGenerateSelectButton="true"
This will add a button to each row with the text Select and when clicked, the SelectedIndexChange event will fire.
In the case of your edit button, you can use the CommandField in the grid view markup, like this:
<asp:GridView ...>
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
Now in your code-behind, you can handle the RowCommand event, like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
// Get the actual row
GridViewRow theGridViewRow = GridView1.Rows(e.RowIndex);
// Do something with grid view row here
}
}

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

Can I access objects (button or hyperlink) within a templatefield in a Gridview?

I want to access an object (hyperlink or button) within a templatefield in a gridview.
How do I do this?
Assuming that you're binding data to it, you'll want to look at doing that inside the RowDataBound event.
Here's an example of how to retrieve a control within a Template Field:
.aspx:
<asp:GridView ID="GridView1" Runat="server"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Template Field">
<ItemTemplate>
<asp:Button ID="btnTest" Runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnTest = (Button)e.Row.FindControl("btnTest");
btnTest.Text = "I'm in a Template Field";
}
}
You can use it on RowDataBound or click event of your template control like
TextBox txtTemp= (TextBox )e.Row[e.RowIndex].FindControl("yourControlName");
string someText=txtTemp.Text;

Categories

Resources