Click here to Skip to main content
14,643,383 members
Rate this:
Please Sign up or sign in to vote.
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 ,

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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:

<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:

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

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[^]
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Control.FindControl[^] should do the trick for you. Dynamic controls should not matter.
   
Comments
sijo 2 6-Sep-12 1:00am
   
Thanks Abhinav for your time
Rate this:
Please Sign up or sign in to vote.

Solution 3

ContentPlaceHolder contentPage = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
            LinkButton lnkBtn = contentPage.FindControl("LinkButtonId") as LinkButton;
     if (lnkBtn!= null)  
     {  
     		//Do some action
     }


This worked correctly
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100