1

你能帮我,告诉我为什么我的代码不正确。我想创建一个将作为注册表单的 UserControl。在这个控件中,我有一个按钮,当单击该按钮时,我希望服务器重定向到另一个页面。这是我的代码:

控制.ascx.cs:

public partial class ControlFormulaire : System.Web.UI.UserControl
{
    public event EventHandler ButtonClicked;// it could be named differently obviously
    protected void ValidateButton_Click(object sender, EventArgs e)
    {
               if (this.ButtonClicked != null)
                    ButtonClicked(this, new EventArgs());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

表单.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    this.SubscribForm.ButtonClicked += new EventHandler(SubscribForm_ButtonClicked); 
}

void SubscribForm_ButtonClicked(object sender, EventArgs e)
{
    Response.Redirect("WebForm2.aspx");
}

这就是我的 form.aspx:

<%@ Register TagPrefix="uc" TagName="Form" Src="~/ControlFormulaire.ascx"%>
<uc:Form ID="SubscribForm" runat="Server" MinVal="1" MaxVal="10" />
4

2 回答 2

0

请试试这个

public partial class ControlFormulaire : System.Web.UI.UserControl
{
    public event EventHandler ButtonClicked;// it could be named differently obviously
    protected void ValidateButton_Click(object sender, EventArgs e)
    {
           ButtonClicked();
    }

    private void ButtonClicked()
    {
       if (ButtonClicked!= null)
       {
           ButtonClicked(this, EventArgs.Empty);
       }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    ControlFormulaire.ButtonClicked+= new EventHandler(SubscribForm_ButtonClicked);
}

void SubscribForm_ButtonClicked(object sender, EventArgs e)
{
    Response.Redirect("WebForm2.aspx");
}
于 2012-11-09T12:09:01.027 回答
0

此行是关键或自定义点击事件忽略更新面板
// ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(this);

并注意页面定义中的 IPostBackEventHandler 部分

在此示例中,普通按钮和自定义单击事件 div 在更新面板中一起工作

公共部分类 tmp2:System.Web.UI.Page,IPostBackEventHandler {

protected void Page_Load(object sender, EventArgs e) { ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(this); if (!Page.IsPostBack) { div1.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this, "ClickDiv"); } } protected void Button1_Click(object sender, EventArgs e) { Label1.Text += "1"; UpdatePanel1.Update(); } protected void Div1_Click() { div1.InnerHtml += "b"; UpdatePanel1.Update(); } public void RaisePostBackEvent(string eventArgument) { if (!string.IsNullOrEmpty(eventArgument)) { if (eventArgument == "ClickDiv") { Div1_Click(); } } }

}

于 2014-11-14T05:49:46.767 回答