Referencing to LinkButton in Gridview TemplateField

  • Thread starter Thread starter Peter
  • Start date Start date
P

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
 
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 aTemplateField 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
 
Peter said:
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
 
Mark Rae said:
if (e.Checked)

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 said:
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
 
Mark Rae said:
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)
 
Mark Rae said:
Peter said:
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
 
Mark Rae said:
Peter said:
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
 
Back
Top