我有一个自定义网格,我在后面的 C# 代码中绑定了数据。我已经为我的专栏之一提供了超链接字段。如果我单击超链接值,它应该导航到该超链接值的详细信息页面。代码如下,
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myLink = new HyperLink();
myLink.Text = e.Row.Cells[2].Text;
e.Row.Cells[2].Controls.Add(myLink);
myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + EstimateID + "&VersionNo=" + VersionNo;
}
}
如果我单击链接,则会导航页面,但我没有获得该页面中已预加载的详细信息。请给我关于如何合并这一点的建议。 谢谢
您可以使用它来重定向,请阅读this
<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl="Default2.aspx">
HyperLink
</asp:HyperLink>
要添加带有链接的属性,只需添加
HyperLink1.Attributes.Add ("");
您需要在 RowDataBound 事件中做一些小的更改
myLink.Attributes.Add("href","你的网址");
您需要从网格行数据中获取
EstimateID
和 VersionNo
的值。查看 GridViewRowEventArgs 的文档,您会看到有一个 .Row 属性。
所以你的代码需要类似于:
myLink.NavigateUrl = "Estimation.aspx?EstimateID=" + e.Row.Cells[4].Text + "&VersionNo=" + e.Row.Cells[5].Text;
或者,也许您需要获取与网格行关联的数据项,在这种情况下,请查看 e.Row.DataItem,即 GridViewRow.DataItem 属性。该 DataItem 需要转换为您绑定到网格的数据类型,以便从中获取数据,这可能类似于:
((MyCustomDataRow)e.Row.DataItem).EstimateID
尝试以下解决方案:
Page-1 即您的列表页面: ASPX 代码:
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
背后代码:
protected void Page_Load(object sender, EventArgs e)
{
List<Data> lstData = new List<Data>();
for (int index = 0; index < 10; index++)
{
Data objData = new Data();
objData.EstimateID = index;
objData.VersionNo = "VersionNo" + index;
lstData.Add(objData);
}
GridView1.DataSource = lstData;
GridView1.DataBind();
}
public class Data
{
public int EstimateID { get; set; }
public string VersionNo { get; set; }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = e.Row.FindControl("HyperLink1") as HyperLink;
HyperLink1.NavigateUrl = "Details.aspx?EstimateID=" + e.Row.Cells[1].Text + "&VersionNo=" + e.Row.Cells[2].Text;
}
}
第2页是您的详细信息页面: 背后代码:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["EstimateID"].ToString());
Response.Write(Request.QueryString["VersionNo"].ToString());
}