更改用户控件的属性

时间:2012-06-06 18:54:58

标签: c# asp.net

我在我的网络用户控件中更改了一个公共属性,但是在我删除用户控件并重新添加它然后它看到更改之前,客户端才看到该更改。

我在想,如果在许多地方使用了用户控件,我是否必须对所有页面执行此操作?当然,我错过了什么?

这是我的webusercontrol的代码隐藏:

public partial class ReportExporter : System.Web.UI.UserControl
{
    public IEnumerable<object> DataSource { get; set; }

    public String ExportFilename { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void BtnExportCsv_Click(object sender, EventArgs e)
    {
        //Client needs to subscribe to this event and set the
        //DataSource property with IEnumerable. 
        //Todo: Find other ways to show this as a requirement.
        OnExportEvent(e);
        if (String.IsNullOrEmpty(ExportFilename))
            ExportFilename = "NeedToOverrideThisName";
        Response.Clear();
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + ExportFilename + ".csv");

        byte[] csvData = Utility.ToCsv(",", DataSource.ToList());
        Response.OutputStream.Write(csvData, 0, csvData.Length);
        HttpContext.Current.Response.End();
    }

    protected void BtnExportPdf_Click(object sender, EventArgs e)
    {

    }

    public event EventHandler ExportEvent;

    protected void OnExportEvent(EventArgs e)
    {
        if (ExportEvent != null)
        {
            ExportEvent(this, e);
        }
    }

}

我所做的只是更改DataSource的属性名称,它是AnyList。

1 个答案:

答案 0 :(得分:1)

如果我理解,你可以在ViewState上保存属性的值,样本:

public String ExportFileName 
{ 
   get 
   {
       if (ViewState["ExportFileName_" + this.Id] == null) 
          return "default_name";

       return ViewState["ExportFileName_" + this.Id].ToString();
   }
   set
   {
       ViewState["ExportFileName_" + this.Id] = value;
   }
}

如果你这样做,你可以在你的页面上有多个用户控件实例而没有问题,因为ViewState Key是由用户控件的Id属性索引的。