![](/img/trans.png)
[英]How to transfer selected data(datalist) from one page to another page without using session in asp.net?
[英]asp.net datalist update to another page
我正在asp.net c#中使用數據列表,以顯示數據庫中的數據並執行刪除。 但在此表上,我還有另一個“編輯”按鈕,我想獲取該項目的ID並轉到另一個表格,該表格中預先填充了有關該項目的數據。 問題是我是asp.net的新手,我不知道如何從一頁到另一頁(如id)獲取數據。
public partial class DataList : System.Web.UI.Page
{
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringDatabase"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if(!IsPostBack)
{
Bind();
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
public void Bind()
{
SqlConnection con = new SqlConnection(connection);
SqlDataAdapter da = new SqlDataAdapter("select * from artikulli", con);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds);
con.Close();
datalist2.DataSource = ds.Tables[0];
datalist2.DataBind();
}
protected void Datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
TextBox txtTema = e.Item.FindControl("txtTema") as TextBox;
SqlConnection conn = new SqlConnection(connection);
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = "Insert into artikulli(tema) values (@tema)";
command.Parameters.Add("@tema", SqlDbType.VarChar, 250).Value = txtTema.Text;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
Bind();
}
}
protected void Datalist1_EditCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
Response.Redirect("EditArtikull3.aspx");
}
}
protected void datalist1_CancelCommand(object source, DataListCommandEventArgs e)
{
datalist2.EditItemIndex = -1;
Bind();
}
protected void datalist1_UpdateCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName.Equals("Update"))
{
}
}
protected void datalist2_DeleteCommand(object source, DataListCommandEventArgs e)
{
Label lblId = e.Item.FindControl("lblId") as Label;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Delete from artikulli where id=@id";
cmd.Parameters.Add("@id", SqlDbType.Int, 11).Value = lblId.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Bind();
}
}
我真的需要幫助
將其作為查詢字符串傳遞,例如
Response.Redirect("EditArtikull3.aspx?Id=yourId");
您可以參考http://msdn.microsoft.com/en-us/library/vstudio/6c3yckfw(v=vs.100).aspx
傳遞數據最簡單的方法之一是通過QueryString。 考慮例如。
Label lblId = e.Item.FindControl("lblId") as Label;
string id=lblId.Text;
Response.Redirect("EditArtikull3.aspx?id="+id);
然后在EditArtikull3
頁面上的Page_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
頁上,從QueryString中讀取它
Page_Load(...)
{
if(!IsPostback)
{
string id = Request.QueryString["id"] as string;
if(id!=null)
{
//query the database and populate the data
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.