ASP.NET自定义控件 - 自定义属性不保留postaback上的指定值

时间:2012-01-13 09:50:02

标签: asp.net custom-controls c#-2.0

我有一个自定义的asp-net控件继承自另一个并且它按预期工作,虽然只有在我直接在标记中编码它们时才能正确设置属性,所以例如如果我需要在运行时设置一个属性是一些动态值,这个值永远不会设置或以某种方式丢失。

这是标记代码:

<!--related form-->
<fw:advancedformdisplay id="formDisp" runat="server" captchaenabled="true" EmailEnabled="true" EnableViewState="true" captchaprivatekey="xxxxxxxxxxxxxxxxxxxx" captchapublickey="xxxxxxxxxxxxx" captchatheme="white" SourceType="MenuItem" SourceMainId="Auto">
</fw:advancedformdisplay> 

这是控件的代码:

    [DefaultProperty("CaptchaEnabled"),ToolboxData("<{0}:AdvancedFormDisplay runat=server></{0}:AdvancedFormDisplay>"), Description("This is an enhanced FormDisplay control that inlcudes Googles Captcha control is enabled")]
public class AdvancedFormDisplay :SiteBuilder.WebControls.FormDisplay
{
    bool _CaptchaEnabled = false, sendEmail = false;
    string captchaErrorMessage = "The verification code entered is not valid. Please try again!";
    RecaptchaControl captchaControl = null;
    string captchaPrivateKey = "", captchaPublicKey = "", captchaTheme = "clean";
    string originalFormHtml = string.Empty;
    string afterText = string.Empty, beforeText = string.Empty;
    Literal litHtmlForm = null;
    string captchaErrorClass = "errorCaptcha";

    public string EmailBeforeText
    {
        get { return beforeText; }
        set { beforeText = value; }
    }

    public string EmailAfterText
    {
        get { return afterText; }
        set { afterText = value; }
    }

    public string CaptchaErrorClass
    {
        get { return captchaErrorClass; }
        set { captchaErrorClass = value; }
    }

    public bool CaptchaEnabled
    {
        get { return _CaptchaEnabled; }
        set { _CaptchaEnabled = value; }
    }

    public bool EmailEnabled
    {
        get { return sendEmail; }
        set { sendEmail = value; }
    }

    public string CaptchaErrorMessage
    {
        get { return captchaErrorMessage; }
        set { captchaErrorMessage = value; }
    }

    /// <summary>
    /// red,white,blackglass,clean
    /// </summary>
    public string CaptchaTheme
    {
        get { return captchaTheme; }
        set { captchaTheme = value; }
    }

    public string CaptchaPrivateKey
    {
        get { return captchaPrivateKey; }
        set { captchaPrivateKey = value; }
    }

    public string CaptchaPublicKey
    {
        get { return captchaPublicKey; }
        set { captchaPublicKey = value; }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }


    public override void OnSaved(FormDisplayEventArgs e)
    {
        //If captcha control is enabled we need to adda  bit of code to redirect form properly
        if (CaptchaEnabled && e.Redirect && !e.SendMail)
        {
            //Do Stuff
        }

        if(sendEmail)
        {
            //Send email
        }

        base.OnSaved(e);
    }

    public override void OnSaving(FormDisplayEventArgs e)
    {
        if (CaptchaEnabled)
        {
            //Validate and do stuff
        }

        base.OnSaving(e);
    }
}

然后在我的asp.net页面中使用控件,由标记代码创建,在Page_Load()中我尝试为某些属性分配一些值,并且值设置不正确,这意味着如果我有设置为isntance,属性EmailBeforeText =“somthing”此值不会被分配..

protected void Page_Load(object sender, EventArgs e)
{
    //2: Get the language of menuitem - Based on current culture setting (for by dropdownbox - change logic)
    try
    {
        currentCulture = Thread.CurrentThread.CurrentCulture.ToString();

        // Redirect if domain does not match rootnode.
        DomainChecker.CheckURL(this.Request, this.Response, currentCulture);

        if (footerArticle != null)
            footerArticle.SourceMenuId = Digimaker.Config.Custom.Get("FooterID_" + currentCulture).ToString();
    }
    catch
    {
        currentCulture = "en-GB";

        if( footerArticle != null )
            footerArticle.SourceMenuId = Digimaker.Config.Custom.Get("FooterID_" + currentCulture).ToString();
    }

我在这里缺少什么想法?

非常感谢您的阅读!

此致 byte_slave

1 个答案:

答案 0 :(得分:1)

简短回答:使用viewstate来保留自定义值!

编辑:阅读白皮书显然是一件非常困难的事情:

  

每个控件负责存储自己的状态,即   通过将其更改的状态添加到其ViewState属性来完成。   ViewState属性在System.Web.UI.Control类中定义,   意味着所有ASP.NET服务器控件都具有此属性。   (一般来说,在讨论视图状态时,我会使用小写字母   视野与国家之间的空间;在讨论ViewState时   属性,我将使用正确的大小写和代码格式的文本。)

     

如果检查任何ASP.NET服务器控件的简单属性   您将看到属性直接读取和写入视图   州。 (您可以通过查看.NET程序集的反编译源代码   使用像Reflector这样的工具。)例如,考虑HyperLink Web   control的NavigateUrl属性。此属性的代码如下所示   这样:

public string NavigateUrl
{
  get
  {
    string text = (string) ViewState["NavigateUrl"];
    if (text != null)
       return text;
    else
       return string.Empty;
  }
  set
  {
    ViewState["NavigateUrl"] = value;
  }
}
  

正如此代码示例所示,只要控件的属性是   阅读,咨询控件的ViewState。如果没有条目   在ViewState中,然后返回属性的默认值。   分配属性后,将直接写入分配的值   到ViewState。