[英]Pass value back to server in asp.net?
In an asp.net application, I have the follow code in the aspx page: 在asp.net应用程序中,我在aspx页面中具有以下代码:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var questionPaper in QuestionPapers) { %>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<%= questionPaper.University %><br/>
<%= questionPaper.CourseName %>: <%= questionPaper.CourseCode %><br/>
<%= questionPaper.Type %><br/>
<%= questionPaper.Year %><br/>
<asp:Button ID="View_Paper" runat="server" OnClick="ViewPaper" Text="View Paper"/>
</div>
<% } %>
</asp:Content>
I want to pass questionPaper.ID back to the ViewPaper() event handler on the server side, how do I do that? 我想将questionPaper.ID传递回服务器端的ViewPaper()事件处理程序,该怎么办?
public void ViewPaper(object sender, EventArgs e)
{
}
My recommendation is to use a Repeater
or a ListView
control 我的建议是使用Repeater
或ListView
控件
Use a Repeater
if the data will be used as read-only 如果数据将用作只读,请使用Repeater
Use a ListView
if you want to page your results and/or allow the end user to perform CRUD operation over your data 如果要分页结果和/或允许最终用户对数据执行CRUD操作,请使用ListView
For example, your code would look like this when using a Repeater
and an ObjectDataSource
controls: 例如,当使用Repeater
和ObjectDataSource
控件时,您的代码将如下所示:
<asp:ObjectDataSource ID="ods" runat="server"
SelectMethod="GetQuestionPapers"
TypeName="Your_Namespace.PapersRepository">
</asp:ObjectDataSource>
<asp:Repeater runat="server" DataSourceID="ods" ID="r" OnItemCommand="r_ItemCommand">
<ItemTemplate>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<asp:HiddenField runat="server" ID="paperID" Value='<%# Eval("PaperID") %>'/>
<asp:Label runat="server" ID="university" Text='<%# Eval("University") %>'/><br />
<asp:Label runat="server" ID="courseName" Text='<%# Eval("CourseName") %>'/>:<asp:Label runat="server" ID="courseCode " Text='<%# Eval("CourseCode ") %>'/><br />
<asp:Label runat="server" ID="paperType" Text='<%# Eval("Type") %>' /><br />
<asp:Label runat="server" ID="year" Text='<%# Eval("Year") %>' /><br />
<asp:Button ID="View_Paper" runat="server" Text="View Paper" CommandName="ViewPaperCommand"
/>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
protected void r_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "ViewPaperCommand":
var hidden = e.Item.FindControl("paperID") as HiddenField;
var myPaperID = hidden.Value;
break;
}
}
namespace Your_Namespace
{
public class PapersRepository
{
public IEnumerable < QuestionPaper > GetQuestionPapers()
{
return QuestionPapers.AsEnumerable();
}
}
}
You could bind it to a control and then reference that control directly? 您可以将其绑定到控件,然后直接引用该控件?
I know you have multiple questions papers being output, so that in itself is the issue. 我知道您正在输出多个问题文件,所以这本身就是问题。 It's been a while since I have used webforms, but could you not use a repeater control which would give you a row context within which you could get the specific question paper id you are looking for? 自从我使用网络表单以来已经有一段时间了,但是您是否可以不使用转发器控件,该控件将为您提供一个行上下文,您可以在其中获得所需的特定试卷ID?
Use a repeater. 使用中继器。 That kind of old skool ASP classic code has no place in ASP.Net projects. 那种老套的ASP经典代码在ASP.Net项目中没有位置。 Here's an example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=663 这是一个示例: http : //www.dotnetcurry.com/ShowArticle.aspx?ID=663
It is possible to customise a repeater to make a lightweight DataGrid or ListView equivalent too. 可以自定义中继器以使轻量级DataGrid或ListView也等效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.