Search code examples
c#asp.netlinkbutton

How do I generate LinkButton's that are created from another Link_Command event on Postback?


Ok so I have a list of LinkButton's that upon being clicked are creating more LinkButton's inside of a table to do postback. For example, Each user has a list of activities or jobs. So when that user is clicked it creates a list of their jobs in a table. These jobs then need to do postback to get their respected content.

C# Code:

protected void Page_Load(object sender, EventArgs e) {
    try {
        lstUsers = GetUsers();
        AddLinkButton("all", "All", tabList, link_Command);
        foreach (var item in lstUsers) {
            AddLinkButton(item.UID.ToString(), item.Name, tabList, link_Command);
        }
    } catch (Exception ex) { 

    }
}

void link_Clicked(object sender, CommandEventArgs e) {
    List<Job> jList = GetJobs(e.CommandArgument.ToString());
    foreach (Job job in jList) {
        HtmlTableRow row = new HtmlTableRow();
        AddCell(job.JobID.ToString(), job.Name, row);
        jobsTable.Rows.Add(row);
    }
} 


void AddCell(string id, string text, HtmlTableRow row) {
    HtmlTableCell cell = new HtmlTableCell();
    LinkButton link = new LinkButton();
    link.ID = id;
    link.Text = text;
    link.Command += new CommandEventHandler(job_Command);
    link.CommandArgument = id;
    link.Style.Add("color", "black");
    cell.Controls.Add(link);
    row.Cells.Add(cell);
}

void AddLinkButton(string id, string text, HtmlGenericControl list, CommandEventHandler handler) {
        LinkButton link = new LinkButton();
        link.ID = id;
        link.Text = text;
        link.Command += new CommandEventHandler(handler);
        link.CommandArgument = id;
        HtmlGenericControl li = new HtmlGenericControl("li");
        li.Controls.Add(link);
        list.Controls.Add(li);
    }

Above in the Page_Load event I am creating the list of users. When a user is clicked it is firing link_Clicked. link_Clicked then creates a list of rows pertaining to that User, so activity or job links.

My problem is that I need to determine in Page_Load whether or not one of the links created in link_Clicked was actually clicked. If anyone has better suggestions to the way that I am doing this I am definitely all ears. I am very new to asp.net development so there is probably a better route to go for this.


Solution

  • Got it!

    string test1234 = Page.Request.Params.Get("__EVENTTARGET");
    

    That passes me back the ID of the control which is exactly what I needed.

    I found a tutorial here.