使用占位符和母版页时POST不起作用

时间:2016-06-16 09:45:28

标签: c# asp.net post master-pages placeholder

我试着在我的“项目”中写一个日志页面。我找到了一些基于会话状态的解决方案。这是一个非常简单的项目并且工作正常但是当我在我的代码中实现它时它不起作用。它没有看到POST方法的值,它们是空的。我项目的唯一区别是我使用了MasterPage和占位符......这会不会影响数据流?

找到项目 - 控件

<form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server" BackColor="#FFE0C0" Height="50px" Width="192px">
        &nbsp;<asp:Label ID="Label1" runat="server" Text="login"></asp:Label>
        <asp:TextBox ID="TBlogin" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label2" runat="server" Text="hasło"></asp:Label>
        <asp:TextBox ID="TBhaslo" runat="server" TextMode="Password"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Zaloguj się!" Width="190px" PostBackUrl="loguj.aspx" />
    </asp:Panel>

    <asp:SqlDataSource ID="SDSUzytkownik" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringSklep %>" SelectCommand="SELECT * FROM [uzytkownik] WHERE (([login] = @Log) AND ([haslo] = @Has))">
        <SelectParameters>
            <asp:FormParameter DefaultValue="&quot;&quot;" FormField="TBlogin" Name="Log" />
            <asp:FormParameter DefaultValue="&quot;&quot;" FormField="TBhaslo" Name="Has" />
        </SelectParameters>
    </asp:SqlDataSource>

    <br />
    &nbsp;

    <asp:Label ID="LComment" runat="server" Text="Label" Visible="False"></asp:Label><br />
    <br />
    <asp:FormView ID="FVUzytkownik" runat="server" CellPadding="4" DataKeyNames="userID"
        DataSourceID="SDSUzytkownik" ForeColor="#333333" OnDataBound="FVUzytkownik_DataBound">

        <ItemTemplate>
            Hello
            <asp:Label ID="imieNazwiskoLabel" runat="server" Text='<%# Bind("imieNazwisko") %>'></asp:Label>!<br />
            <br />
            Your login:
            <asp:Label ID="loginLabel" runat="server" Text='<%# Bind("login") %>'></asp:Label><br />

            Your email:
            <asp:Label ID="emailLabel" runat="server" Text='<%# Bind("email") %>'></asp:Label><br />
        </ItemTemplate>

    </asp:FormView>
    <asp:Label ID="LabelTEST" runat="server" Text="LabelTestujący"></asp:Label>
    &nbsp;&nbsp;<br />
    <asp:Label ID="Label3" runat="server" Text="Label" Visible="False"></asp:Label>
</form>

背后的代码

protected void FVUzytkownik_DataBound(object sender, EventArgs e)
{
    LabelTEST.Text = "Im in";
    if (FVUzytkownik.Row != null)
    {
        Session["Logged"] = Request.Form["TBlogin"];
        Panel1.Visible = false;
        LZalogowanyJako.Text = "You are logged as:" + Session["Logged"];

        LabelTEST.Text = "<br/>Im logged <br/><br/> databse ROW =  " + FVUzytkownik.Row + "<br/><br/> Session =  " + Session["Logged"] + " <br/><br/> Request Form Login = " + Request.Form["TBlogin"];
    }
    else if (Request.Form["TBlogin"] != null && Session["Logged"] == null)
    {
        LComment.Text = "Logging failed. Wrong pass.";
        LComment.Visible = true;
        LabelTEST.Text = "<br/>Loggin failed <br/><br/> databse ROW =  " + FVUzytkownik.Row + "<br/><br/> Session =  " + Session["Logged"] + " <br/><br/> Request Form Login = " + Request.Form["TBlogin"];
    }
    else {
        LabelTEST.Text = "<br/> All failed <br/><br/> databse ROW =  " + FVUzytkownik.Row + "<br/><br/> Session =  " + Session["Logged"] + " <br/><br/> Request Form Login = " + Request.Form["TBlogin"];
    }
}

我的代码 - 母版页上的占位符

