选定的索引已更改事件未同时触发两个Autopostback属性

时间:2014-05-31 07:06:07

标签: c# asp.net c#-4.0

在我的Dropdownlist中选择的索引更改事件未触发。这里我使用自动回帖true& 查看状态也是如此。但是选择的索引已更改事件未触发

我的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminEagleViewLogin.aspx.cs" Inherits="AdminEagleViewLogin" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style>
        body{padding-top:20px;}
    </style>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="container">
    <div class="row">
          User :  <asp:DropDownList ID="drpusr" runat="server"  Visible="true" OnSelectedIndexChanged="drpusr_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true" ></asp:DropDownList>
       Password: <asp:Label ID="lbluserpw" runat="server"></asp:Label>
        <div class="col-md-4 col-md-offset-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Please sign in</h3>
                </div>
                <div class="panel-body">
                    <form accept-charset="UTF-8" role="form">
                    <fieldset>
                        <div class="form-group">
                           <asp:TextBox ID="txtusr" runat="server"></asp:TextBox>
                        </div>
                        <div class="form-group">
                             <asp:TextBox ID="txtpw" runat="server" TextMode="Password"></asp:TextBox>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input name="remember" type="checkbox" value="Remember Me"> Remember Me
                            </label>
                        </div>
                     <asp:CheckBox ID="chkremember" runat="server" Visible="false" class="remchkbox" />
                         <asp:Button ID="submit" runat="server" class="btn btn-lg btn-success btn-block" Text="Submit" OnClick="submit_Click" />
                    </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
    </form>
</body>
</html>

服务器端 用户绑定到Dropdown正在运行。

   public partial class AdminEagleViewLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindUsers();
        //lbluserpw.Text = Membership.Provider.GetPassword(drpusr.SelectedValue, String.Empty);
    }
    protected void submit_Click(object sender, EventArgs e)
    {

        if (Membership.ValidateUser(txtusr.Text, txtpw.Text))
        {
            FormsAuthentication.SetAuthCookie(txtusr.Text, chkremember.Checked);
            string[] CurrentUserRole = Roles.GetRolesForUser(txtusr.Text);

            var admin = "Administrator";
            var manager = "Manager";
            var user = "User";

            if (CurrentUserRole.Contains(admin))
            {
                Response.Redirect("Administrator.aspx");
            }
            else if (CurrentUserRole.Contains(manager))
            {
                Response.Redirect("Manager.aspx");
            }
            else
            {
                Response.Redirect("UserPage.aspx");
            }
        }
        else
        {
            Response.Redirect("AdminEagleViewLogin.aspx");

        }
    }

    protected void BindUsers()
    {
        DataAccess da = new DataAccess();
        drpusr.DataSource = da.GetUsers();
        drpusr.DataTextField = "UserName";
        drpusr.DataValueField = "UserId";
        drpusr.DataBind();

        drpusr.Items.Insert(0, new ListItem("-- Select User --", "0"));
        drpusr.Items.RemoveAt(1);

    }

    protected void drpusr_SelectedIndexChanged(object sender, EventArgs e)
    {
       lbluserpw.Text = Membership.Provider.GetPassword(drpusr.SelectedValue, String.Empty);


 }
}

2 个答案:

答案 0 :(得分:0)

您不应该将表单元素放在另一个表单中:

<form accept-charset="UTF-8" role="form">
  <fieldset>
    ...
    <asp:Button ID="submit" runat="server" class="btn btn-lg btn-success btn-block" Text="Submit" OnClick="submit_Click" />
   </fieldset>
 </form>

删除内部表格。

<强> [更新]

另一个问题是&#34; ID&#34;按钮的属性。 将其更改为其他内容,除了&#34;提交&#34;。

答案 1 :(得分:0)

试试这个:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindUsers();
    }
}