如何在ASP.NET中将C#变量作为CommandArgument传递 [英] How to pass a C# variable as a CommandArgument in ASP.NET

查看:81
本文介绍了如何在ASP.NET中将C#变量作为CommandArgument传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建一个数组,为某些数据的每个条目添加一行.每个条目都必须有一个按钮来执行一些操作,但是我现在仍然停留在该位置.我有以下代码来创建 ASP.NET 按钮:

I am creating dynamically an array, adding a line for each entry of some data. Each entry must have a button to do some stuff with it, but I am stuck at this point. I have this code to create the ASP.NET button:

<asp:Button ID="buttonyes" class="buttonyes" CommandArgument='<%= request.ID %>' 
            CommandName="AcceptRequest" OnClick='<%= ButtonPressed %>' runat="server"/>

但是,当我运行它时,我有多个错误:

But when I run it, I have multiple errors saying that:

%= < > 表达式无效.

当我单击按钮时,我想调用在与我的 index.aspx 链接的 index.aspx.cs 文件中编写的函数,这是 ButtonPressed 函数,并在此函数中带有过滤器,它可以选择应执行的操作:

When I click on the button, I want to call a function written in the index.aspx.cs file linked with my index.aspx, here is ButtonPressed function and with a filter in this function, it choose what action it should do:

protected void ButtonPressed(Object sender, CommandEventArgs e)
{
    Button btn = (Button)sender;
    switch (btn.CommandName)
    {
        case "AcceptRequest":
            AcceptRequest(btn.CommandArgument.ToString());
            break;
        case "RefuseRequest":
            RefuseRequest(btn.CommandArgument.ToString());
            break;
    }
}

问题是我没有成功将那些 C#变量作为 CommandArgument CommandName 传递.

The problem is that I don't succeed in passing those C# variables as a CommandArgument and CommandName.

我已经按照之前的一些问题尝试过这些语法:

I already tried those syntaxes, following some previous questions:

  • CommandArgument =<%:request.ID%>"
  • CommandArgument ='<%:request.ID%>'
  • CommandArgument =<%= request.ID%>"
  • CommandArgument ='<%= request.ID%>'

但是他们都不起作用.

如下所述, @ grek40 ,应替换 OnClick 值,如下所示:

As @grek40 said below, the OnClick value should be replace like the following:

<asp:Button ID="buttonyes" class="buttonyes" CommandArgument='<%= request.ID %>' 
            CommandName="AcceptRequest" OnCommand="ButtonPressed" runat="server"/>

我想知道如何传递C# local 变量,因为应该将这些按钮添加到 foreach 循环中,如下所示:

I would like to know how to pass a C# local variable since those button should be added in a foreach loop like so:

foreach (Request request in requests)
     {
     %>
         <tr>
              <td><%: request.applicant.Name + " (" + request.applicant.ID + ")" %></td>
              <td><%: request.software.Name %></td>
              <td><%: request.date %></td>
              <td class="actions">
                   <asp:Button ID="buttonyes" class="buttonyes" CommandName="AcceptRequest" CommandArgument="<# request.Id %>"  OnCommand="ButtonPressed" runat="server"/>
                   <asp:Button ID="buttonno" class="buttonno" CommandName="RefuseRequest" CommandArgument="<# request.Id %>" OnCommand="ButtonPressed" runat="server"/>
              </td>
          </tr>
      <%
      }

顺便说一句,上面的代码不起作用,因为正如 @Nino 所说,这仅适用于全局变量.

By the way, the code above doesn't work, since, as @Nino said, this only works for global variables.

推荐答案

为什么不为自己省去很多麻烦,而改用强类型中继器控件.您可以使用 ItemType 属性使其安全.该值必须是类 request

Why not save yourself a whole lot of trouble and switch to a Strongly Typed Repeater Control. You can make it type safe with the ItemType property. The value must the the complete namespace to the class request

然后,您仍然可以像现在一样访问属性.但是,您可以使用固定的名称 Item .而不是使用 request .

Then you will still be able to access the properties like you have now. But instead of using request, you use the fixed name of Item.

<table border="1">
    <asp:Repeater ID="Repeater1" runat="server" ItemType="Namespace1.Default1.request">
        <ItemTemplate>
            <tr>
                <td><%# Item.applicant.Name + " (" + Item.applicant.ID + ")" %></td>
                <td><%# Item.software.Name %></td>
                <td><%# Item.date.ToLongDateString() %></td>
                <td class="actions">
                    <asp:Button ID="buttonyes" class="buttonyes" CommandName="AcceptRequest" CommandArgument='<%# Item.Id %>' OnCommand="ButtonPressed" runat="server" />
                    <asp:Button ID="buttonno" class="buttonno" CommandName="RefuseRequest" CommandArgument='<%# Item.Id %>' OnCommand="ButtonPressed" runat="server" />
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

现在,您可以使用常规的数据绑定方式: CommandArgument ='<%#Item.Id%>'

Now you can use the normal way of databinding: CommandArgument='<%# Item.Id %>'

下面是完整的测试代码...

Below the full code for testing...

public List<request> requests = new List<request>();

protected void Page_Load(object sender, EventArgs e)
{
    //create some dummy data
    for (int i = 0; i < 10; i++)
    {
        request r = new request() { Id = i, date = DateTime.Now };
        r.applicant = new applicant() { Name = "Applicant " + i, ID = 1 + 100 };
        r.software = new software() { Name = "Software " + i };
        requests.Add(r);
    }

    if (!Page.IsPostBack)
    {
        Repeater1.DataSource = requests;
        Repeater1.DataBind();
    }
}

public class request
{
    public int Id { get; set; }
    public DateTime date { get; set; }
    public applicant applicant { get; set; }
    public software software { get; set; }
}

public class applicant
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class software
{
    public int ID { get; set; }
    public string Name { get; set; }
}

这篇关于如何在ASP.NET中将C#变量作为CommandArgument传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