MadDev MadDev - 28 days ago 6
ASP.NET (C#) Question

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

button when
is null in the database and display the text "No Receipt".

when I debug,
comes back as null.


<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">
<asp:BoundField DataField="ExpenseID" HeaderText="ExpenseID" Visible="False"/>
<asp:BoundField DataField="Description" HeaderText="Type" />
<asp:BoundField DataField="TotalAmount" HeaderText="Amount" SortExpression="TotalAmount"
<ItemStyle HorizontalAlign="Right" />
<asp:BoundField DataField="RegisterDate" HeaderText="Time" DataFormatString="{0:hh:mm tt}">
<asp:BoundField DataField="RegisteredBy" HeaderText="User"></asp:BoundField>
<asp:BoundField DataField="FileName" HeaderText="FileName" Visible="False"/>
<asp:UpdatePanel ID="updDownload" runat="server" UpdateMode="Conditional">
<asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandName="Download" Text='Download' />
<asp:PostBackTrigger ControlID="lnkDownload" />


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;

Answer Source

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.