1

使用中继器显示列表,(在每条记录旁边带有“删除”按钮)单击“删除”,它将从列表中删除。我使用commandArgument传递'id',我想传递另一个值......'公司名称'通过文本尝试过,但不知道如何传递它......然后在后面的代码中我使用companyName来获取contractorId,(公司名称对于承包商

<td>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Bind ("id") %>' runat="server" OnCommand="RemoveSubContractor" CssClass="bottomhyperlink">Remove</asp:LinkButton>
<asp:HiddenField id="hdnCompanyName" value='<%#Bind("Company_Name")%>'>
</td>

在后面的代码中:

HiddenField hdnCompanyName = (HiddenField)e.CommandArgument.ToString("hdnCompanyName); 
string companyName = hdnCompanyName.Value;

然后问题来自我尝试执行 .findControl 的 e.commandArgument 之后的代码,但它不允许我......有人知道我错过了什么吗?谢谢你

4

1 回答 1

3

您可以遍历中继器项目,获取通过的 id 的项目CommandArgument='<%# Bind ("id") %>'并调用FindControl()以获取公司名称,这是一个示例:

ASPX:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblId" runat="server" Text='<%# Eval("ID") %>' />
        <asp:HiddenField ID="hdCompanyName" runat="server" Value='<%# Eval("CompanyName") %>' />
        <asp:Button ID="btnSave" runat="server" OnCommand="Save" CommandArgument='<%# Eval("ID") %>' Text="Save" />
    </ItemTemplate>
</asp:Repeater>

后面的代码:

public partial class RepeaterExample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var employees = new List<Employee>()
            {
                new Employee{ Id="1",CompanyName="Company 1"},
                new Employee{ Id="2",CompanyName="Company 2"}
            };
            Repeater1.DataSource = employees;
            Repeater1.DataBind();
        }
    }

    protected void Save(object sender, CommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        string companyName = string.Empty;

        foreach (RepeaterItem item in Repeater1.Items)
        {
            Label lblId = item.FindControl("lblId") as Label;
            if (lblId.Text.Equals(id))
            {
                companyName = (item.FindControl("hdCompanyName") as HiddenField).Value;
                break;
            }
        }
        System.Diagnostics.Debug.WriteLine(companyName);
    }
}

public class Employee
{
    public string Id { get; set; }
    public string CompanyName { get; set; }
}

也许更好的解决方案是将id公司名称都传递给CommandArgumentthis 将避免通过循环通过中继器创建的额外处理:

ASPX:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblId" runat="server" Text='<%# Eval("ID") %>' />
        <asp:HiddenField ID="hdCompanyName" runat="server" Value='<%# Eval("CompanyName") %>' />
        <asp:Button ID="btnSave" runat="server" OnCommand="Save" CommandArgument='<%# String.Format("{0},{1}",Eval("ID"),Eval("CompanyName"))  %>' Text="Save" />
    </ItemTemplate>
</asp:Repeater> 

后面的代码:

只需拆分字符串,就有两个参数!

protected void Save(object sender, CommandEventArgs e)
{
    string [] arguments = e.CommandArgument.ToString().Split(',');
    string id = arguments[0];
    string companyName = arguments[1];
}
于 2013-02-10T14:45:31.907 回答