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

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();
}

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.