Using a repeater to display a list,(with 'remove' btn beside each record) click 'remove' and it removes from the list. Im passing 'id' using commandArgument, I want to pass another value...'company name' tried it through text, but not sure how to pass it...then in code behind I use companyName to get contractorId, (company name for the contractor
<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>
In code behind:
HiddenField hdnCompanyName = (HiddenField)e.CommandArgument.ToString("hdnCompanyName);
string companyName = hdnCompanyName.Value;
Then problem is coming from code behind after the e.commandArgument I tried to do .findControl but it wouldnt allow me...ANyone know what I am missing? thank you
You can loop through the repeater items, get the item with the id passed through by CommandArgument='<%# Bind ("id") %>'
and call FindControl()
to get the company name, here's an example:
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>
Code behind:
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; }
}
Perhaps even a better solution would be to pass both id and company name to the CommandArgument
this will avoid extra processing created by looping through the repeater:
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>
Code behind:
Just split the string and there are both your parameters!
protected void Save(object sender, CommandEventArgs e)
{
string [] arguments = e.CommandArgument.ToString().Split(',');
string id = arguments[0];
string companyName = arguments[1];
}