Referencing to LinkButton in Gridview TemplateField

Discussion in 'ASP .Net' started by Peter, Jul 1, 2009.

  1. Peter

    Peter Guest

    Hi

    I have the following GridView with a LinkButton in a TemplateField,

    <asp:GridView ID="ServersGrid" runat="server">
    <Columns>
    <asp:TemplateField HeaderText="Action">
    <ItemTemplate>
    <asp:LinkButton ID="lbDetails" runat="server" onfocus="this.blur()" Text="Details"
    CssClass="cntltext" Width="70" OnCommand="LinkButtonClick" CommandName="Details"
    CommandArgument='<%#Eval("Name") %>' Visible="false" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>

    I would like to change the Visible state in my Page_load code behind depending on some conditions as below but cannot locate the
    context name

    lbDetails.Visible = true;

    I also tried to use Page.FindControl("lbDetails") but returns null as well.

    Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView and what's the correct way of
    referencing the control ?

    Thanks
    Peter
    Peter, Jul 1, 2009
    #1
    1. Advertising

  2. On Jul 1, 9:26 am, "Peter" <> wrote:
    > Hi
    >
    > I have the following GridView with a LinkButton in a TemplateField,
    >
    >                     <asp:GridView ID="ServersGrid" runat="server">
    >                         <Columns>
    >                             <asp:TemplateField HeaderText="Action">
    >                                 <ItemTemplate>
    >                                     <asp:LinkButton ID="lbDetails" runat="server" onfocus="this.blur()" Text="Details"
    >                                         CssClass="cntltext" Width="70" OnCommand="LinkButtonClick" CommandName="Details"
    >                                         CommandArgument='<%#Eval("Name") %>'  Visible="false" />
    >                                 </ItemTemplate>
    >                             </asp:TemplateField>
    >                         </Columns>
    >
    > I would like to change the Visible state in my Page_load code behind depending on some conditions as below but cannot locate the
    > context name
    >
    > lbDetails.Visible = true;
    >
    > I also tried to use Page.FindControl("lbDetails") but returns null as well.
    >
    > Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView and what's the correct way of
    > referencing the control ?
    >
    > Thanks
    > Peter


    Peter, it's a nested control and you need to look for it against its
    naming container. For example, when it is in the first row, use

    Dim lb as LinkButton = ctype(GridView1.Rows(0).FindControl
    ("lbDetails"), LinkButton)

    More examples: http://forums.asp.net/p/998368/1311013.aspx

    Hope this helps
    Alexey Smirnov, Jul 1, 2009
    #2
    1. Advertising

  3. Peter

    Peter Guest

    "Peter" <> wrote in message news:%23%23c9H1h%...
    > Hi
    >
    > I have the following GridView with a LinkButton in a TemplateField,
    >
    > <asp:GridView ID="ServersGrid" runat="server">
    > <Columns>
    > <asp:TemplateField HeaderText="Action">
    > <ItemTemplate>
    > <asp:LinkButton ID="lbDetails" runat="server" onfocus="this.blur()" Text="Details"
    > CssClass="cntltext" Width="70" OnCommand="LinkButtonClick" CommandName="Details"
    > CommandArgument='<%#Eval("Name") %>' Visible="false" />
    > </ItemTemplate>
    > </asp:TemplateField>
    > </Columns>
    >
    > I would like to change the Visible state in my Page_load code behind depending on some conditions as below but cannot locate the
    > context name
    >
    > lbDetails.Visible = true;
    >
    > I also tried to use Page.FindControl("lbDetails") but returns null as well.
    >
    > Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView and what's the correct way
    > of referencing the control ?
    >
    > Thanks
    > Peter
    >


    I also have a Checkbox inside a TemplateField as below

    <asp:TemplateField HeaderText="Action">
    <ItemTemplate>
    <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateField>

    I would like to reference the button checked status with below but also cannot locate the context name

    protected void cbConfirm_Change(Object sender, EventArgs e)
    {
    if (cbConfirmCheck.Checked)
    {
    }
    else
    {
    }
    }

    Would someone please kindly help.

    Thanks
    Peter
    Peter, Jul 1, 2009
    #3
  4. Peter

    Peter Guest

    "Mark Rae [MVP]" <> wrote in message news:uOHdN7j%...
    > "Peter" <> wrote in message news:%23Fl8Axi%...
    >
    >> if (cbConfirmCheck.Checked)

    >
    >
    > if (e.Checked)
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net


    Hi Mark

    I have tried the followings but still gett CS0117: 'System.EventArgs' does not contain a definition for 'Checked'

    <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>


    with the following code behind

    protected void cbConfirm_Change(Object sender, EventArgs e)
    {
    if (e.Checked)
    {
    }
    else
    {
    }
    }
    Peter, Jul 1, 2009
    #4
  5. Peter

    Peter Guest

    "Peter" <> wrote in message news:uoTFY4k%...
    >
    > "Mark Rae [MVP]" <> wrote in message news:uOHdN7j%...
    >> "Peter" <> wrote in message news:%23Fl8Axi%...
    >>
    >>> if (cbConfirmCheck.Checked)

    >>
    >>
    >> if (e.Checked)
    >>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net

    >
    > Hi Mark
    >
    > I have tried the followings but still gett CS0117: 'System.EventArgs' does not contain a definition for 'Checked'
    >
    > <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    >
    >
    > with the following code behind
    >
    > protected void cbConfirm_Change(Object sender, EventArgs e)
    > {
    > if (e.Checked)
    > {
    > }
    > else
    > {
    > }
    > }
    >
    >


    Hi Mark

    What if I also need to reference the cbConfirmedCheck checkbox from the event "Delete_Click' on another control, what's the
    reference syntax ?

    <ItemTemplate>
    <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click"
    Width="70" Text="Delete"></asp:Button>
    </ItemTemplate>

    Thanks for your help.

    Peter
    Peter, Jul 1, 2009
    #5
  6. Peter

    Peter Guest

    "Mark Rae [MVP]" <> wrote in message news:uu9nHHl%...
    > "Peter" <> wrote in message news:%23y6Sa8k%...
    >
    >> I have tried the followings but still gett CS0117: 'System.EventArgs' does not contain a definition for 'Checked'
    >>
    >> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    >>
    >> with the following code behind
    >>
    >> protected void cbConfirm_Change(Object sender, EventArgs e)
    >> {
    >> if (e.Checked)

    >
    > Hmm...
    >
    > Try this instead:
    >
    > if(((CheckBox)sender).Checked)
    >

    This works.....thanks Mark

    >> What if I also need to reference the cbConfirmedCheck checkbox from the event "Delete_Click' on another control, what's the
    >> reference syntax ?

    >
    >> <ItemTemplate>
    >> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    >> <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click"
    >> Width="70" Text="Delete"></asp:Button>
    >> </ItemTemplate>

    >
    > Since the CheckBox and the Button are in the same Column (and, therefore, in the same tablecell once the GridView has been
    > rendered into an HTML table), you can do something like this:
    >
    > ((CheckBox)((Button)sender).Parent.Controls[0]).Checked
    >
    >

    Hm....don't quite understand the correct syntax in the Delete_Click event....can I still reference it by "sender" ? When I tried,
    I get the following....

    Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type
    'System.Web.UI.WebControls.CheckBox'.

    Source Error:

    Line 108: protected void Delete_Click(Object sender, CommandEventArgs e)
    Line 109: {
    Line 110: if (((CheckBox)((Button)sender).Parent.Controls[0]).Checked)
    Peter, Jul 1, 2009
    #6
  7. Peter

    Peter Guest

    "Mark Rae [MVP]" <> wrote in message news:ecSWnZl%...
    > "Peter" <> wrote in message news:uWTtsUl%...
    >
    >> Hm....don't quite understand the correct syntax in the Delete_Click event....can I still reference it by "sender" ? When I
    >> tried, I get the following....
    >>
    >> Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type
    >> 'System.Web.UI.WebControls.CheckBox'.
    >>
    >> Source Error:
    >>
    >> Line 108: protected void Delete_Click(Object sender, CommandEventArgs e)
    >> Line 109: {
    >> Line 110: if (((CheckBox)((Button)sender).Parent.Controls[0]).Checked)

    >
    >
    > You said you wanted to reference the CheckBox from the Delete Button - if the CheckBox isn't the first control in the Cell, you'll
    > need to modify Controls[0] accordingly...
    >
    >

    The checkbox is the first control in the cell as below, below is the exact code that I have, any comments ?

    <ItemTemplate>
    <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click"
    Width="70" Text="Delete"></asp:Button>
    </ItemTemplate>

    Peter
    Peter, Jul 1, 2009
    #7
  8. Peter

    Peter Guest

    "Mark Rae [MVP]" <> wrote in message news:%23fOBzhl%...
    > "Peter" <> wrote in message news:uImScdl%...
    >
    >>>> Line 110: if (((CheckBox)((Button)sender).Parent.Controls[0]).Checked)
    >>>
    >>> You said you wanted to reference the CheckBox from the Delete Button - if the CheckBox isn't the first control in the Cell,
    >>> you'll need to modify Controls[0] accordingly...
    >>>

    >> The checkbox is the first control in the cell as below, below is the exact code that I have, any comments ?
    >>
    >> <ItemTemplate>
    >> <asp:CheckBox ID="cbConfirmCheck" runat="server" OnCheckedChanged="cbConfirm_Change"></asp:CheckBox>
    >> <asp:Button ID="DeleteButton" CssClass="cntltext" runat="server" OnCommand="Delete_Click"
    >> Width="70" Text="Delete"></asp:Button>
    >> </ItemTemplate>

    >
    > 1) Put a breakpoint on line 110 above
    >
    > 2) Inspect the value of sender - is it *definitely* the Delete button?
    >
    > 3) Assuming it is, inspect ((Button)sender.Parent.Controls.Count - how many Controls are in the Delete Button's Parent (i.e.
    > Cell)?
    >
    > 4) Inspect the type of each control - GetType().Name
    >
    > Essentially, what you're trying to do is walk backwards through the control hierarchy until you find the Control you're interested
    > in...
    >



    Hi Mark

    I managed to get the following syntax to get what I want.

    ((CheckBox)((Button)sender).Parent.Controls[0].FindControl("cbConfirmCheck")).Checked

    Thanks for your help again.

    Peter
    Peter, Jul 1, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    3
    Views:
    3,100
    Sathyaish
    Sep 8, 2005
  2. lupina
    Replies:
    3
    Views:
    27,212
    =?Utf-8?B?RWx0b24gVw==?=
    Mar 1, 2006
  3. sloan
    Replies:
    2
    Views:
    4,897
    aryan_subhash
    Oct 17, 2007
  4. mesut
    Replies:
    0
    Views:
    568
    mesut
    Jun 29, 2007
  5. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    182
    Sathyaish
    Sep 8, 2005
Loading...

Share This Page