给实体类的数据赋值

锤子馆长 2015-12-29 08:16:17

protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] == null)
{
//this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", "alert('请先登录!')",true);
//Response.Redirect("~/index.aspx");
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>if(confirm('您尚未登录,请登录后进行此操作!')){self.location='index.aspx';}</script>");

}
else
{
if (!IsPostBack)
{
Profile.shoplist.GetCartItem(Session["UserName"].ToString());
string DressId = Request.QueryString["DressId"];
int Num = Convert.ToInt32(Request.QueryString["num"]);
string size = Request.QueryString["size"];
if (!string.IsNullOrEmpty(DressId))
{

if (Session["flag"].ToString().Equals(""))
{
Profile.shoplist.Add(DressId, size, Num);
Session["flag"] = DressId;

}

}
BingCart(Profile.shoplist);
Profile.shoplist.SetCartItems(Session["UserName"].ToString());
Profile.Save();
}
}

}


public class DressItemInfo
{
string dressId;//服装编号
public string DressId
{
get { return dressId; }
set { dressId = value; }
}

string dressMerchant;//商家
public string DressMerchant
{
get { return dressMerchant; }
set { dressMerchant = value; }
}

decimal unitcost;//成本价
public decimal Unitcost
{
get { return unitcost; }
set { unitcost = value; }
}

decimal marketPrice;//市场价
public decimal MarketPrice
{
get { return marketPrice; }
set { marketPrice = value; }
}

decimal orignial;//吊牌价
public decimal Orignial
{
get { return orignial; }
set { orignial = value; }
}

string dressTitle;//商品标题
public string DressTitle
{
get { return dressTitle; }
set { dressTitle = value; }
}

string dressdetails;//商品介绍
public string Dressdetails
{
get { return dressdetails; }
set { dressdetails = value; }
}

string dressImage;//图片
public string DressImage
{
get { return dressImage; }
set { dressImage = value; }
}

int qty;//库存
public int Qty
{
get { return qty; }
set { qty = value; }
}

string size;//尺寸大小

public string Size
{
get { return size; }
set { size = value; }
}
int quantity;//购买数量

public int Quantity
{
get { return quantity; }
set { quantity = value; }
}
///<summary>
///默认构造
///</summary>
public DressItemInfo() { }
public DressItemInfo(int quantity) { this.quantity = quantity; }
///<summary>
///带参数构造
///</summary>
public DressItemInfo(string DressId, string DressMerchant, decimal Unitcost, decimal MarketPrice, decimal Orignial, string DressTitle, string Dressdetails, string DressImage, int Qty,string Size,int Quantiy)
{
this.dressId = DressId;
this.dressMerchant = DressMerchant;
this.unitcost = Unitcost;
this.marketPrice = MarketPrice;
this.orignial = Orignial;
this.dressTitle = DressTitle;
this.dressdetails = Dressdetails;
this.dressImage = DressImage;
this.qty = Qty;
this.size = Size;
this.quantity = Quantiy;
}
}


我希望加载的时候把Num赋值给DressItemInfo的Quantity 要怎么实现?因为我下面的方法会调用到Quantity
另外顺便问下 在加载的时候我注释的那2行 我发现当UserName为null的时候不会出现提示 而是直接跳转到首页 //Response.Redirect("~/index.aspx");这句没有的话就会跳出提示
...全文
1442 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2015-12-31
  • 打赏
  • 举报
回复
DressItemInfo 这个对象的命名空间是否正确? 我也怀疑是不是存在相同的名为DressItemInfo的实体。
w6579106 2015-12-31
  • 打赏
  • 举报
回复
引用 5 楼 qq389216533 的回复:
[quote=引用 4 楼 w6579106 的回复:]

            DressItemInfo Info = new DressItemInfo(Num );
DressItemInfo类中 不是写着带参数的构造函数么:

   public DressItemInfo(int quantity) { this.quantity = quantity; } 
要么换一种:

 DressItemInfo Info = new DressItemInfo();
