本文介绍了不评估commandargument串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须设置为一个对象变量里面一个for循环,如下一个aspx页面内commandargument:

I have a commandargument inside an aspx page set to an object variable inside a for loop as below:

<% 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#code后面我有以下的,试图得到这个值:

Then in my c# code behind I have the following to try to get this value:

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

然而,C#code保持返回System.FormatException:输入字符串的不正确的格式。

However the c# code keeps returning "System.FormatException: Input string was not in a correct format."

在调试e.CommandArgument包含字符串&LT;%#p.ImageId.ToString();%>,而不是实际的ImageId,它为什么没有评估?尽管所有我的其他变量计算罚款?

When debugging the e.CommandArgument contains the string "<%# p.ImageId.ToString(); %>" rather than the actual ImageId, why is it not evaluating? Though all my other variables evaluate fine?

推荐答案

使用&LT;%#%方式&gt; 用于结合

使用&LT;%= p.propertyblahblah%方式&gt; 而不是

此外,不要做:commandArgument =...,但使用单引号代替;也';'可以在你的语句结尾被丢弃。

Furthermore, don't do: commandArgument="..." but use single quotes instead; also the ';' can be dropped at the end of your statement.

-edit:最后一件事:只使用一个中继器为这个

-edit: One last thing: just use a repeater for this.

-edit2:好的。这是忠实地不会,除非你使用一个中继器或一些其他类型的数据绑定控件的工作。的或者的你就必须做到这一点在现实code,如:

-edit2: Ok. this is truely not going to work unless you're using a repeater or some other kind of databound control. Or you'll have to do this in real code like:

    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;   
    }

这篇关于不评估commandargument串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 13:07