Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have drop the link button(exp: C.S Faculty's) on my page, If user clicks on the Link button it will dynamically generate the list of link button based on the number of Faculty's available on the database. After that if user click on the any one of the Faculty's name link button(which is dynamically generated link button), code should recognize which link button have click, I am not getting how to get it, please help me out.

Hear is the code

protected void LnkCS_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MainConnection"]);
            con.Open();
            string sm = string.Format("Select FName,FacID from Faculty where DeptID='cs'");
            SqlCommand cmd = new SqlCommand(sm, con);
            SqlDataAdapter adp = new SqlDataAdapter(sm, con);
            DataTable tab = new DataTable();
            adp.Fill(tab);

            for (int i = 0; i < tab.Rows.Count; i++)
            {
                TableRow Frow = new TableRow();
                TableCell Fcell = new TableCell();
                LinkButton FbtnLink = new LinkButton();
                FbtnLink.ID = tab.Rows[i][1].ToString();
                FbtnLink.Text = tab.Rows[i][0].ToString();
                FbtnLink.PostBackUrl = "UserFacultyPage2.aspx";
                


                Session["cs"] = FbtnLink.ID.ToString();
                
                Fcell.Controls.Add(FbtnLink);
                Frow.Cells.Add(Fcell);
                TableDeptFac.Rows.Add(Frow);

            }

        }


[edit]Code block inserted - OriginalGriff[/edit]
Posted
Updated 15-Apr-11 22:17pm
v2
Comments
amitkarnik2211 16-Apr-11 4:02am    
Pls insert Pre tags for code

1 solution

Just normal like at page_load FbtnLink.Click +=new EventHandler(FbtnLink_Click);.

But as you are doing a cross page posting make the accessibility of this page data through properties. So if you have

public string message { get; set; } then at Link button click

C#
protected void FbtnLink_Click(object sender, EventArgs e)
{
    message = "Hello Page2 From Page1";
}


this property can be accessed from the other page

At the markup register the type of previous page <%@ PreviousPageType VirtualPath="~/Default.aspx" %>

Response.Write(this.PreviousPage.message); .

You need to access the previous page at least one time to get the click event run. This is wired but some strange behavior.

Good luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900