【问题标题】:POST doesnt works when using Placeholders and Masterpage使用占位符和母版页时 POST 不起作用
【发布时间】:2016-10-17 18:11:06
【问题描述】:

我尝试将日志记录页面写入我的“项目”。我找到了一些基于会话状态的解决方案。这是一个非常简单的项目并且工作正常,但是当我在我的代码中实现它时它不起作用。它看不到 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"];
    }
}

以及效果画面:

【问题讨论】:

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


    【解决方案1】:

    不是很清楚,但从您发布的代码中我看到您正在尝试使用多个 form 标记。好吧,虽然有可能,但 ASP.NET WebForms 并不是这样设计的。如果您坚持使用多种形式,那么其中只有一种必须具有runat="server" 属性。 但是如果你需要这样的功能,你最好改用其他方法,比如asp:UpdatePanel

    【讨论】:

    • 我曾经在母版页和其中的占位符上使用过“表单”。第二种方法是从母版页中删除它并放入占位符,但功能没有任何改变,它仍然不起作用。
    • 是的,我说的是呈现的 HTML。它包含两个表单,因为您在母版页和占位符中都使用了该标记——这在 WebForms 中不是一个好的做法。并且不要从母版页中删除表单标签; ASP.NET WebForms 需要它才能正常工作。
    • 嗯。好的,所以我从占位符中删除了form...您建议使用asp:UpdatePanel,但哪些部分应该在里面?仍然在等待来自 POST 的值的“隐藏代码”部分呢?其实我也不知道怎么弄
    • 使用UpdatePanels 会利用所谓的“部分页面更新”。通过实现它,您可以向服务器发送任何数据,无论它是否在表单上。它代表一种方法,而不仅仅是编码元素,因此您应该在开始实施之前熟悉自己。在后面的代码中,使用页面上声明的控件,而不是在请求的字段中查找已发布的值...
    猜你喜欢
    • 2012-09-28
    • 2013-10-11
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多