[英]Get data from RowCommand
I have a grid which shows product versions and have a few link buttons like edit, delete, preview etc. On click of the edit button I want to get the Product ID and Version ID and redirect to some xyz .aspx page where the product details can be edited. 我有一个网格,显示产品版本,并有一些链接按钮,如编辑,删除,预览等。点击编辑按钮,我想获得产品ID和版本ID,并重定向到一些xyz .aspx页面,其中的产品详细信息可以编辑。
Here is how my Grid looks: 以下是我的Grid的外观:
<asp:GridView ID="grdBindVersion" runat="server" AutoGenerateColumns="false"
onrowcommand="grdBindVersion_RowCommand" >
<RowStyle BorderStyle="Solid" BorderWidth="1px" />
<Columns>
<asp:BoundField datafield="HistoryId" HeaderText="Version ID.">
<HeaderStyle CssClass="grid"></HeaderStyle>
</asp:BoundField>
<asp:BoundField datafield="Title" HeaderText="Title">
<HeaderStyle CssClass="grid"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText = "Edit">
<ItemTemplate>
<asp:LinkButton ID="Edit" runat="server" CommandArgument='<%# Container.DataItem %>' CommandName ="Add">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Delete">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Add Sub Page">
<ItemTemplate>
<asp:LinkButton ID="AddSubPage" runat="server">Add Sub Page</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Add Rank">
<ItemTemplate>
<asp:LinkButton ID="AddRank" runat="server">Add Rank</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Approve/DisApprove">
<ItemTemplate>
<asp:LinkButton ID="ApproveStatus" runat="server">Approve/DisApprove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Complete">
<ItemTemplate>
<asp:LinkButton ID="Complete" runat="server">Complete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Preview">
<ItemTemplate>
<asp:LinkButton ID="Preview" runat="server">Preview</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Comment">
<ItemTemplate>
<asp:LinkButton ID="Comment" runat="server">Comment</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am tracking the clicking on the link button in the RowCommand
event of the GridView. 我正在跟踪点击GridView的
RowCommand
事件中的链接按钮。 Here i want to get the VersionID and ProductID and then redirect to another page. 在这里,我想获取VersionID和ProductID,然后重定向到另一个页面。 Please help me.
请帮我。 I am really new to coding.
我真的很喜欢编码。 I did a bit of Google but none of the solutions are helping me.
我做了一点谷歌,但没有一个解决方案帮助我。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
// row contains current Clicked Gridview Row
String VersionId = row.Cells[CellIndex].Text;
.............
.............
//e.CommandArgument -- this return Data Key Value
}
}
You can assign more than one assignment to a Gridview's OnRowCommand ... 您可以为Gridview的OnRowCommand分配多个作业...
<asp:TemplateField HeaderText="Feedback Form">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkFB" Enabled="true" Text="CREATE"
CommandArgument='<%# Eval("ApprenticeshipID") + ";" + Eval("OrganisationID") + ";" + Eval("StudentID") %>' CommandName="btnFB"
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
And in codebehind, extract the arguments one by one and assign them to session variables for later use ... 在代码隐藏中,逐个提取参数并将它们分配给会话变量供以后使用......
string[] arg = new string[2]; //say up to 3 arguments (ie: 0,1,2)
arg = e.CommandArgument.ToString().Split(';');
Session["ApprenticeshipID"] = arg[0]; //I only need first argument
GenerateFeedbackForm(Session["ApprenticeshipID"].ToString());
将VersionID和ProductID指定为GridView的DataKeyNames,然后通过Query Strings传递这两个值。
You can get the data on gridview_RowCommand
event by placing below code: 您可以通过下面的代码获取
gridview_RowCommand
事件的数据:
Int32 HistoryId = Convert.ToInt32(e.Row.Cells[0].Text.ToString());
You can set the [index]
in Cells[index]
according to your GridView items, then redirect to another page with that fetched HistoryId or ProductId. 您可以根据GridView项目在
Cells[index]
设置[index]
,然后使用获取的HistoryId或ProductId重定向到另一个页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.