在LoadControlState之后第二次不触发RaisePostBackEvent

时间:2012-10-14 10:31:44

标签: c# asp.net

我正在尝试使用输入字段和提交按钮创建一些控件。当我将此控件放在页面上并设置Click事件的处理程序时,它仅在第一次单击提交按钮时触发一次。但是,当我再次单击该按钮时,RaisePostBackEvent不会触发。我做错了什么?

public class AuthControl : Control, IPostBackEventHandler
{
    public int FailCount { get; set; }

    public AuthControl()
    {
    }

    public string LoginKey { get { return "login"; }}

    public string PasswordKey { get { return "password"; } }

    protected override void Render(HtmlTextWriter writer)
    {
        // <div>
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "field");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        // <input type="text" name="login" placeholder="login" />
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
        writer.AddAttribute(HtmlTextWriterAttribute.Name, LoginKey);
        writer.AddAttribute(HtmlTextWriterAttribute.Value, Page.Request.Form[LoginKey]);
        writer.AddAttribute("placeholder", LoginKey);
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

        // </div>
        writer.RenderEndTag();

        // <div>
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "field");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        // <input type="password" name="password" placeholder="password" />
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
        writer.AddAttribute(HtmlTextWriterAttribute.Name, PasswordKey);
        writer.AddAttribute(HtmlTextWriterAttribute.Value, Page.Request.Form[PasswordKey]);
        writer.AddAttribute("placeholder", PasswordKey);
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

        // </div>
        writer.RenderEndTag();

        // <div>
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        // <input type="submit" />
        writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
        writer.AddAttribute(HtmlTextWriterAttribute.Value, "Authorize");

        // </div>
        writer.RenderEndTag();

        if (FailCount > 0)
        {
            // <div>
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            // <span>
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("Осталось попыток: {0}", 3 - FailCount);

            // </span>
            writer.RenderEndTag();

            // </div>
            writer.RenderEndTag();
        }

        base.Render(writer);
    }

    protected override void OnInit(EventArgs e)
    {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }

    protected override object SaveControlState()
    {
        object baseState = base.SaveControlState();

        object currentState = new Pair(baseState, FailCount);

        return currentState;
    }

    protected override void LoadControlState(object savedState)
    {
        var stateLastRequest = (Pair)savedState;

        base.LoadControlState(stateLastRequest.First);

        FailCount = (int)stateLastRequest.Second + 1;
    }

    public event OnClickEventHandler Click;

    public void RaisePostBackEvent(string eventArgument)
    {
        if (Click != null)
        {
            Click(this, EventArgs.Empty);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对不起,这是我的错。在添加了控件的页面上有另一个按钮,并且未添加Render方法中的提交按钮(我不添加writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag();。这就是为什么我点击另一个按钮并且控制事件不会触发。