سؤال

I'm having trouble figuring this out. I can't explain it that well, so here some html/asp first:

<%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>
<% }

For each row in a dataset, a form is dynamically created with a simple submit button. This is what happens in code behind

//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";
    }
}

So, how can I for example get the value of the hidden field width id "fest_id" in festival_details.aspx? This is what I tried:

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

However, the String strFormIdFest is always null. I guess it's got something to do with the clientId?

هل كانت مفيدة؟

المحلول

First off, asp.net webforms are designed to only use one form per page. You should consider using a control like a repeater to render your your controls within the same form.

Anyhow. If you are looking (using a breakpoint and a quick watch) at your Request variables you should be able to find the hidden field. But its not named "fest_id", it got another name containing the page and control name and a bunch of other characters. This is the default way for all asp.net controls (like asp:HiddenField).

You may force asp.net to give the control your id if you are using asp.net 4.0 by setting ClientIDMode to Static:

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

Or you may use the standard html hidden control to get the correct id but you cannot use code behind to set the value in this case:

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

You should consider if a for loop in your aspx is the way to go. Its not the standard way to go in webforms. Take a look at the repeater or another control to render complex markup.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top