我有一个包含一些Textbox和一个Button的WebControl。一切都很美。单击按钮时,我需要服务器对文本框的内容执行一些操作。为了在回发后保留文本框的内容,我将Textboxes和Button放在单独的UpdatePanels中,并将其UpdateMode设置为UpdatePanelUpdateMode.Conditional。我不需要在页面中处理此事件;我总是希望在单击按钮时执行WebControl中定义的btnSubmit_Click事件。
我认为这比举起包含Page的事件更容易处理,但是唉!到目前为止,我不能让事件发生。我在这里做错了什么?
public class EditUserSettings : WebControl
{
private TextBox txtShort_name;
private TextBox txtServco_No;
private TextBox txtEmp_No;
private Button btnSubmit;
private UpdatePanel pnlControls;
private UpdatePanel pnlButton;
protected override void CreateChildControls()
{
txtShort_name = new TextBox();
txtServco_No = new TextBox();
txtEmp_No = new TextBox();
btnSubmit = new Button();
pnlControls = new UpdatePanel();
pnlButton = new UpdatePanel();
pnlControls.UpdateMode = UpdatePanelUpdateMode.Conditional;
pnlButton.UpdateMode = UpdatePanelUpdateMode.Conditional;
btnSubmit.Text = "Submit";
btnSubmit.CssClass = "btn btn-primary btn-large";
btnSubmit.Click += new EventHandler(btnSubmit_Click);
txtShort_name.Text = HttpContext.Current.Profile["Short_name"].ToString();
txtServco_No.Text = HttpContext.Current.Profile["Servco_No"].ToString();
txtEmp_No.Text = HttpContext.Current.Profile["Emp_No"].ToString();
Panel pnlRow1 = new Panel();
Panel pnlRow2 = new Panel();
Panel pnlContainer = new Panel();
pnlRow1.CssClass = "row";
pnlRow2.CssClass = "row";
pnlContainer.CssClass = "span12";
pnlRow1.Controls.Add(new LiteralControl("Short Name: "));
pnlRow1.Controls.Add(txtShort_name);
pnlRow1.Controls.Add(new LiteralControl("Servco #: "));
pnlRow1.Controls.Add(txtServco_No);
pnlRow2.Controls.Add(new LiteralControl("Employee #: "));
pnlRow2.Controls.Add(txtEmp_No);
pnlControls.ContentTemplateContainer.Controls.Add(pnlRow1);
pnlControls.ContentTemplateContainer.Controls.Add(pnlRow2);
pnlButton.ContentTemplateContainer.Controls.Add(btnSubmit);
pnlContainer.Controls.Add(pnlControls);
pnlContainer.Controls.Add(pnlButton);
Controls.Add(pnlContainer);
base.CreateChildControls();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpContext.Current.Profile["Short_name"] = txtShort_name.Text;
HttpContext.Current.Profile["Servco_no"] = Convert.ToInt32(txtServco_No.Text);
HttpContext.Current.Profile["Emp_no"] = Convert.ToInt32(txtEmp_No.Text);
HttpContext.Current.Profile.Save();
pnlControls.Update();
}
}
更新:进一步的调试让我相信这是我的AJAX UpdatePanels的一个问题。我在包含WebControl的页面上的btnSubmit_Click代码和我的Page_Load中放置了断点,它得到了Page_Load而且从不btnSubmit_Click!我认为UpdatePanels应该保持整页回发不发生?!?
答案 0 :(得分:0)
在创建按钮中添加此代码:
btnSubmit.Attributes.Add("onclick", "btnSubmit_Click");