从datagridview中将行值从一个页面检索到另一个页面 [英] Retrieve row values from one page to another from datagridview
本文介绍了从datagridview中将行值从一个页面检索到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过选择数据网格视图中的特定行并在下一页显示来从数据库表中检索id?
how to retrieve id from database table by selecting a particular row in data grid view and display in next page?
推荐答案
在第1页中编写此代码
DEFAULT.ASP
write this code in 1st page
DEFAULT.ASP
<div>
<a href="ShowDetails.aspx?autoid=5" title="Show records where AutoId is 5">Show records where AutoId is 5 </a>
<p><asp:HyperLink ID="hyper1" runat="server" Text="Show Record where AutoID is 8"> </asp:HyperLink> </p>
</div>
DEFAULT.ASPX.CS代码背后
DEFAULT.ASPX.CS CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
hyper1.NavigateUrl = "ShowDetails.aspx?autoid=8&com=show";
}
}
SHOWDETAILS.ASPX
SHOWDETAILS.ASPX
<div>
<asp:DetailsView ID="DetailsView1" runat="server" EnableViewState="false" />
</div>
SHOWDETAILS.ASPX.CS代码背后
SHOWDETAILS.ASPX.CS CODE BEHIND
rotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrWhiteSpace(Request.QueryString["autoid"]))
{
int autoId = 0;
int.TryParse(Request.QueryString["autoid"], out autoId);
if (!autoId.Equals(0))
{
GetData(autoId);
}
}
// to get the other querystring do the same
string command = Request.QueryString["com"];
}
}
private void GetData(int autoId)
{
DataTable table = new DataTable();
// get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
{
// write the sql statement to execute
string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail WHERE AutoId = @AutoId ORDER By AutoId";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
SqlParameter prm = new SqlParameter("@AutoId", SqlDbType.Int);
prm.Value = autoId;
cmd.Parameters.Add(prm);
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
}
}
// specify the data source for the GridView
DetailsView1.DataSource = table;
// bind the data now
DetailsView1.DataBind();
}
您可以使用请求查询字符串或会话变量来存储所选行的ID。
You can use request query string or session variable to store id of selected row.
if(!String.IsNullOrEmpty(Request.QueryString("TextBoxValue"))
{
TextBox1.Text = Request.QueryString("TextBoxValue");
}
使用该值存储在数据库中.....
using that value to store in database.....
这篇关于从datagridview中将行值从一个页面检索到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文