asp按钮不会在usercontrol updatepanel中调用回发

时间:2015-02-18 11:18:30

标签: c# asp.net postback

我在更新面板中的用户控件中有两个asp按钮。但是当我点击它们时,回发没有完成(我有一个java脚本警报设置为page_load告诉我何时发生回发,例如,当下拉列表选择被更改时它会触发)。我无法弄清楚为什么按钮没有回发。

我的用户控件,有问题的按钮是createBtn和signinBtn。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Login.ascx.cs" Inherits="TestApp2.Views.Login" %>

<asp:UpdatePanel ID="LoginMainUpdatePanel" runat="server" UpdateMode="Conditional" OnUnload="UpdatePanel_Unload">
    <ContentTemplate>
        <asp:TextBox ID="infoTB" runat="server" TextMode="MultiLine" />            
        <div>
            <div class="left-align" style="background-color:blue">
                <asp:button runat="server" id="createBtn" CssClass="btn btn-default" OnClick="createBtn_Click" Text="Create" /><br/>
                <asp:button runat="server" id="signinBtn" CssClass="btn btn-default" OnClick="signinBtn_Click" Text="Sign In" />
            </div>
            <!-- Login View -->
            <div id="loginView" runat="server" class="centre-form centering text-center" style="background-color:green">
                <h1>Login</h1>
                <asp:Label ID="info" runat="server" Text="" />
                    
                <div class="form-group">
                    <label for="userIn">Username</label>
                    <asp:TextBox runat="server" id="userIn" CssClass="form-control" TextMode="SingleLine" />
                    <asp:RequiredFieldValidator runat="server" id="reqUser" controltovalidate="userIn" errormessage="Enter Username" />
                </div>
                <div class="form-group">
                    <label for="passwordIn">Password</label>
                    <asp:TextBox runat="server" id="passwordIn" CssClass="form-control" Text="Enter Password" TextMode="Password" />
                    <asp:RequiredFieldValidator runat="server" id="reqPass" controltovalidate="passwordIn" errormessage="Enter Password" />
                </div>
                <asp:Button runat="server" id="loginBtn" CssClass="btn btn-default" onclick="loginBtn_Click" text="Login" />
            </div>

            <!-- Create Account View -->
            <div id="createView" runat="server" class="centre-form centering text-center" style="background-color:green">
                <h1>Create</h1>
                <div class="form-horizontal">
                    <div class="form-group" style="background-color:yellow;">
                        <asp:TextBox runat="server" id="personalFName" CssClass="form-control" TextMode="SingleLine" />
                        <label class="control-label" for="personalFName">First Name</label>
                    </div>

                    <div class="form-group">
                        <asp:TextBox runat="server" id="personalLName" CssClass="form-control" TextMode="SingleLine" />
                        <label class="control-label" for="personalLName" >Last Name</label>
                    </div>

                    <div class="form-group">
                        <asp:DropDownList runat="server" id="selGender" CssClass="form-control">
                            <asp:ListItem Value="Male" Text="Male" />
                            <asp:ListItem Value="Female" Text="Female" />
                            <asp:ListItem Value="Other" Text="Other" />
                        </asp:DropDownList>
                        <label class="control-label" for="selGender">Gender</label>
                    </div>
                    <asp:UpdatePanel ID="dobUpdatePanel" runat="server" UpdateMode="Conditional" OnUnload="UpdatePanel_Unload">
                        <ContentTemplate>
                            <div class="form-group">
                                <asp:DropDownList runat="server" id="selYear" CssClass="form-control" OnSelectedIndexChanged="selYear_SelectedIndexChanged" AutoPostBack="true" />
                                <asp:DropDownList runat="server" id="selMonth" CssClass="form-control" OnSelectedIndexChanged="selMonth_SelectedIndexChanged"  AutoPostBack="true" />
                                <asp:DropDownList runat="server" id="selDay" CssClass="form-control"/>
                                <label class="control-label" for="selDay" >Date of Birth</label>
                            </div>
                        </COntentTemplate>
                    </asp:UpdatePanel>
                </div> 
            </div> 

        </div>
   </ContentTemplate>
</asp:UpdatePanel>

背后的代码就是这样

 public partial class Login : System.Web.UI.UserControl
{
    private DateMenu dobCtrl;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Paths.USERDETAILS] != null) Response.Redirect(Paths.USERCTRL_BASE + "Profile.ascx");
        else setDetails();
    }

    protected void loginBtn_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            String SOAPbdy = "<Username>" + userIn.Text + "</Username><Password>" + passwordIn.Text + "</Password>";
            HTTPRequest req = new HTTPRequest();
            String response = req.HttpSOAPRequest(SOAPbdy, "GetUser");
            infoTB.Text += response;
            String uIDs = (new XMLParse(response)).getElementText("UserID");
            int uID = 0;
            Int32.TryParse(uIDs, out uID);

            if (uID > 0)
            {
                Session["userDetails"] = response;
                info.Text = "Success";
            }
            else info.Text = "Login failed";
        }
    }

    private void setDetails() 
    {
        if (dobCtrl == null) dobCtrl = new DateMenu(selYear, selMonth, selDay);
        dobCtrl.setDateDropdown();
    }

    protected void UpdatePanel_Unload(object sender, EventArgs e)
    {
        MethodInfo methodInfo = typeof(ScriptManager).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
            .Where(i => i.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")).First();
        methodInfo.Invoke(ScriptManager.GetCurrent(Page),
            new object[] { sender as UpdatePanel });
    }

    protected void selYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        dobCtrl.fillDays();
    }

    protected void selMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        dobCtrl.fillDays();
    }

    protected void createBtn_Click(object sender, EventArgs e)
    {
        loginView.Visible = false;
    }

    protected void signinBtn_Click(object sender, EventArgs e)
    {

    }
}

4 个答案:

答案 0 :(得分:0)

我相信您必须使用注册回发控件 ScriptManager.RegisterPostbackControl()

https://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerpostbackcontrol%28v=vs.110%29.aspx

答案 1 :(得分:0)

尝试设置UpdatePanel的触发器,如下所示:

<asp:UpdatePanel ID="LoginMainUpdatePanel" runat="server" UpdateMode="Conditional" OnUnload="UpdatePanel_Unload">
    <ContentTemplate>
        <asp:TextBox ID="infoTB" runat="server" TextMode="MultiLine" />            
        <div>
            <div class="left-align" style="background-color:blue">
                <asp:button runat="server" id="createBtn" CssClass="btn btn-default" OnClick="createBtn_Click" Text="Create" /><br/>
                <asp:button runat="server" id="signinBtn" CssClass="btn btn-default" OnClick="signinBtn_Click" Text="Sign In" />
            </div>
            [...]
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="createBtn" />
        <asp:PostBackTrigger ControlID="signinBtn" />
    </Triggers>
</asp:UpdatePanel>

答案 2 :(得分:0)

尝试为UpdatePanel添加PostBackTriggers。

为每个按钮添加2个触发器。

<asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional"ChildrenAsTriggers="false">
    <ContentTemplate>
      //Your Content
 </ContentTemplate>
    <Triggers>
      <asp:PostBackTrigger ControlID="createBtn" />
      <asp:PostBackTrigger ControlID="Signbtn" />
    </Triggers>
</asp:UpdatePanel>

答案 3 :(得分:0)

这是因为asp格式验证,因为表单无效,所以没有发生回发(异步或非异步)。删除它使一切工作。