gpt4 book ai didi

c# - 命令参数字符串未评估

转载 作者:行者123 更新时间:2023-11-30 16:35:39 24 4
gpt4 key购买 nike

我在 aspx 页面中有一个命令参数设置为 for 循环中的对象变量,如下所示:

<% foreach (PromotionImage p in imageList)
{
%>
<asp:LinkButton runat="server" OnCommand="deleteButton_Click" ID="deleteButton" CommandArgument="<%# p.ImageId.ToString(); %>" ForeColor="Red"
OnClientClick="javascript:return confirm('Are you sure you want to delete this item?');">X</asp:LinkButton>
<%
}
%>

然后在我后面的 c# 代码中,我有以下尝试获取此值:

protected void deleteButton_Click(object sender, CommandEventArgs e)
{
int imageId = System.Convert.ToInt32(e.CommandArgument.ToString());
}

但是 C# 代码不断返回“System.FormatException:输入字符串的格式不正确。”

当调试 e.CommandArgument 包含字符串“<%# p.ImageId.ToString(); %>”而不是实际的 ImageId 时,为什么它不评估?虽然我的所有其他变量都评估正常?

最佳答案

使用 <%# %>用于绑定(bind)。

使用 <%= p.propertyblahblah %>相反。

此外,不要这样做: commandArgument="..."而是使用单引号;还有';'可以在语句末尾删除。

-edit:最后一件事:为此使用中继器。

-edit2:好的。除非您使用中继器或某种其他类型的数据绑定(bind)控件,否则这确实行不通。 或者您必须在真实代码中执行此操作,例如:

    protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
LinkButton l = new LinkButton
{
CommandArgument = i.ToString(),
Text = i.ToString(),
};
l.Click += test_onclick;

holder.Controls.Add(l);
}
}
protected void test_onclick(object sender, EventArgs e)
{
var x = ((LinkButton)sender).CommandArgument;
}

关于c# - 命令参数字符串未评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1644224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com