Click here to Skip to main content
15,902,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I have one problem in my project.Can anybody help me to sort out my problem..It is as below..

I have dynamically created link buttons with the name of the folders which contain my files and assigned unique id to them.
I have also included the event handler for them.

Now on the click of the link button,I want to know which button was clicked and store in a session variable the complete path of those link sp that I can access the text files stored in them on the next page.

So can anybody tell me how to identify those buttons?

The code is given below..THanks in advance..


string stringChapterno = Session["chapterno"].ToString();
string stringAppPath = HttpContext.Current.Request.ApplicationPath;
string stringPhysicalPath = HttpContext.Current.Request.MapPath(stringAppPath);
stringPhysicalPath = stringPhysicalPath + "\\App_Data\\" + stringChapterno;
//List<string> dirs = GetFilesRecursive(stringPhysicalPath);

foreach (string p in Directory.GetDirectories(stringPhysicalPath))
{
i = i + 1;
LinkButton lnkbtn = new LinkButton();
ViewState["btnlink" + i] = p.ToString();
string s = Path.GetFileNameWithoutExtension(p);
lnkbtn.Text = s;
lnkbtn.CssClass = "hyperlink";
lnkbtn.ID = "btnlink" + i;
Panel1.Controls.Add(lnkbtn);
Panel1.Controls.Add(new LiteralControl("<br>"));
Panel1.Controls.Add(new LiteralControl("<br>"));
lnkbtn.Click += new EventHandler(this.lnkbtn_Click);
HiddenField1.Value = p;
base.OnInit(e);
}

}
protected void lnkbtn_Click(object sender, EventArgs e)
{
???
}
Posted

C#
protected void lnkbtn_Click(object sender, EventArgs e)
{
    string id = ((LinkButton)sender).ID;
}
 
Share this answer
 
Use
C#
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
    return this.Page.FindControl(ctrlname);
}

It holds the UniqueID of the control that caused postback.
 
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