在asp.net的一个页面上有多个相同的表单.我怎么得到变量
本文关键字:表单 变量 net asp 一个 | 更新日期: 2023-09-27 18:12:57
我想不通。我不能解释得很好,所以这里有一些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";
}
}
那么,我如何才能获得隐藏字段宽度id"fest_id"的值在festival_details.aspx?这是我尝试过的:
HttpContext variables = HttpContext.Current;
strFormIdFest = variables.Request["fest_id"];
但是,字符串str350est总是空的。我想这和客户端有关系吧?
首先,asp.net webforms被设计成每个页面只使用一个表单。您应该考虑使用类似于重复器的控件来呈现同一表单中的控件。
。如果您正在查看(使用断点和快速监视)您的请求变量,您应该能够找到隐藏字段。但它没有命名为"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循环是否合适。这不是webforms的标准方式。看一下用于呈现复杂标记的重复器或其他控件。