Home All Groups Group Topic Archive Search About

Referencing to LinkButton in Gridview TemplateField

Author
1 Jul 2009 7:26 AM
Peter
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

Author
1 Jul 2009 9:13 AM
Peter
Show quote Hide quote
"Peter" <peter***@msn.com> wrote in message news:%23%23c9H1h%23JHA.4168@TK2MSFTNGP05.phx.gbl...
> 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
Are all your drivers up to date? click for free checkup

Author
1 Jul 2009 11:26 AM
Mark Rae [MVP]
"Peter" <peter***@msn.com> wrote in message
news:%23Fl8Axi%23JHA.1492@TK2MSFTNGP03.phx.gbl...

> if (cbConfirmCheck.Checked)


if (e.Checked)


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
1 Jul 2009 1:16 PM
Peter
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:uOHdN7j%23JHA.5064@TK2MSFTNGP03.phx.gbl...
> "Peter" <peter***@msn.com> wrote in message news:%23Fl8Axi%23JHA.1492@TK2MSFTNGP03.phx.gbl...
>
>> 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
        {
        }
    }
Author
1 Jul 2009 1:23 PM
Peter
Show quote Hide quote
"Peter" <peter***@msn.com> wrote in message news:uoTFY4k%23JHA.5780@TK2MSFTNGP03.phx.gbl...
>
> "Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:uOHdN7j%23JHA.5064@TK2MSFTNGP03.phx.gbl...
>> "Peter" <peter***@msn.com> wrote in message news:%23Fl8Axi%23JHA.1492@TK2MSFTNGP03.phx.gbl...
>>
>>> 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
Author
1 Jul 2009 1:42 PM
Mark Rae [MVP]
Show quote Hide quote
"Peter" <peter***@msn.com> wrote in message
news:%23y6Sa8k%23JHA.5040@TK2MSFTNGP04.phx.gbl...

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

> 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



--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
1 Jul 2009 2:06 PM
Peter
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:uu9nHHl%23JHA.5780@TK2MSFTNGP03.phx.gbl...
> "Peter" <peter***@msn.com> wrote in message news:%23y6Sa8k%23JHA.5040@TK2MSFTNGP04.phx.gbl...
>
>> 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

Show quoteHide quote
>> 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)
Author
1 Jul 2009 2:15 PM
Mark Rae [MVP]
Show quote Hide quote
"Peter" <peter***@msn.com> wrote in message
news:uWTtsUl%23JHA.4560@TK2MSFTNGP05.phx.gbl...

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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
1 Jul 2009 2:22 PM
Peter
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:ecSWnZl%23JHA.1252@TK2MSFTNGP04.phx.gbl...
> "Peter" <peter***@msn.com> wrote in message news:uWTtsUl%23JHA.4560@TK2MSFTNGP05.phx.gbl...
>
>> 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
Author
1 Jul 2009 2:30 PM
Mark Rae [MVP]
Show quote Hide quote
"Peter" <peter***@msn.com> wrote in message
news:uImScdl%23JHA.1376@TK2MSFTNGP02.phx.gbl...

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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
1 Jul 2009 3:36 PM
Peter
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:%23fOBzhl%23JHA.1380@TK2MSFTNGP02.phx.gbl...
> "Peter" <peter***@msn.com> wrote in message news:uImScdl%23JHA.1376@TK2MSFTNGP02.phx.gbl...
>
>>>> 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
Author
1 Jul 2009 11:23 AM
Mark Rae [MVP]
"Peter" <peter***@msn.com> wrote in message
news:%23%23c9H1h%23JHA.4168@TK2MSFTNGP05.phx.gbl...

> Can someone let me know why the name lbDetails cannot be located within a
> TemplateField in a GridView

It can...


> and what's the correct way of referencing the control ?

In which of the GridView's methods are you trying to reference it?

Please show some code...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
1 Jul 2009 1:19 PM
Peter
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:%23Pi2Y5j%23JHA.2872@TK2MSFTNGP05.phx.gbl...
> "Peter" <peter***@msn.com> wrote in message news:%23%23c9H1h%23JHA.4168@TK2MSFTNGP05.phx.gbl...
>
>> Can someone let me know why the name lbDetails cannot be located within a TemplateField in a GridView
>
> It can...
>
>
>> and what's the correct way of referencing the control ?
>
> In which of the GridView's methods are you trying to reference it?
>
> Please show some code...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net

Hi Mark

I would like to reference it from Page_Load, is that possible ?  Or do I need to reference it from the OnRowDataBound method ?
Because whether the button is shown can be determined from the beginning of the page and it applies to ALL rows.  Therefore I just
need to apply it when the page is being loaded.

Thanks
Peter
Author
1 Jul 2009 1:50 PM
Mark Rae [MVP]
"Peter" <peter***@msn.com> wrote in message
news:OtyJf6k%23JHA.1376@TK2MSFTNGP02.phx.gbl...

>>> and what's the correct way of referencing the control ?
>>
>> In which of the GridView's methods are you trying to reference it?
>>
>> Please show some code...
>
> I would like to reference it from Page_Load, is that possible?

The thing here is that there is no "it"... Controls inside a GridView are
not really accessible from Page_Load because they don't really "exist" until
the databinding process begins. When the page opens, it has no way of
knowing how many of these controls will eventually be created in databound
controls because it doesn't know how many rows there will be - maybe there
won't be any at all...


> Or do I need to reference it from the OnRowDataBound method ? Because
> whether the button is shown can be determined from the beginning of the
> page and it applies to ALL rows.  Therefore I just need to apply it when
> the page is being loaded.

If the button is in a separate column, you can simply hide that column
(.Visible = false) after the databinding process. If it is in the same
column as other controls, you will need to hide each "instance" of it on a
row-by-row basis in the RowDataBound method...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
1 Jul 2009 2:20 PM
Peter
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message news:%23nwiaLl%23JHA.1336@TK2MSFTNGP05.phx.gbl...
> "Peter" <peter***@msn.com> wrote in message news:OtyJf6k%23JHA.1376@TK2MSFTNGP02.phx.gbl...
>
>>>> and what's the correct way of referencing the control ?
>>>
>>> In which of the GridView's methods are you trying to reference it?
>>>
>>> Please show some code...
>>
>> I would like to reference it from Page_Load, is that possible?
>
> The thing here is that there is no "it"... Controls inside a GridView are not really accessible from Page_Load because they don't
> really "exist" until the databinding process begins. When the page opens, it has no way of knowing how many of these controls will
> eventually be created in databound controls because it doesn't know how many rows there will be - maybe there won't be any at
> all...
>
Thanks for your detail explanation. Is it the same applies to Page_LoadCompleted ?

>
>> Or do I need to reference it from the OnRowDataBound method ? Because whether the button is shown can be determined from the
>> beginning of the page and it applies to ALL rows.  Therefore I just need to apply it when the page is being loaded.
>
> If the button is in a separate column, you can simply hide that column (.Visible = false) after the databinding process. If it is
> in the same column as other controls, you will need to hide each "instance" of it on a row-by-row basis in the RowDataBound
> method...
>

I will give it a try in RowDataBound method in this case... thanks again

Peter

Bookmark and Share