我正在尝试使用输入字段和提交按钮创建一些控件。当我将此控件放在页面上并设置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);
}
}
}