简体   繁体   English

将变量值从 aspx 链接按钮传递给 ASP 中的代码。 NET 使用 C#

[英]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.

相关问题 如何使用asp.net C#中后面的代码使用子链接按钮在父级li中添加类? - How to add class in parent li using child link button from code behind in asp.net c#? C#ASP.NET使用来自ASP.NET背后代码的布尔变量 - C# ASP.NET using Boolean variable from code behind in ASP.NET 使用 C# 将在 JavaScript 中创建的变量值传递到 ASP.Net 中的服务器端(代码隐藏) - Pass the variable values created in JavaScript to Server Side (Code Behind) in ASP.Net using C# 尝试使用隐藏字段将值从ASP.NET JavaScript脚本传递给我的C#代码隐藏函数 - Trying to pass value from ASP.NET JavaScript script to my C# Code Behind function using Hidden Field 使用html控件的asp.net将值传递给C#背后的代码 - asp.net using html control to pass value to code behind c# 在.aspx中,如何将值从asp:Repeater传递给函数后面的代码 - In .aspx, how to pass a value from asp:Repeater to code behind function Asp.net从aspx的文本框中获取值以进行代码隐藏 - Asp.net get value from Textbox in aspx to code behind 如何将全局字符串变量从ASP.NET页后面的C#代码传递到经典ASP页? - How to pass a Global String Variable from the C# code behind of an ASP.NET page to a classic ASP page? 使用C#从代码后面添加ASPX组件 - Adding ASPX components from Code behind using C# 在ASP.NET中使用javascript传递在函数后面调用c#的变量值 - Pass variable value calling c# behind function with javascript in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM