Reputation: 969
How would I get the username from a repeater and put it into a label? i am using item command in my repeater but it wont output the user I need. My repeater has a select button ,forename,surname,department,username. I need the username please
protected void rptrAdmUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string pnumber = rptrAdmUserList.Items[3].ToString();
lblseluser.Text = "Selected user is: " + pnumber;
}
Upvotes: 0
Views: 5437
Reputation: 814
if your container[3] is empty you will need to figure out a way to populate it. If it is empty, do you have values in the container for forename,surname and department?
let us know.
Upvotes: 0
Reputation: 22511
Assuming that you output the username in a control (e.g. Label or TextBox) in each RepeaterItem, you need to identify the control dynamically in the ItemCommand-event handler.
The following sample shows how you can access the control. It assumes that the UserName is displayed in a TextBox with the ID txtUserName
:
protected void rptrAdmUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
var txtUserName = e.Item.FindControl("txtUserName") as TextBox;
if (txtUserName != null)
{
string pnumber = txtUserName.Text;
lblseluser.Text = "Selected user is: " + pnumber;
}
}
You can use the other properties of the RepeaterCommandEventArgs parameter e
to identify the command that was fired.
Upvotes: 1
Reputation: 9725
You need to have code like this on your button to pass in an argument - usually the ID...
<asp:LinkButton ID="btnDelete" CommandArgument=<%#Eval("ID") %> runat="server" Text="Delete" CommandName="Delete" ></asp:LinkButton>
Then in your code behind something like this to get the argument:
int iD = int.Parse(((LinkButton)e.CommandSource).CommandArgument);
Upvotes: 0