asp.net datalist更新到另一个页面

时间:2014-05-01 14:29:37

标签: c# asp.net sql

我在asp.net c#中使用datalist,从数据库显示数据并执行删除。但是在这个表格中我还有另一个按钮“编辑”,我想获取项目的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();

        }
}

我真的需要一些帮助

4 个答案:

答案 0 :(得分:0)

将其作为查询字符串传递,例如

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

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

答案 1 :(得分:0)

传递数据的最简单方法之一是通过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.
        }
    }
}

答案 2 :(得分:0)

查询字符串方法:

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

在重定向页面中..

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

答案 3 :(得分:0)

另一种方法是在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
      }
    }

}