简体   繁体   English

ASP表单参数名称已更改为包括母版页内容占位符

[英]ASP form parameter names being changed to include the master page contentplaceholder

Here is my page: 这是我的页面:

<%@ Page Language="C#" MasterPageFile="~/FBMaster.master" CodeFile="ViewOffer.aspx.cs" Inherits="ViewOffer" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <% 
        FlightBookingWS.FlightBookingSoapClient client = new FlightBookingWS.FlightBookingSoapClient();

        FlightBookingWS.Offer offer = client.GetOffer(Request.Form["OfferID"]);

        if (offer != null)
        {
    %>
    <div class="OfferDiv">
        <span><b>Origin Airport: </b><%=offer.OriginAirport ?? "" %></span>
        <span><b>Destination Airport: </b><%=offer.DestinationAirport ?? "" %></span>
        <span><b>Airline: </b><%=offer.Airline ?? ""%></span>
        <span><b>Available Seats: </b><%=offer.AvailableSeats%></span>
        <span><b>Number Of Connections: </b><%=offer.NumberOfConnections%></span>
        <%
            if (offer.Fare != null)
            {
        %>
        <span><b>Fare: </b><%=String.Format("{0:0.00} {1}", offer.Fare.Value, offer.Fare.Currency) %></span>
        <form runat="server">
            <span>
                <input type="hidden" id="OfferIDField" runat="server" />
                <input type="hidden" id="MessageField" runat="server" />
                <b>Number of Seats: </b>
                <asp:TextBox ID="NumSeatsField" runat="server" Text="1" />
                <asp:Button runat="server" Text="Book now" />
            </span>
        </form>
        <% 
                }
            }
            else
            {
        %>
        Offer not found.
        <%
            }
        %>
        <div id="ErrorBox" runat="server"></div>
    </div>
</asp:Content>

Whenever I submit the form, the keys used in the post data are changed from the IDs I wrote to the following ones: 每当我提交表单时,帖子数据中使用的密钥就会从我写的ID更改为以下ID:

编辑后的参数键

Ideally I'd like to access them using the same keys as the IDs of the input s they came from, like in normal HTML. 理想情况下,我想使用与input ID相同的键来访问它们,就像在普通HTML中一样。

That's not how ASP.NET web forms work 那不是ASP.NET Web表单的工作方式

When you put markup on a page with the runat="server" attribute, you are not actually writing page markup. 当您将标记放在带有runat="server"属性的页面上时,实际上并不是在写页面标记。 You are defining server-side controls that emit page markup. 您正在定义发出页面标记的服务器端控件。 You're not meant to use them like actual HTML elements. 您无意像实际的HTML元素一样使用它们。

When the page is posted back, the ASP.NET framework looks at the request message and parses all of the values. 页面回发后,ASP.NET框架将查看请求消息并解析所有值。 It then populates the server-side controls with the necessary data so you can retrieve it easily using ASP.NET syntax. 然后,它将用必要的数据填充服务器端控件,以便您可以使用ASP.NET语法轻松地检索它。

So, instead of 所以,代替

var offerID = Request.Form["ctl100$ContentPlaceHolder1#OfferIDField"]

you should simply use 你应该简单地使用

var offerID = this.OfferID.Text;

This is the way ASP.NET web forms work. 这就是ASP.NET Web表单的工作方式。

The old-fashioned way 老式的方式

If you'd rather do it the old-fashioned way, remove the runat="server" attribute and write your markup like regular HTML: 如果您希望采用老式的方法,请删除runat="server"属性,并像常规HTML一样编写标记:

<INPUT ID="OfferID" Name="OfferID">

...and then you can read it the "normal" way: ...然后您可以按“正常”方式阅读它:

var offerID = Request.Form["OfferID"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 母版页中contentplaceholder之外的更新面板导致回发到内容页 - An updatepanel outside of contentplaceholder in master page causes post back to content page 我可以知道母版页的ContentPlaceHolder中的哪个页吗? - Can I know what page is in my master page's ContentPlaceHolder? asp.net母版页css不适用于asp表单页面 - asp.net master page css not applying to asp form pages 您可以在.NET中异步更改母版页的ContentPlaceHolder的内容页吗? - Can you change a master page's ContentPlaceHolder's Content Page Asynchronously in .NET? 如果内容页面不包含,则仅在母版页中包含表单标签<form>标签? - Include form tag only in master page if content page not contains <form> tag? 带有母版页的ASP.Net Web表单中的JQuery - JQuery in ASP.Net Web form with a master page ASP.NET C#检查母版页中的FORM值 - ASP.NET C# Check for FORM value in Master Page 内容页面找不到ContentPlaceHolder&#39;ContentPlaceHolder1&#39; - Content page cannot find ContentPlaceHolder 'ContentPlaceHolder1' 主页中的ASP.NET MVC2登录表单 - ASP.NET MVC2 Login form in master page 我可以在Razor Partial View中填充母版页中的ContentPlaceHolder吗? - Can I populate a ContentPlaceHolder in a master page from within a Razor Partial View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM