簡體   English   中英

asp.net中頁面上的多個相同表單。 如何獲得變量?

[英]Multiple identical forms on a page in asp.net. How do I get variables?

我很難弄清楚這一點。 我不能很好地解釋它,所以這里先介紹一些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始終為null。 我想這與clientId有關系嗎?

首先,asp.net Web窗體設計為每頁僅使用一個窗體。 您應該考慮使用類似中繼器的控件以相同的形式呈現控件。

無論如何。 如果您正在查看(使用斷點和快速監視)您的Request變量,則應該能夠找到隱藏的字段。 但是它沒有命名為“ 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循環。 它不是進入Web表單的標准方法。 看一下轉發器或另一個控件以呈現復雜的標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM