简体   繁体   English

更改用户控件上的属性

[英]change property on user control

I changed a public property in my web user control but the client doesn't see that change until I delete the user control and re-add it and then it sees the change. 我更改了Web用户控件中的公共属性,但是客户端直到我删除用户控件并重新添加它然后看到更改后,才能看到该更改。

I'm thinking to myself what if the user control is used in many places, will I have to do that to all of the pages? 我在想我自己,如果在许多地方都使用了用户控件,那我必须对所有页面都这样做吗? Certainly, I missing something? 当然,我想念什么吗?

Here's the code-behind of my webusercontrol: 这是我的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);
        }
    }

}

All I did was change the property name for DataSource, it was AnyList. 我所做的就是更改DataSource的属性名称,即AnyList。

If I understand, you could save the value of the property on the ViewState, for sample: 据我了解,您可以将属性的值保存在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;
   }
}

If you do something like this, you can have more than one user control instance on your page without problem, because the ViewState Key is indexed by the Id property of the user control. 如果执行这样的操作,则页面上可以有多个用户控件实例,而不会出现问题,因为ViewState Key由用户控件的Id属性索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM