MadDev
MadDev

Reputation: 1150

Hide a LinkButton inside an UpdatePanel, which is inside a GridView

Is it possible to hide a LinkButton inside an UpdatePanel, which is inside a GridView? or am I going about this the complete wrong way?

I want to disable the lnkDownload button when ExpenseReceipt is null in the database and display the text "No Receipt".

when I debug, lnkDownload comes back as null.

ASP.NET:

<asp:GridView ID="gvTillExpenseRegistration" runat="server" AutoGenerateColumns="False"
    EmptyDataText="No expense registered today." GridLines="Horizontal" SkinID="SimpleBlackWhite"
    CellPadding="10"  Caption="Today's Expense Registration" OnRowCommand="gvTillExpenseRegistration_RowCommand" DataKeyNames="ExpenseID, FileName">
    <Columns>
        <asp:BoundField DataField="ExpenseID" HeaderText="ExpenseID" Visible="False"/>
        <asp:BoundField DataField="Description" HeaderText="Type" />
        <asp:BoundField DataField="TotalAmount" HeaderText="Amount" SortExpression="TotalAmount"
            DataFormatString="{0:0.00}">
            <ItemStyle HorizontalAlign="Right" />
        </asp:BoundField>
        <asp:BoundField DataField="RegisterDate" HeaderText="Time" DataFormatString="{0:hh:mm tt}">
        </asp:BoundField>
        <asp:BoundField DataField="RegisteredBy" HeaderText="User"></asp:BoundField>
        <asp:BoundField DataField="FileName" HeaderText="FileName" Visible="False"/>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:UpdatePanel ID="updDownload" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandName="Download" Text='Download' />
                    </ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger ControlID="lnkDownload" />
                    </Triggers>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C#:

private void LoadTodaysExpensesByTill(int tillID)
{
    DataTable dt = new DataTable();
    dt = new TillEndOfDayDAL().GetTodaysExpensesByTillID(tillID);
    pnlTillExpenseRegistration.Visible = false;
    if (dt != null && dt.Rows.Count > 0)
    {
        gvTillExpenseRegistration.DataSource = dt;

        foreach (DataRow row in dt.Rows)
        {
            if (row["ExpenseReceipt"] == DBNull.Value)
            {
                LinkButton lnkDownload = (LinkButton)gvTillExpenseRegistration.FindControl("lnkDownload");
                lnkDownload.Enabled = false;
                lnkDownload.Text = "No Receipt";
            }
        }
        pnlTillExpenseRegistration.Visible = true;
    }
    gvTillExpenseRegistration.DataBind();
}

Upvotes: 0

Views: 302

Answers (3)

Per G
Per G

Reputation: 375

Try adding an event on gvTillExpenseRegistration

 <asp:GridView ID="gvTillExpenseRegistration" runat="server" 
   OnRowDataBound="gvTillExpenseRegistration_DataBound"

..

  protected void gvTillExpenseRegistration_DataBound(object sender, EventArgs e)
    {

    }

Upvotes: 0

Kevin Shah
Kevin Shah

Reputation: 1617

You need to change your code as per below

aspx

<asp:GridView ID="gvTillExpenseRegistration" runat="server" AutoGenerateColumns="False"
            EmptyDataText="No expense registered today." GridLines="Horizontal" SkinID="SimpleBlackWhite"
            CellPadding="10" Caption="Today's Expense Registration" OnRowCommand="gvTillExpenseRegistration_RowCommand" OnRowDataBound="GrdView_RowDataBound" DataKeyNames="ExpenseID, FileName">
            <Columns>
                <asp:BoundField DataField="ExpenseID" HeaderText="ExpenseID" Visible="False" />
                <asp:BoundField DataField="Description" HeaderText="Type" />
                <asp:BoundField DataField="TotalAmount" HeaderText="Amount" SortExpression="TotalAmount"
                    DataFormatString="{0:0.00}">
                    <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
                <asp:BoundField DataField="RegisterDate" HeaderText="Time" DataFormatString="{0:hh:mm tt}"></asp:BoundField>
                <asp:BoundField DataField="RegisteredBy" HeaderText="User"></asp:BoundField>
                <asp:BoundField DataField="FileName" HeaderText="FileName" Visible="False" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField runat="server" ID="hdnExpenseReceipt" Value='<%# Eval("ExpenseReceipt") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:UpdatePanel ID="updDownload" runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandName="Download" Text='Download' />
                            </ContentTemplate>
                            <Triggers>
                                <asp:PostBackTrigger ControlID="lnkDownload" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

C#

private void LoadTodaysExpensesByTill(int tillID)
{
    DataTable dt = new DataTable();
    dt = new TillEndOfDayDAL().GetTodaysExpensesByTillID(tillID);
    pnlTillExpenseRegistration.Visible = false;
    if (dt != null && dt.Rows.Count > 0)
    {
        gvTillExpenseRegistration.DataSource = dt;        
        pnlTillExpenseRegistration.Visible = true;
    }
    gvTillExpenseRegistration.DataBind();
}

and also created one method

    protected void GrdView_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                HiddenField hdnExpenseReceipt = (HiddenField)e.Row.FindControl("hdnExpenseReceipt");
                if (string.IsNullOrWhiteSpace(hdnExpenseReceipt.Value))
                {
                    LinkButton lnkDownload = (LinkButton)gvTillExpenseRegistration.FindControl("lnkDownload");
lnkDownload.Visible = false;
                }
            }

This method will call each time when row created in gridview.

try and let me know.

Upvotes: 0

VDWWD
VDWWD

Reputation: 35514

You can set the Text and Enabled properties of the LinkButton inline with a ternary operator.

<asp:LinkButton ID="lnkDownload" runat="server"
   Text='<%# string.IsNullOrEmpty(Eval("ExpenseReceipt").ToString()) ? "No Receipt" : "Download" %>'
   Enabled='<%# string.IsNullOrEmpty(Eval("ExpenseReceipt").ToString()) ? false : true %>' />

And it would be better to wrap the GridView in the UpdatePanel, not UpdatePanel inside ItemTemplates. This could give unexpected behavior.

However in your case you could remove it completely since you are adding an UpdatePanel, and then set a PostBackTrigger, making the panel useless anyway.

Upvotes: 1

Related Questions