[英]How to display data from Database after selecting a row from Gridview
我有一个带有 HyperLinkField 的 Databound Gridview 来查看重定向到下一页的行信息。 Gridview 中显示的列只是我数据库中的一小部分,我希望整个数据库数据(包括 GridView 上未显示的数据)显示到下一页。
我曾尝试使用DataNavigateUrlFields
,但这只会显示 GridView 中的记录。
这是我的代码的一些部分
<asp:GridView ID="gvStock" CssClass="GV" runat="server" enablepagingandcallbackz="false" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" OnSorting="OnSorting" OnPageIndexChanging="OnPageIndexChanging" PageSize="20" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black">
<Columns>
<asp:HyperLinkField Text="View" DataNavigateUrlFields="pCode,pID,bCode,SBrand,SDescription,sCost,sPrice,SType,sSupplierName,sSupplierDirect" DataNavigateUrlFormatString="ProductDetail.aspx?pCode={0}&pID={1}&bCode={2}&SBrand={3}&SDescription={4}&sCost={5}&sPrice={6}&SType={7}&sSupplierName={8}&sSupplierDirect={9}"/>
<asp:BoundField DataField="pCode" HeaderText="Product Code" />
<asp:BoundField DataField="pID" HeaderText="PID"/>
</Columns>
</asp:GridView>
背后的代码
protected void gvStock_SelectedIndexChanged(object sender, EventArgs e)
{
string id = gvStock.SelectedRow.Cells[0].Text;
Response.Redirect("ProductDetail.aspx?");
}
我会给你一个例子......所以,你可以很容易理解......应用你的逻辑......在你的代码......
.aspx 页面
<asp:GridView ID="grdTesting" runat="server" AutoGenerateColumns="false" Width="98%">
<Columns>
<asp:TemplateField HeaderText="Job Number">
<ItemTemplate>
<asp:LinkButton ID="lnkTest" runat="server" Text='<% # Bind("Test") %>' ToolTip='<%# Bind("Test") %>'
OnClick="lnkTest_Click"></asp:LinkButton>
<asp:HiddenField ID="hdnId" runat="server" Value='<% # Bind("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs 页面
protected void lnkTest_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((Control)sender).Parent.Parent;
HiddenField hdnId = (HiddenField)gvr.FindControl("hdnId");
if(Convert.ToString(hdnId.value)=="1")
{
Response.Redirect("frmTest1.aspx?Id=" + Convert.ToString(hdnId.Value), false);
}
else if(Convert.ToString(hdnId.value)=="2")
{
Response.Redirect("frmTest2.aspx?Id=" + Convert.ToString(hdnId.Value), false);
}
else
{
Response.Redirect("frmTest.aspx?Id=" + Convert.ToString(hdnId.Value), false);
}
}
笔记:-
在页面加载中,您必须绑定 GridView(根据您的偏好..)
假设,您有一个带有链接按钮的 Gridview(重定向到不同的页面......)
因此,您还必须绑定 Id........所以可以根据您的 ID 重定向到不同的页面......
单击链接按钮后,您可以通过发送查询字符串值(Id)重定向到其他页面(根据您的逻辑..)
我正在使用 asp.net 和 c# 4.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.