在 GridView 中选择一行,并将记录 ID 传递到重定向的页面
本文关键字:ID 记录 重定向 选择 GridView 一行 | 更新日期: 2023-09-27 18:31:08
我已经为这个问题苦苦挣扎了一整天,但我似乎没有找到答案。
我有一个名为PickRecord的页面.aspx其中我动态生成了"开始面试","编辑"和"查看记录"列,这些列将相应地将我重定向到StartInterview.aspx或EditView.aspx Interview。
PickRecord.aspx - GridView 填充了受访者姓名,并生成了一个按钮,单击该按钮时应将我重定向到 StartInterview.aspx并在所选记录中为我提供其他字段/信息。
StartInterview.aspx - 包含有关所选记录的其他信息,这些信息需要使用数据库中的数据进行填充。我尝试过查询字符串,使用selectedIndexChanged,但没有运气。
如果有人能指导我完成这个,我们将不胜感激。提前感谢!
PickRecord.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="470px" Width="660px" HorizontalAlign="Center" SelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="First_Name" HeaderText="First Name" ItemStyle-HorizontalAlign = "Center">
</asp:BoundField>
<asp:BoundField DataField="Last_Name" HeaderText=" Last Name" ItemStyle-HorizontalAlign = "Center" >
</asp:BoundField>
<asp:BoundField DataField="Salon_Number" HeaderText="Salon" ItemStyle-HorizontalAlign = "Center" >
</asp:BoundField>
</Columns>
</asp:GridView>
选择记录.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace TSS.Trainer
{
public partial class Trainer_PickRecord : System.Web.UI.Page
{
static string id;
protected void Page_Load(object sender, EventArgs e)
{
dbConnection dbConn = new dbConnection();
string s = Request.QueryString["mode"].ToString();
if (!IsPostBack)
{
FillMonth(ddl_T_month);
FillYear(ddl_T_year);
}
if (s == "a")
{
TemplateField tf1 = new TemplateField();
tf1.ItemTemplate = new btntemplate1();
tf1.HeaderText = "Start Interview";
tf1.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
GridView1.Columns.Add(tf1);
DataTable dtInterviewee = new DataTable();
dtInterviewee = Salon_WebService.populateInterviewee();
GridView1.DataSource = dtInterviewee;
GridView1.DataBind();
}
else if (s == "b")
{
TemplateField tf2 = new TemplateField();
TemplateField tf3 = new TemplateField();
tf2.ItemTemplate = new btntemplate2();
tf3.ItemTemplate = new btntemplate3();
tf2.HeaderText = "Edit";
tf3.HeaderText = "View";
tf2.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
tf3.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
GridView1.Columns.Add(tf2);
GridView1.Columns.Add(tf3);
DataTable dtInterviewee = new DataTable();
dtInterviewee = Salon_WebService.populateInterviewee();
GridView1.DataSource = dtInterviewee;
GridView1.DataBind();
}
void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
id = row.Cells[0].Text;
}
}
class btntemplate1 : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
HyperLink h1 = new HyperLink();
h1.NavigateUrl = "Trainer_StartInterview.aspx?x= " +id;
h1.ImageUrl = "../Images/T_StartIV_small.png";
container.Controls.Add(h1);
}
}
class btntemplate2 : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
HyperLink h2 = new HyperLink();
h2.NavigateUrl = "Trainer_Home.aspx";
h2.ImageUrl = "../Images/Edit_small.png";
container.Controls.Add(h2);
}
}
class btntemplate3 : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
HyperLink h3 = new HyperLink();
h3.NavigateUrl = "Trainer_ViewRecord.aspx";
h3.ImageUrl = "../Images/View_small.png";
container.Controls.Add(h3);
}
}
}
}
开始面试.aspx
<div id="T_StartInterview_formHeader" class="fh2">
<center>
<table>
<tr>
<td style="text-align: right; color: white">
<asp:Label ID="lblCreate_Date" runat="server" Text="Create Date : "></asp:Label>
</td>
<td>
<asp:Label ID="LabelCD" runat="server" Text="December 05, 2011 "></asp:Label>
</td>
<td style="text-align: right; color: white">
<asp:Label ID="lblUpdate_Date" runat="server" Text="Update Date : "></asp:Label>
</td>
<td>
<asp:Label ID="lblUD" runat="server" Text="December 09, 2011 "></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right; color: white">
<asp:Label ID="lblCreatedBy" runat="server" Text="Created By : "></asp:Label>
</td>
<td>
<asp:Label ID="lblCB" runat="server" Text='<%# Eval ("First_Name") %>' class="lblSizeR"></asp:Label>
</td>
<td style="text-align: right; color: white">
<asp:Label ID="lblUpBy" runat="server" Text="Updated By : "></asp:Label>
</td>
<td>
<asp:Label ID="lblUB" runat="server" Text="Sharon Miller " class="lblSizeR"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right; color: white">
<asp:Label ID="lblFName" runat="server" Text="First Name : "></asp:Label>
</td>
<td>
<asp:Label ID="lblFN" runat="server" Text="Michelle"></asp:Label>
</td>
<td style="text-align: right; color: white">
<asp:Label ID="lblLName" runat="server" Text="Last Name : "></asp:Label>
</td>
<td>
<asp:Label ID="lblLN" runat="server" Text="Rusk"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right; color: white">
<asp:Label ID="lblPhone" runat="server" Text="Phone : "></asp:Label>
</td>
<td>
<asp:Label ID="lblPh" runat="server" Text="269 123 1234"></asp:Label>
</td>
<td style="text-align: right; color: white">
<asp:Label ID="lblEmail" runat="server" Text="E-Mail : "></asp:Label>
</td>
<td>
<asp:Label ID="lblMail" runat="server" Text="xyz@visiblechanges.com" class="lblSizeR"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right; color: white">
<asp:Label ID="lblSalon" runat="server" Text="Salon: "></asp:Label>
</td>
<td>
<asp:Label ID="lblSal" runat="server" Text="Memorial City Mall" class="lblSizeR"></asp:Label>
</td>
</tr>
</table>
Trainer_StartInterview.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TSS.Trainer
{
public partial class Trainer_StartInterview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = Request.QueryString["id"].ToString();
}
}
}
您还没有提到确切的问题,但是如果您甚至没有点击StartInterview.aspx页面,那么我建议您删除代码中实例化的复杂内容,并将其替换为具有按钮甚至按钮列的基于模板的简单列。您可以查看此链接以进行实施:单击网格视图按钮字段时重定向到特定页面