【发布时间】:2019-05-28 23:08:25
【问题描述】:
我有一个网格视图,其中显示了多条记录。在页面加载事件中,我正在从数据库中检索数据并填充 gridview。我还有另一列用于编辑和删除,它们是 Imagebuttons。现在我希望当用户单击编辑按钮时,然后基于 Id(用户单击时的该行),他/她重定向到具有所选 Id 的另一个网络表单,然后该人记录从数据库加载并填充相应的文本框。我没有更新 Gridview 中的记录。 这是清晰理解的图像
这是我的 .aspx 代码
<asp:GridView ID="dgvEmployeesInformation" runat="server" CssClass=" table table-bordered table-hover table-responsive" AutoGenerateColumns="false" OnRowCommand="dgvEmployeesInformation_RowCommand">
<%--1st Column--%>
<Columns>
<asp:BoundField HeaderText="ID" DataField="Id" ShowHeader="false" Visible="false" ControlStyle-BackColor="#0066ff"/>
<asp:BoundField HeaderText="Name" DataField="Name"/>
<asp:BoundField HeaderText="Employee No" DataField="EmployeeNo"/>
<asp:BoundField HeaderText="Father Name" DataField="FatherName"/>
<asp:BoundField HeaderText="CNIC" DataField="CNIC"/>
<asp:BoundField HeaderText="Contact No" DataField="ContactNo"/>
<asp:BoundField HeaderText="City" DataField="City"/>
<asp:BoundField HeaderText="Post" DataField="RecommendedPost"/>
<asp:BoundField HeaderText="Status" DataField="Status"/>
<asp:BoundField HeaderText="Degree Status" DataField="DegreeStatus"/>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" CommandName="Edit" Text='<%# Eval("ID") %>' ToolTip="Edit" Width="20px" Height="20px" runat="server"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" CommandName="Delete" Text='<%# Eval("ID") %>' ToolTip="Delete" Width="20px" Height="20px" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这里是 .aspx.cs 代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dgvEmployeesInformation.DataSource = GetData();
dgvEmployeesInformation.DataBind();
}
}
private DataSet GetData()
{
DataSet DS = new DataSet();
using (SqlConnection con = new SqlConnection(Base.GetConnection))
{
SqlDataAdapter ad = new SqlDataAdapter("spSelectEmployeeSpecificRecord", con);
ad.SelectCommand.CommandType = CommandType.StoredProcedure;
ad.Fill(DS);
}
return DS;
}
protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
}
}
我不知道如何获取所选行的 ID 并将其发送到另一个页面。请帮忙
【问题讨论】:
-
您希望/已经将哪一列设置为 ID?我认为您可以将
SelectedRow和DataKeyNames用于特定列。
标签: c# asp.net .net gridview datagridview