Doesn't find grid control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kimbred
    New Member
    • Jan 2009
    • 47

    Doesn't find grid control

    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.
    Code:
    protected void gvEmployeesBenefits_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            TextBox EditFlg = (TextBox) e.Row.FindControl("txtEditflg");
            if (EditFlg.Text == "1")
            {
                LinkButton MyLinkButton = (LinkButton)e.Row.FindControl("lnkEdit");
                MyLinkButton.Visible = true;
            }
            else
            {
                LinkButton MyLinkButton = (LinkButton)e.Row.FindControl("lnkEdit");
                MyLinkButton.Visible = false;
            }
    
        }
    Code:
    <asp:GridView ID="gvEmployeesBenefits" runat="server" DataSourceID="Benefits" Style="z-index: 100;
        left: 0px; top: 0px" AutoGenerateColumns="False" BackColor="White" 
        BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
        GridLines="Horizontal" onrowdatabound="gvEmployeesBenefits_RowDataBound" 
        CellSpacing="3" HorizontalAlign="Justify">
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                        CommandName="Update" Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="BGAN8" HeaderText="Emp#" 
                ReadOnly="True" Visible="False" />
            <asp:BoundField DataField="BGPLAN" HeaderText="Plan" ReadOnly="True" 
                Visible="False" />
            <asp:BoundField DataField="BGAOPT" HeaderText="Option" ReadOnly="True" 
                Visible="False" />
            <asp:BoundField DataField="BAEXA" HeaderText="Plan Description" 
                ReadOnly="True" />
            <asp:BoundField DataField="BGEFT" HeaderText="Enrollment Date" 
                ReadOnly="True" />
            <asp:BoundField DataField="BAFDBA" HeaderText="BAFDBA" ReadOnly="True" 
                Visible="False" />
            <asp:BoundField DataField="BASDBA" HeaderText="BASDBA" ReadOnly="True" 
                Visible="False" />
            <asp:BoundField AccessibleHeaderText="RT" DataField="RT" 
                DataFormatString="{0:N2}" HeaderText="RT" />
            <asp:BoundField DataField="YTD" DataFormatString="{0:N2}" HeaderText="YTD" 
                ReadOnly="True" />
            <asp:HyperLinkField HeaderText="Website" DataNavigateUrlFields="Website" 
                DataTextField="Website" Target="_blank" 
               />
            <asp:HyperLinkField 
                HeaderText="Forms" Target="_blank" Text='<% Eval("Forms") %>' />
            <asp:TemplateField HeaderText="EditFlg">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Editflg") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="txtEditFlg" runat="server" Text='<%# Bind("Editflg") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <EditRowStyle HorizontalAlign="Right" VerticalAlign="Middle" Wrap="False" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:GridView>
    Last edited by Frinavale; May 7 '09, 06:30 PM. Reason: Added code tags. Please post code in [code] [/code] tags.
  • shiznit770
    New Member
    • Apr 2009
    • 10

    #2
    Originally posted by kimbred
    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

    Comment

    • kimbred
      New Member
      • Jan 2009
      • 47

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

      Comment

      • shiznit770
        New Member
        • Apr 2009
        • 10

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

        Code:
        If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.DataItem("Editflg") = "1" Then
                 e.Row.RowState = DataControlRowState.Edit
            Else
                 e.Row.RowState = DataControlRowState.Normal
            End If
        End If
        Using the c# equivalent ofcourse.

        Comment

        Working...