简体   繁体   English

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

[英]asp.net datalist update to another page

I am using datalist in asp.net c#, to display data from database and perform delete. 我正在asp.net c#中使用数据列表,以显示数据库中的数据并执行删除。 But also at this table I have an other button "edit" which I want to get the id of item and go to another page with a form that is prefilled with data about that item. 但在此表上,我还有另一个“编辑”按钮,我想获取该项目的ID并转到另一个表格,该表格中预先填充了有关该项目的数据。 The problem is that I am new at asp.net and I dont know how to get data form one page to another (like 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();

        }
}

I really need some help 我真的需要帮助

Pass it as querystring eg 将其作为查询字符串传递,例如

Response.Redirect("EditArtikull3.aspx?Id=yourId");

You can refer to http://msdn.microsoft.com/en-us/library/vstudio/6c3yckfw(v=vs.100).aspx 您可以参考http://msdn.microsoft.com/en-us/library/vstudio/6c3yckfw(v=vs.100).aspx

One of the easiest ways to pass data is through the QueryString. 传递数据最简单的方法之一是通过QueryString。 Consider for example.. 考虑例如。

Label lblId = e.Item.FindControl("lblId") as Label;
string id=lblId.Text;
Response.Redirect("EditArtikull3.aspx?id="+id);

Then on the EditArtikull3 page, in the Page_Load method, check for that QueryString parameter and load data accordingly. 然后在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.
        }
    }
}

Query string method: 查询字符串的方法:

Response.Redirect("EditArtikull3.aspx?id=yourId");

In redirected page.. 在重定向页面中。

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

One alternative would be to pass the id in the URL as in: 一种替代方法是在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
    }
}

Then on the EditArtikull3.aspx page read it from the QueryString 然后在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM