简体   繁体   中英

asp.net datalist update to another page

I am using datalist in asp.net c#, to display data from database and perform delete. 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. 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).

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

One of the easiest ways to pass data is through the 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.

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:

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

Page_Load(...)
{

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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