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.
Related
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;
}
Each row of the GridView is populated from a SQL DB.
Each row has a LinkButton that brings up a popup.
In the code behind I want to have access to the DataField="RCID"
I guess I'd like to attach the RCID field to the Upload link so when it's clicked I have access to the RCID within the function that handles the onclick.
How do I get this rows RCID?
<asp:GridView ID="GridView1" runat="server"
OnPageIndexChanging="GridView1_PageIndexChanging"
GridLines="Horizontal" AllowPaging="true" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="RCID" DataField="RCID" Visible="false"></asp:BoundField>
<asp:BoundField HeaderText="RC Type" DataField="RCType"></asp:BoundField>
<asp:BoundField HeaderText="Channel" DataField="Channel"></asp:BoundField>
<asp:BoundField HeaderText="Total" DataField="Total"></asp:BoundField>
<asp:BoundField HeaderText="Expired In 30 Days" DataField="ExpiredIn30Days"></asp:BoundField>
<asp:BoundField HeaderText="Expired In 60 Days" DataField="ExpiredIn60Days"></asp:BoundField>
<asp:BoundField HeaderText="Expired In 90 Days" DataField="ExpiredIn90Days"></asp:BoundField>
<asp:BoundField HeaderText="Last Updated" DataField="LastUpdated"></asp:BoundField>
<asp:TemplateField HeaderText="Management">
<ItemTemplate>
<asp:LinkButton runat="server" ID="Upload" Text="Upload" CommandName="Upload" ></asp:LinkButton> |
<asp:LinkButton runat="server" ID="Details" Text="Details" CommandName="Details"></asp:LinkButton> |
<asp:LinkButton runat="server" ID="Files" Text="Files" CommandName="Files"></asp:LinkButton> |
<asp:LinkButton runat="server" ID="Edit" Text="Edit" CommandName="Edit"></asp:LinkButton> |
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Possible solution would be as follows:
Add RowCreated and RowCommand events for GridView
In RowCreated event, I have set “Visible” property to “false” for first column for both header and data row.
In RowCommand event, CommandName is checked first and then index for select command is retrieved. I have retrieved the row for that index and then retrieved the text in the specified row cell.
check this example:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Visible = false;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label1.Text = (row.Cells[0].Text);
}
}
Second approach:
Assign command argument to your hidden field value. This will reduce another efforts. check this way:
<asp:TemplateField HeaderText="Action3" Visible="false">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:LinkButton ID="lnkretqty" runat="server" Text="Return Qty" CommandName="RETQTY" ToolTip="Click here to Add Return Qty Entry"
CommandArgument='<%# Container.DataItemIndex %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code Behind code
protected void gvsearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "SRCSELREC")
{
Int32 rowind = Convert.ToInt32(e.CommandArgument.ToString());
string val = ((Label)gvgpitemdtl.Rows[rowind].FindControl("d")).Text.ToString();
}
}
catch (Exception ex)
{
General.MessageBox(this.Page, "Error at Gridview Row Command : " + ex.Message.ToString());
return;
}
}
References:
Way of getting Hidden column value in GridView
How to Get Hidden Column values in GridView
To Find Control in GridView on RowCommand event in asp.net
How to hide GridView column and retrieve value from hidden column cell in ASP.NET
Add CommandArgument='<%# Eval("RCID") %>' in the linkbutton's markup:
<asp:LinkButton runat="server" ID="Upload" Text="Upload" CommandName="Upload" CommandArgument='<%# Eval("RCID") %>'/>
Now, in the code behind of the handler, just read the CommandArgumentproperty of the passed GridViewCommandEventArgs parameter:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Upload")
{
var valueOfRCID = e.CommandArgument;
}
}
mshsayem may be right but note you can also just grab the value from within the click event as follows
<asp:LinkButton runat="server" ID="Upload" Text="Upload" OnClick="Upload_Click" CommandArgument='<%# Eval("RCID") %>'/>
protected void Upload_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string RCID = btn.CommandArgument;
}
I have a gridview row that when clicked has to do postback A and a buttonfield in that row that when clicked has to do postback B. The problem is that when i click on the buttonfield, both event1 and event2 gets fired. Below is the code.
protected void gdv_RowCommand(object sender, GridViewCommandEventArgs e)
{
string arg = Convert.ToString(((System.Web.UI.WebControls.CommandEventArgs)(e)).CommandArgument);
if (e.CommandName == "Command1")
{
doEvent1(arg);
}
else if (e.CommandName == "Command2")
{
doEvent2(arg);
}
}
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton b1 = (LinkButton)e.Row.Cells[0].Controls[0];
matchesButton.CommandArgument = arg1;
LinkButton rowLink = (LinkButton)e.Row.Cells[1].Controls[1];
rowLink.CommandArgument = arg2;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(rowLink, "");
}
}
And this is the asp code for the gridview
<Columns>
<asp:ButtonField DataTextField="f1" HeaderText="H1" CommandName="Command1" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btn1" runat="server" Text="" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
try to use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Command2")
{
// Your Code here
}
}
to find control in grid view use this code
LinkButton lnkbtn= (LinkButton)e.Row.FindControl("btn1");
Try adding both the button with in same <asp:TemplateField> if you don't want separate headers
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="button" runat="server" CommandName="Command1" />
<asp:LinkButton ID="btn1" runat="server" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
if you want separate headers make two separate <asp:TemplateField> and then add buttons in them.
I have a GridView in which I have put an extra column Details, and in each row of the GridView, I have a LinkButton called Details. So when I click this I want an event to trigger.
The asp.net code is:
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Kindly help. Thank you.
Then use the LinkButton.Click-event:
<asp:LinkButton ID="Details" OnClick="LinkClicked" runat="server" Text="Details">
in codebehind:
protected void LinkClicked(Object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
GridViewRow row = (GridViewRow)link.NamingContainer;
// assuming there's a label with ID=Label1 in another TemplateField:
Label label1 = (Label)row.FindControl("Label1");
label1.Text="You clicked the link button";
}
If you need the GridViewRow of the GridView where the user clicked the link, for example to find controls in other templateFields, you can use the NamingContainerproperty.
Handle GridView.RowCommand Event
Occurs when a button is clicked in a GridView control.
<asp:LinkButton ID="Details" commandname="Details" runat="server" Text="Details"></asp:LinkButton>
void GridView1_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=="Details")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
//Your Code
}
}
You can have a command argument to determine which row was clicked (assuming you have more than one button (standard buttons/link buttons) in a row
<asp:GridView OnRowCommand="GridViewOnItemCommand" runat="server">
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="btnDetails" CommandName="Details" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Details" runat="server"/>
<asp:LinkButton ID="btnDelete" CommandName="Delete" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Delete" runat="server"/>
<ItemTemplate>
</asp:TemplateField>
</asp:GridView>
in the code behind file
protected void GridViewOnItemCommand(object sender, GridViewCommandEventArgs e)
{
//you can determine which button was clicked (detail or delete)
var command = e.CommandName;
//you can determine which row was clicked
var arg = e.CommandArgument;
if(command == "Details")
ShowDetails(arg);
if(command == "Delete")
Delete(arg);
}
hope this helps
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
}
}