<div class="content">
    <form id="form1" runat="server" action="/acc/Log.aspx" method="post">
        <asp:ContentPlaceHolder ID="middle" runat="server">
        </asp:ContentPlaceHolder>
    </form>
</div>

放置持有人内容

<asp:Content ID="Content2" ContentPlaceHolderID="middle" runat="server">
    <h2>Logowanie</h2>
        <table style="width: 100%;">
            <tr>
                <td>Login:</td>
                <td class="auto-style1">
                    <asp:TextBox ID="loginInput" runat="server" placeholder="ImieNazwisko" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Pass:</td>
                <td class="auto-style1">
                    <asp:TextBox ID="hasloInput" runat="server" placeholder="8 znaków" Width="155px" TextMode="password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td class="auto-style1">
                    <asp:ImageButton ID="zaloguj" runat="server" Height="40px" ImageUrl="~/images/zaloguj.png" PostBackUrl="/logowanie/Logowanie.aspx" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [uzytkownicy] WHERE (([login] = @login) AND ([haslo] = @haslo))">
                        <SelectParameters>
                            <asp:FormParameter FormField="loginInput" Name="login" Type="String" />
                            <asp:FormParameter FormField="hasloInput" Name="haslo" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <asp:FormView ID="FVUzytkownik" runat="server" CellPadding="4" DataKeyNames="userID"
                        DataSourceID="SqlDataSource1" ForeColor="#333333" OnDataBound="FVUzytkownik_DataBound" EnableModelValidation="True">
                        <ItemTemplate>
                            Hello!
                         <br />
                            Your login:
                                 <asp:Label ID="loginLabel" runat="server" Text='<%# Bind("login") %>'></asp:Label><br />

                            <br />
                            Your email :
                       <asp:Label ID="emailLabel" runat="server" Text='<%# Bind("email") %>'></asp:Label><br />
                        </ItemTemplate>
                    </asp:FormView>
                </td>
                <td>
                    <asp:Label ID="LabelZalogowanyJako" runat="server" Text="message log "></asp:Label>
                    <br />
                    <asp:Label ID="LabelTEST" runat="server" Text="LabelTESTowanie"></asp:Label>
                </td>
            </tr>
        </table>
</asp:Content>

和代码背后相同

protected void FVUzytkownik_DataBound(object sender, EventArgs e)
{
    LabelTEST.Text = "Im in";
    if (FVUzytkownik.Row != null)
    {
        Session["Logged"] = Request.Form["TBlogin"];
        Panel1.Visible = false;
        LZalogowanyJako.Text = "You are logged as:" + Session["Logged"];

        LabelTEST.Text = "<br/>Im logged <br/><br/> databse ROW =  " + FVUzytkownik.Row + "<br/><br/> Session =  " + Session["Logged"] + " <br/><br/> Request Form Login = " + Request.Form["TBlogin"];
    }
    else if (Request.Form["TBlogin"] != null && Session["Logged"] == null)
    {
        LComment.Text = "Logging failed. Wrong pass.";
        LComment.Visible = true;
        LabelTEST.Text = "<br/>Loggin failed <br/><br/> databse ROW =  " + FVUzytkownik.Row + "<br/><br/> Session =  " + Session["Logged"] + " <br/><br/> Request Form Login = " + Request.Form["TBlogin"];
    }
    else {
        LabelTEST.Text = "<br/> All failed <br/><br/> databse ROW =  " + FVUzytkownik.Row + "<br/><br/> Session =  " + Session["Logged"] + " <br/><br/> Request Form Login = " + Request.Form["TBlogin"];
    }
}

和效果屏幕:

s

1 个答案:

答案 0 :(得分:0)

虽然从您发布的代码中我发现您尝试使用多个form标记,但目前还不是很清楚。好吧,尽管有可能,ASP.NET WebForms并不是那样设计的。如果您坚持使用多个表单,那么其中只有一个必须具有runat="server"属性。 但是如果需要这样的功能,最好切换到其他方法,例如asp:UpdatePanel