[英]Pass variable value from aspx link button to code behind in ASP. NET using C#
Inside a for loop, how to pass the variable to the code-behind from an asp tag and access the variable value在 for 循环中,如何将变量从 asp 标记传递到代码隐藏并访问变量值
CommandArgument
works but shows <# gigs[x].Id%>
- not the value. CommandArgument
有效,但显示<# gigs[x].Id%>
- 不是值。
Aspx象素
<%for (int x = 0; x < gigs.Count; x++){%>
<asp:LinkButton ID="LinkButton2" CssClass="btn btn-danger mt-3" runat="server"
UseSubmitBehavior="false" CommandArgument="<# gigs[x].Id%>"
OnClick="LinkButton2_Click">Hide Gig <i class="fa fa-eye-slash ml-1"></i>
</asp:LinkButton>
Code behind背后的代码
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
String Value1 = lnk.CommandArgument;
Response.Write(Value1);
}
Result结果
<# gigs[x].Id%>
Expected Result预期结果
1 1
Try using <%# gigs[x].Id %>
instead and call Page.DataBind()
on the Page_Load
event.尝试改用
<%# gigs[x].Id %>
并在Page_Load
事件上调用Page.DataBind()
。
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag. <%= %> 是一个缩短的 response.Write() 并且对于任何服务器标记都不能作为属性有效。
<%# %> can be used, only if the container is databound (the page in your case). <%# %> 可以使用,只有当容器是数据绑定的(你的情况下的页面)。
You're much better off using a Strongly Typed Repeater.你最好使用强类型中继器。 You can set the
ItemType
as your class gigs
and have full access to all it's properties.您可以将
ItemType
设置为您的 class gigs
,并可以完全访问它的所有属性。 Then you can easily use them in the LinkButton.然后您可以轻松地在 LinkButton 中使用它们。
Repeater1.DataSource = gigs;
Repeater1.DataBind();
And then in the aspx然后在aspx中
<asp:Repeater ID="Repeater1" runat="server" ItemType="MyNameSpace.MyClass.Gig">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" CommandArgument='<%# Item.Id %>' OnClick="LinkButton2_Click" runat="server">Hide Gig</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.