简体   繁体   English

如何将变量传递到另一个ASP.net页

[英]How to pass a variable to another ASP.net page

Okay so I have some c# that generated href anchor tags styled as list items and throws it onto an aspx page like so; 好的,我有一些c#生成了样式为列表项的href锚标签,并将其扔到aspx页面上;

html += "<a href='../InspectionView.aspx' class='list-group-item' id=''>Inspection ID: " + inspectionID + " - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";

Now this is in a loop, the variables are pulled from a SQL database and used to populate that html string. 现在这是一个循环,将变量从SQL数据库中提取出来并用于填充该html字符串。

Now, what I'm trying to do is have it so when the user clicks on one of the generated hrefs, and is redirected to the next page, the variable inspectionID is passed forward. 现在,我想做的就是拥有它,以便当用户单击生成的href之一并重定向到下一页时,将变量InspectionID向前传递。 I thought there might be someway of storing it in the ID of the href tag but I dont know where to go from there. 我认为可能会有某种方式将其存储在href标签的ID中,但我不知道从那里去哪里。

Thanks a lot. 非常感谢。

Add a query string parameter. 添加查询字符串参数。

html += "<a href='../InspectionView.aspx?inspectionID='" + inspectionID + " class='list-group-item' id=''>Inspection ID: " + inspectionID + " - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";

For reading on the receiving page: 在接收页面上阅读:

string inspectionID = Request.QueryString["inspectionID"];

See https://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring(v=vs.110).aspx 参见https://msdn.microsoft.com/zh-cn/library/system.web.httprequest.querystring(v=vs.110).aspx

a very simple way is to stick into a query string. 一种非常简单的方法是插入查询字符串。 Since this isn't a server control it might be the only way to it. 由于这不是服务器控件,因此可能是唯一的方法。

something like... 就像是...

html += "<a href='../InspectionView.aspx?InspectionID="+HttpUtility.UrlEncode(Inspection_ID.ToString())+"&anyotherQSField="+HttpUtility.UrlEncode(anyotherQSFieldVariable) + "' class='list-group-item'> - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";

Then in InspectionView.aspx,get values with something like: 然后在InspectionView.aspx中,获取类似以下内容的值:

String strInspection_ID = Request.QueryString["InspectionID"];

You likely need to convert to string for this to work for the ID. 您可能需要转换为字符串才能使用此ID。 You dont have to use HttpUtility.UrlEncode for Inspection_ID but if you have other strings you want to use in QS that might contain spaces or other odd characters - it would be wise. 您不必为Inspection_ID使用HttpUtility.UrlEncode,但如果要在QS中使用其他字符串,这些字符串可能包含空格或其他奇数字符,这将是明智的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM