【问题标题】:Multiple identical forms on a page in asp.net. How do I get variables?asp.net 中的一个页面上有多个相同的表单。如何获取变量?
【发布时间】:2013-05-04 04:56:40
【问题描述】:

我无法弄清楚这一点。我解释的不是很好,所以这里先写一些html/asp:

<%for(int i=0; i<getCountRows(); i++)
    {
        setLabelsFestivals(i);
%>
    <form action="festival_details.aspx" method="post">
        <table id="table_600px_border">
            <tbody class="content_table_left" style='padding: 10px;'>
                <tr>
                    <td class="content_table_300px content_table_left_up">
                        <div class="text_bold">
                            <asp:Label ID="nameFestival" runat="server"></asp:Label>
                        </div>
                        <asp:HiddenField ID="fest_id" runat="server" />
                    </td>
                    <td class="content_table_300px content_table_left_up_bottom">
                        <asp:Label ID="startDateFestival" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="content_table_left_up">
                        <asp:Label ID="locationFestival" runat="server"></asp:Label>
                    </td>
                    <td class="content_table_left_up">
                        <asp:Label ID="endDateFestival" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="content_table_left_up_bottom">
                        <asp:HyperLink ID="urlFestival" runat="server">Site</asp:HyperLink>
                    </td>
                    <td class="content_table_right_bottom">
                    <input type="submit" name="btnDetails" value="Details" />
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
<% }

对于数据集中的每一行,通过一个简单的提交按钮动态创建一个表单。 这就是代码背后发生的事情

//This method makes sure that the correct labels are shown on the screen
protected void setLabelsFestivals(int positionDataSet)
{
    //Checking if data is found for bands
    if (getCountRows() > 0)
    {
        DateTime dtStartDate = Convert.ToDateTime(dataSetFestivals.Tables[0].Rows[positionDataSet]["fest_datum"].ToString());
        DateTime dtEndDate = Convert.ToDateTime(dataSetFestivals.Tables[0].Rows[positionDataSet]["fest_einddatum"].ToString());

        //Showing all festivals and its data
        nameFestival.Text = dataSetFestivals.Tables[0].Rows[positionDataSet]["fest_naam"].ToString();
        fest_id.Value = dataSetFestivals.Tables[0].Rows[positionDataSet]["fest_id"].ToString();
        startDateFestival.Text = "Begindatum: " + dtStartDate.ToString("yyyy-MM-dd");
        locationFestival.Text = "Locatie: " + dataSetFestivals.Tables[0].Rows[positionDataSet]["fest_locatie"].ToString();
        endDateFestival.Text = "Einddatum: " + dtEndDate.ToString("yyyy-MM-dd");
        urlFestival.NavigateUrl = "http://" + dataSetFestivals.Tables[0].Rows[positionDataSet]["fest_url"].ToString();
        urlFestival.Target = "_blank";
    }
}

那么,例如,我怎样才能在festival_details.aspx 中获取隐藏字段宽度id“fest_id”的值? 这是我尝试过的:

HttpContext variables = HttpContext.Current;
strFormIdFest = variables.Request["fest_id"];

但是,字符串 strFormIdFest 始终为空。 估计跟clientId有关吧?

【问题讨论】:

    标签: c# asp.net forms submit httpcontext


    【解决方案1】:

    首先,asp.net 网络表单被设计为每页只使用一个表单。您应该考虑使用中继器之类的控件来在同一个表单中呈现您的控件。

    无论如何。如果您正在查看(使用断点和快速查看)您的请求变量,您应该能够找到隐藏字段。但它没有命名为“fest_id”,它有另一个名称,包含页面和控件名称以及一堆其他字符。这是所有 asp.net 控件(如 asp:HiddenField)的默认方式。

    如果您使用 asp.net 4.0,您可以通过将 ClientIDMode 设置为 Static 来强制 asp.net 将您的 id 控制:

    <asp:HiddenField ID="fest_id" runat="server" ClientIDMode="Static" />
    

    或者您可以使用标准的 html 隐藏控件来获取正确的 id,但在这种情况下您不能使用后面的代码来设置值:

     <input type="hidden" name="fest_id" id="fest_id">
    

    您应该考虑在您的 aspx 中使用 for 循环是否可行。它不是进入网络表单的标准方式。查看中继器或其他控件以呈现复杂的标记。

    【讨论】:

    • 非常清晰详细。谢谢。我已经设法将 clientId 用于我的目的,但我会调查你所说的中继器;)
    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 2020-04-22
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    相关资源
    最近更新 更多