Info.Quantity = Num ;
我试过了 这种是不行的[/quote] 完整的测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 4;
            DressItemInfo item = new DressItemInfo(num);
            Console.WriteLine("Quantity value:" + item.Quantity);
            Console.Read();
        }
    }


    public class DressItemInfo
    {
        string dressId;//服装编号
        public string DressId
        {
            get { return dressId; }
            set { dressId = value; }
        }

        string dressMerchant;//商家
        public string DressMerchant
        {
            get { return dressMerchant; }
            set { dressMerchant = value; }
        }

        decimal unitcost;//成本价
        public decimal Unitcost
        {
            get { return unitcost; }
            set { unitcost = value; }
        }

        decimal marketPrice;//市场价
        public decimal MarketPrice
        {
            get { return marketPrice; }
            set { marketPrice = value; }
        }

        decimal orignial;//吊牌价
        public decimal Orignial
        {
            get { return orignial; }
            set { orignial = value; }
        }

        string dressTitle;//商品标题
        public string DressTitle
        {
            get { return dressTitle; }
            set { dressTitle = value; }
        }

        string dressdetails;//商品介绍
        public string Dressdetails
        {
            get { return dressdetails; }
            set { dressdetails = value; }
        }

        string dressImage;//图片
        public string DressImage
        {
            get { return dressImage; }
            set { dressImage = value; }
        }

        int qty;//库存
        public int Qty
        {
            get { return qty; }
            set { qty = value; }
        }

        string size;//尺寸大小

        public string Size
        {
            get { return size; }
            set { size = value; }
        }
        int quantity;//购买数量

        public int Quantity
        {
            get { return quantity; }
            set { quantity = value; }
        }
        ///<summary>
        ///默认构造
        ///</summary>
        public DressItemInfo() { }
        public DressItemInfo(int quantity) { this.quantity = quantity; }
        ///<summary>
        ///带参数构造
        ///</summary>
        public DressItemInfo(string DressId, string DressMerchant, decimal Unitcost, decimal MarketPrice, decimal Orignial, string DressTitle, string Dressdetails, string DressImage, int Qty, string Size, int Quantiy)
        {
            this.dressId = DressId;
            this.dressMerchant = DressMerchant;
            this.unitcost = Unitcost;
            this.marketPrice = MarketPrice;
            this.orignial = Orignial;
            this.dressTitle = DressTitle;
            this.dressdetails = Dressdetails;
            this.dressImage = DressImage;
            this.qty = Qty;
            this.size = Size;
            this.quantity = Quantiy;
        }
    }

}


提示不包含一个参数的方法,可能的问题: 未正确引用 类DressItemInfo ,例如程序中还有另一个 类DressItemInfo(未含带参的构造函数), 或者类DressItemInfo 在另一个项目中,但程序引用的DLL并未及时更新,导致 DressItemInfo 实例化时,并未得到具有带参构造函数的方法。 一句话:再仔细检查
锤子馆长 2015-12-30
  • 打赏
  • 举报
回复
引用 7 楼 以专业开发人员为伍的回复:
[quote=引用 5 楼 qq389216533 的回复:] 我试过了 这种是不行的
靠瞎试?你是想随便 new 一个新的对象然后为其 Quanlity 属性赋值,还是为你的代码中已经得到的数据对象赋值? 人家的赋值语句不可能出错。关键是你自己不去想象该给哪个对象的属性赋值。[/quote] 那个赋值语句要是行我还用的着来论坛问?
  • 打赏
  • 举报
回复
引用 楼主 qq389216533 的回复:
我希望加载的时候把Num赋值给DressItemInfo的Quantity 要怎么实现?因为我下面的方法会调用到Quantity 另外顺便问下 在加载的时候我注释的那2行 我发现当UserName为null的时候不会出现提示 而是直接跳转到首页 //Response.Redirect("~/index.aspx");这句没有的话就会跳出提示
在你的 Page_Load 中获取到数据之后,直接为每一个 DressItemInfo 类型对象的 Quanlity 属性赋值即可。 Response.Redirect 语句会中断当前页面的处理,想客户端发送 302 状态信息,然后浏览器直接进行客户端重定向。了解这个机制就知道,你向浏览器输出的内容自然就没用。
  • 打赏
  • 举报
回复
引用 5 楼 qq389216533 的回复:
我试过了 这种是不行的
靠瞎试?你是想随便 new 一个新的对象然后为其 Quanlity 属性赋值,还是为你的代码中已经得到的数据对象赋值? 人家的赋值语句不可能出错。关键是你自己不去想象该给哪个对象的属性赋值。
锤子馆长 2015-12-29
  • 打赏
  • 举报
回复
引用 4 楼 w6579106 的回复:

            DressItemInfo Info = new DressItemInfo(Num );
DressItemInfo类中 不是写着带参数的构造函数么:

   public DressItemInfo(int quantity) { this.quantity = quantity; } 
要么换一种:

 DressItemInfo Info = new DressItemInfo();
Info.Quantity = Num ;
我试过了 这种是不行的
w6579106 2015-12-29
  • 打赏
  • 举报
回复

            DressItemInfo Info = new DressItemInfo(Num );
DressItemInfo类中 不是写着带参数的构造函数么:

   public DressItemInfo(int quantity) { this.quantity = quantity; } 
要么换一种:

 DressItemInfo Info = new DressItemInfo();
Info.Quantity = Num ;
锤子馆长 2015-12-29
  • 打赏
  • 举报
回复
引用 1 楼 w6579106 的回复:
DressItemInfo dItemInfo = new DressItemInfo(Num);
提示不包含一个参数的方法
锤子馆长 2015-12-29
  • 打赏
  • 举报
回复
引用 1 楼 w6579106 的回复:
DressItemInfo dItemInfo = new DressItemInfo(Num);
这样不行吧。。。
w6579106 2015-12-29
  • 打赏
  • 举报
回复
DressItemInfo dItemInfo = new DressItemInfo(Num);

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