日期:2014-05-17  浏览次数:20453 次

datalist里德linkbutton传值
C# code
 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "look1")
        {
            Int32 id = Int32.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());//获得当前商品编号
            Response.Write("<script language='javascript'>window.open('goodslook.aspx?goodid=" + id + "');document.location=document.location;</script>");
            //在新窗口中弹出商品详细信息页面
        }

C# code
if (!IsPostBack)
        {

            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connstr"]);
            conn.Open();
            Int32 id = Convert.ToInt32(Request.QueryString["goodsid"]);
            string sql = "select * from dbo.Goods where GoodsID=" + id;
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);

           this.Label1.Text = ds.Tables[0].Rows[0][3].ToString();



结果id就变0了..报错了

------解决方案--------------------
注意:地址栏的参数是 goodid 而不是goodsid

所以

C# code
Int32 id = Convert.ToInt32(Request.QueryString["goodsid"]);