Connecting Tech Pros Worldwide Help | Site Map

Doesn't find grid control

Member
 
Join Date: Jan 2009
Posts: 47
#1: May 7 '09
Can someone help? I have the code below in which I'm attempting to show or hide the edit control in a gridview based on the value of a column in that view. It doesn't seem to find the control. I've included the RowDataBound event as well as the gridview definition. Thanks in advance.
Expand|Select|Wrap|Line Numbers
  1. protected void gvEmployeesBenefits_RowDataBound(object sender, GridViewRowEventArgs e)
  2.     {
  3.         TextBox EditFlg = (TextBox) e.Row.FindControl("txtEditflg");
  4.         if (EditFlg.Text == "1")
  5.         {
  6.             LinkButton MyLinkButton = (LinkButton)e.Row.FindControl("lnkEdit");
  7.             MyLinkButton.Visible = true;
  8.         }
  9.         else
  10.         {
  11.             LinkButton MyLinkButton = (LinkButton)e.Row.FindControl("lnkEdit");
  12.             MyLinkButton.Visible = false;
  13.         }
  14.  
  15.     }
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="gvEmployeesBenefits" runat="server" DataSourceID="Benefits" Style="z-index: 100;
  2.     left: 0px; top: 0px" AutoGenerateColumns="False" BackColor="White" 
  3.     BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
  4.     GridLines="Horizontal" onrowdatabound="gvEmployeesBenefits_RowDataBound" 
  5.     CellSpacing="3" HorizontalAlign="Justify">
  6.     <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
  7.     <Columns>
  8.         <asp:TemplateField ShowHeader="False">
  9.             <EditItemTemplate>
  10.                 <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
  11.                     CommandName="Update" Text="Update"></asp:LinkButton>
  12.                 &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
  13.                     CommandName="Cancel" Text="Cancel"></asp:LinkButton>
  14.             </EditItemTemplate>
  15.             <ItemTemplate>
  16.                 <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" 
  17.                     CommandName="Edit" Text="Edit"></asp:LinkButton>
  18.             </ItemTemplate>
  19.         </asp:TemplateField>
  20.         <asp:BoundField DataField="BGAN8" HeaderText="Emp#" 
  21.             ReadOnly="True" Visible="False" />
  22.         <asp:BoundField DataField="BGPLAN" HeaderText="Plan" ReadOnly="True" 
  23.             Visible="False" />
  24.         <asp:BoundField DataField="BGAOPT" HeaderText="Option" ReadOnly="True" 
  25.             Visible="False" />
  26.         <asp:BoundField DataField="BAEXA" HeaderText="Plan Description" 
  27.             ReadOnly="True" />
  28.         <asp:BoundField DataField="BGEFT" HeaderText="Enrollment Date" 
  29.             ReadOnly="True" />
  30.         <asp:BoundField DataField="BAFDBA" HeaderText="BAFDBA" ReadOnly="True" 
  31.             Visible="False" />
  32.         <asp:BoundField DataField="BASDBA" HeaderText="BASDBA" ReadOnly="True" 
  33.             Visible="False" />
  34.         <asp:BoundField AccessibleHeaderText="RT" DataField="RT" 
  35.             DataFormatString="{0:N2}" HeaderText="RT" />
  36.         <asp:BoundField DataField="YTD" DataFormatString="{0:N2}" HeaderText="YTD" 
  37.             ReadOnly="True" />
  38.         <asp:HyperLinkField HeaderText="Website" DataNavigateUrlFields="Website" 
  39.             DataTextField="Website" Target="_blank" 
  40.            />
  41.         <asp:HyperLinkField 
  42.             HeaderText="Forms" Target="_blank" Text='<% Eval("Forms") %>' />
  43.         <asp:TemplateField HeaderText="EditFlg">
  44.             <EditItemTemplate>
  45.                 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Editflg") %>'></asp:TextBox>
  46.             </EditItemTemplate>
  47.             <ItemTemplate>
  48.                 <asp:Label ID="txtEditFlg" runat="server" Text='<%# Bind("Editflg") %>'></asp:Label>
  49.             </ItemTemplate>
  50.         </asp:TemplateField>
  51.     </Columns>
  52.     <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
  53.     <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
  54.     <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
  55.     <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
  56.     <EditRowStyle HorizontalAlign="Right" VerticalAlign="Middle" Wrap="False" />
  57.     <AlternatingRowStyle BackColor="#F7F7F7" />
  58. </asp:GridView>
Newbie
 
Join Date: Apr 2009
Posts: 10
#2: May 8 '09

re: Doesn't find grid control


Quote:

Originally Posted by kimbred View Post

Can someone help? I have the code below in which I'm attempting to show or hide the edit control in a gridview based on the value of a column in that view. It doesn't seem to find the control. I've included the RowDataBound event as well as the gridview definition. Thanks in advance.

Not sure if this is it, but you declared your variable of type TextBox when the control you're finding is a Label
Member
 
Join Date: Jan 2009
Posts: 47
#3: May 8 '09

re: Doesn't find grid control


That was it. Thanks. Not sure the Row Data Bound event is the correct place to put this though. When I select the edit link on the row, all rows magically get the edit link. Thanks again.
Newbie
 
Join Date: Apr 2009
Posts: 10
#4: May 8 '09

re: Doesn't find grid control


This is probably what you want (in the rowdatabound event)

Expand|Select|Wrap|Line Numbers
  1. If e.Row.RowType = DataControlRowType.DataRow Then
  2.     If e.Row.DataItem("Editflg") = "1" Then
  3.          e.Row.RowState = DataControlRowState.Edit
  4.     Else
  5.          e.Row.RowState = DataControlRowState.Normal
  6.     End If
  7. End If
  8.  
Using the c# equivalent ofcourse.
Reply