FindControl Masterpage problem

Last post 04-09-2008 9:53 AM by vgiambattista. 10 replies.

Sort Posts:

  • FindControl Masterpage problem

    04-08-2008, 3:49 PM
    • Loading...
    • dbrook007
    • Joined on 10-30-2006, 8:59 PM
    • Posts 38

     I have a problem using FindControl in a project.

    I am using Masterpages.

    On one of the pages (which uses a masterpage), I have an UpdatePanel within a ContentPlaceHolder.

    Within the UpdatePanel's ContentTemplate is a Panel.

    Within the Panel is a GridView.

    Within the GridView's ItemTemplate is a LinkButton.

    When the LinkButton is clicked and the Click event fires, I then want to read the CommandArgument property of the LinkButton.

    So, it is the LinkButton I am trying to find.

    However, so far I have not been able to find any controls in the GridView.

    I've tried a recursive FindControl but that did not work.

    So, the structure is:

    Content Page
       |
       --->ContentPlaceHolder
              |
              --->UpdatePanel
                    |
                    --->Panel
                         |
                         --->GridView
                              |
                              --->LinkButton (in ItemTemplate)

    Again, it's the LinkButton (or controls at that level) that I need to "find".

    Can anyone offer any help or advice on this?

    Many thanks,
    Darren


    Darren Brook
    e: darrenbrook@btconnect.com
    Filed under: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
  • Re: FindControl Masterpage problem

    04-08-2008, 4:18 PM

    Darren,

    Are you making sure you pass the ClientID of your LinkButton, rather than the plain ID, to FindControl?

    James Ashley, Magenic Technologies
    (james.ashley.magenic@gmail.com)
  • Re: FindControl Masterpage problem

    04-08-2008, 4:39 PM
    • Loading...
    • dojaju
    • Joined on 09-14-2007, 2:49 PM
    • Posts 26

    Hi Darren,

    In order to find which LinkButton inside the Gridview has been clicked, you will have to work in your aspx page and codebehind.

    1 - In your aspx page:

    <asp:TemplateField>

    <ItemTemplate>

    <asp:ImageButton id="Image1" runat="server"

                ImageUrl="~/App_Themes/Default/Images/DataGrid/loupe.png"

                CommandName="ShowDetails" 

                CommandArgument="<%# Container.DataItemIndex %>" >

    </asp:ImageButton>

    </ItemTemplate>

    </asp:TemplateField>

    2 - In your codebehind:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

       if (e.CommandName == "ShowDetails"){

           // with paging function of 20 result by page       

           int npos = Convert.ToInt32(e.CommandArgument) - (GridView1.PageIndex * 20); 

           // without paging function

          int npos = Convert.ToInt32(e.CommandArgument)

     

    }

    3 - Add to the gridview the event OnRowCommand:

    OnRowCommand="GridView1_RowCommand"

     

    Kind regards,
    Dojaju


    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: FindControl Masterpage problem

    04-08-2008, 4:54 PM
    • Loading...
    • dbrook007
    • Joined on 10-30-2006, 8:59 PM
    • Posts 38

    How do I pass the Client ID?

    Thanks. 

    Darren Brook
    e: darrenbrook@btconnect.com
  • Re: FindControl Masterpage problem

    04-08-2008, 4:58 PM
    • Loading...
    • dbrook007
    • Joined on 10-30-2006, 8:59 PM
    • Posts 38

    dojaju -> I'm not sure I follow what you are doing in the code... could you elaborate a little?

     

    Thanks

         

    Darren Brook
    e: darrenbrook@btconnect.com
  • Re: FindControl Masterpage problem

    04-08-2008, 5:36 PM
    Answer

    It's a property of your LinkButton control.  Out of curiosity, have you tried adding an event handler to your LinkButton?  You should be able to get what you need that way, for instance:

    protected void LinkButton1_Click(object sender, EventArgs e)

    {

    LinkButton btn = (LinkButton)sender;

    //string id = btn.ClientID;

    string arg = btn.CommandArgument;

    }

    James

    James Ashley, Magenic Technologies
    (james.ashley.magenic@gmail.com)
  • Re: FindControl Masterpage problem

    04-09-2008, 4:28 AM
    • Loading...
    • dbrook007
    • Joined on 10-30-2006, 8:59 PM
    • Posts 38

    Yes, I could do that.  And it works.... thanks!!! :-)

    Thanks for everyone's help - much appreciated.

    ...Darren     

     

    Darren Brook
    e: darrenbrook@btconnect.com
  • Re: FindControl Masterpage problem

    04-09-2008, 4:36 AM
    • Loading...
    • dojaju
    • Joined on 09-14-2007, 2:49 PM
    • Posts 26

    Hi Daren,

    Sorry, I can't help you more, and happy to see that you've got a solution. 

      

    Kind regards,
    Dojaju


    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: FindControl Masterpage problem

    04-09-2008, 4:38 AM
    • Loading...
    • dbrook007
    • Joined on 10-30-2006, 8:59 PM
    • Posts 38

    How would I get the ClientID if I was not in the event handler and thus did not have a reference to the control?

    In other words, in code where the control is not available? 

    Thanks - Darren 

     

    Darren Brook
    e: darrenbrook@btconnect.com
  • Re: FindControl Masterpage problem

    04-09-2008, 5:00 AM
    • Loading...
    • dbrook007
    • Joined on 10-30-2006, 8:59 PM
    • Posts 38

    What I'm having trouble with is this:

    In the UpdatePanel, I have two normal panels. Upon a visitor selecting an item from the first normal panel (via a LinkButton), I want to display the second panel (it's a master-detail type thing).

    But, I need to programmatically set a text property of a label that is in the second panel before the second panel is visible and rendered.  This would ideally be done in the Click event of the LinkButton in the first panel.  What I am having difficulty with is finding out how to do this.

    Any help appreciated.

    Thank you.

     


     

    Darren Brook
    e: darrenbrook@btconnect.com
  • Re: FindControl Masterpage problem

    04-09-2008, 9:53 AM
    Answer

    Darren,

    After trying out some code, it appears the FindControl method of the Page object wants the UniqueID, not the ClientID.  The first concatenates the control's id with any containers it is found it with colons, while ClientID does the same thing with underscores, in order to gen a special id.

    You can implement the GridView_RowDataBound event handler to grab hold of an instance of your templated control, like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Control c = e.Row.FindControl("Image1");

            ...

           }

    The Row object's FindControl method actually takes the simple ID property of your control as a parameter.  This would be the id you gave your control inside your Item template.  In my case, I had an image control called, naturally, Image1.

    I found out that it was the UniqueID I wanted by adding this bit of code:

    Control c = e.Row.FindControl("Image1");
                Control c2 = Page.FindControl(c.ClientID);
                Control c3 = Page.FindControl(c.ID);
                Control c4 = Page.FindControl(c.UniqueID);

    Only c4 actually evaluates to an object.  c2 and c3 are null. 

    My bad, Darren.  I apologize for misleading you.  To my chagrin, I don't know as much as I sometimes  think I do.

    --James

    James Ashley, Magenic Technologies
    (james.ashley.magenic@gmail.com)
Page 1 of 1 (11 items)
Microsoft Communities
Page view counter