Question

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;
 }
Was it helpful?

Solution

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.

OTHER TIPS

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);
  1. put a breakpoint on: lblseluser.Text = "Selected user is: " + pnumber;
  2. Run your prog
  3. Hover over : rptrAdmUserList on the previous line...
  4. You should be able to see the content inside of rptrAdmUserList.
  5. Check the 4th item (0 based index [3]) for the username.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top