0

我更改了 Web 用户控件中的公共属性,但客户端看不到该更改,直到我删除用户控件并重新添加它,然后它才看到更改。

我在想,如果在很多地方都使用了用户控件,我是否必须对所有页面都这样做?当然,我错过了什么?

这是我的 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。

4

1 回答 1

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 属性索引的。

于 2012-06-06T19:17:21.690 回答