C# asp.net数据列表更新到另一页

C# asp.net数据列表更新到另一页,c#,asp.net,sql,C#,Asp.net,Sql,我正在使用asp.net c#中的datalist显示数据库中的数据并执行删除。但在这个表中,我还有一个“编辑”按钮,我想获取项目的id,然后转到另一个页面,该页面的表单预先填充了该项目的数据。问题是我是asp.net的新手,我不知道如何从一个页面到另一个页面获取数据(比如id) 我真的需要一些帮助将其作为查询字符串传递 Response.Redirect("EditArtikull3.aspx?Id=yourId"); 你可以参考 传递数据的最简单方法之一是通过查询字符串。例如,考虑……/P

我正在使用asp.net c#中的datalist显示数据库中的数据并执行删除。但在这个表中,我还有一个“编辑”按钮,我想获取项目的id,然后转到另一个页面,该页面的表单预先填充了该项目的数据。问题是我是asp.net的新手,我不知道如何从一个页面到另一个页面获取数据(比如id)


我真的需要一些帮助

将其作为查询字符串传递

Response.Redirect("EditArtikull3.aspx?Id=yourId");
你可以参考

传递数据的最简单方法之一是通过查询字符串。例如,考虑……/P>
Label lblId = e.Item.FindControl("lblId") as Label;
string id=lblId.Text;
Response.Redirect("EditArtikull3.aspx?id="+id);
然后在
EditArtikull3
页面上,在
page\u Load
方法中,检查
QueryString
参数并相应地加载数据

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        if(!String.IsNullOrEmpty(Request.QueryString["id"]))
        {
            string id=Request.QueryString["id"];
            //load data based on the id
        }
        else
        {
             //tell the user they can't navigate directly to this page.
        }
    }
}

查询字符串方法:

Response.Redirect("EditArtikull3.aspx?id=yourId");
在重定向页面中

protected void Page_Load(object sender, EventArgs e)
{
    string id=Request.QueryString["id"];
}

一种替代方法是在URL中传递id,如下所示:

protected void Datalist1_EditCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
            Response.Redirect(string.Format("EditArtikull3.aspx?id={0}",((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()); // where [0] is the index of the column containing the item ID
    }
}
然后在
EditArtikull3.aspx
页面上,从查询字符串中读取它

Page_Load(...)
{

    if(!IsPostback)
    {
      string id = Request.QueryString["id"] as string;
      if(id!=null)
      {
         //query the database and populate the data
      }
    }

}

会话是一个糟糕的选择。如果用户打开两个窗口并单击“编辑”两篇不同的文章,该怎么办?如果我是你,我会在回答中删除对会话的任何提及,以避免混淆。
Page_Load(...)
{

    if(!IsPostback)
    {
      string id = Request.QueryString["id"] as string;
      if(id!=null)
      {
         //query the database and populate the data
      }
    }

}