Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a content page i have added some linkbuttons. the number of linkbuttons varies depending on the data in the page. data is not in grid or any other data-controls.
i need to access the linkbutton which user clicks.
i used the following code ,

C#
int PageNumber = Convert.ToInt16(e.CommandArgument.ToString());
            string str = e.CommandName;
            LinkButton lbl = (LinkButton)FindControl(str);
            lbl.ForeColor = System.Drawing.Color.LightGreen;
            lbl.Font.Bold = true;

CommandName and id is also same.
CommandName is correctly received but the control could not be found.

Please suggest a solution
Posted
Updated 5-Sep-12 18:50pm
v2

Where do you add the controls? to a place holder? I have given below a rough idea of how you could do this:

Add a place holder that will have the labels:

XML
<form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="plcMain" runat="server">

        </asp:PlaceHolder>
        <asp:Label runat="server" ID="lblInfo"></asp:Label>
    </div>
    </form>


Code - behind:

C#
protected void Page_Load(object sender, EventArgs e)
{
    AddLinks();
}

private void AddLinks()
{
    for (int i=0;i<10;i++)
    {
        var linkButton = new LinkButton
                                    {
                                        ID = "lnkBtn" + i.ToString(),
                                        CommandArgument = i.ToString(),
                                        Text = "Link " + i.ToString()
                                    };
        linkButton.Click += new EventHandler(linkButton_Click);
        plcMain.Controls.Add(linkButton);
    }
}

protected void linkButton_Click(object sender, EventArgs e)
{
    var btn = sender as LinkButton;
    lblInfo.Text = "Link's command argument = " + btn.CommandArgument;
}


If you notice, you don't even need the FindControl method, just the linkButton_Click event handler. If you insist on using the FindControl method, you can do something like

C#
LinkButton control = plcMain.FindControl("lnkBtn1") as LinkButton; // Find "Link 1"
// Do something w/ "control"


In your case, you use the FindControl method on the page. Try a recursive FindControl if you don't wish to start the search with a certain control http://weblogs.asp.net/palermo4/archive/2007/04/13/recursive-findcontrol-t.aspx[^]
 
Share this answer
 
v3
Comments
sijo 2 6-Sep-12 1:00am    
Thanks Karthik for your time
Prasad_Kulkarni 6-Sep-12 1:08am    
'Accept Soution' for the time and effort of Karhik. He had given detailed description of what you want.
Control.FindControl[^] should do the trick for you. Dynamic controls should not matter.
 
Share this answer
 
Comments
sijo 2 6-Sep-12 1:00am    
Thanks Abhinav for your time
ContentPlaceHolder contentPage = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
            LinkButton lnkBtn = contentPage.FindControl("LinkButtonId") as LinkButton;
     if (lnkBtn!= null)  
     {  
     		//Do some action
     }


This worked correctly
 
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