我有一个gridview,其中显示了多条记录。在页面加载事件中,我正在从数据库中检索数据并填充gridview。我还有另一列用于编辑和删除的图像按钮。现在我想要当用户单击“编辑”按钮时,然后基于ID(在用户单击时该行的ID)的基础上,他/她重定向到具有所选ID的另一个Web窗体,然后该人记录数据库中的负载并填充相应的文本框。我不在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并将其发送到另一页。请帮助
答案 0 :(得分:0)
e.CommandArgument
包含您单击的索引。
dgvEmployeesInformation.SelectedValue
包含您在DataKeyNames
上设置的ID。
在DataKeyNames
上将您拥有的数据库字段设置为id
-在您的情况下,DataKeyNames="EmployeeNo"
可以设置为
protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
int SelectedIndex;
if (int.TryParse(e.CommandArgument.ToString(), out SelectedIndex))
{
// make it selected after the click
dgvEmployeesInformation.SelectedIndex = SelectedIndex;
// now the SelectedValue contains the id
Edit(dgvEmployeesInformation.SelectedValue);
}
}
}
答案 1 :(得分:-1)
尝试一下
void yourGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Edit")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = ContactsGridView.Rows[index];
var yourID = row.Cells[0].Text;
//....
}
}