是否可以在UpdatePanel中隐藏LinkButton,它位于GridView内部?或者我是以完全错误的方式解决这个问题的?
我希望在数据库中lnkDownload
为空时禁用ExpenseReceipt
按钮,并显示文本" No Receipt"。
当我调试时,lnkDownload
返回为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();
}
答案 0 :(得分:1)
您可以使用三元运算符设置LinkButton的Text和Enabled属性。
<asp:LinkButton ID="lnkDownload" runat="server"
Text='<%# string.IsNullOrEmpty(Eval("ExpenseReceipt").ToString()) ? "No Receipt" : "Download" %>'
Enabled='<%# string.IsNullOrEmpty(Eval("ExpenseReceipt").ToString()) ? false : true %>' />
最好将GridView包装在UpdatePanel中,而不是在ItemTemplates中包装UpdatePanel。这可能会产生意想不到的行为。
但是在你的情况下你可以完全删除它,因为你要添加一个UpdatePanel,然后设置一个PostBackTrigger,使得面板无论如何都没用。
答案 1 :(得分:0)
尝试在gvTillExpenseRegistration上添加一个事件
<asp:GridView ID="gvTillExpenseRegistration" runat="server"
OnRowDataBound="gvTillExpenseRegistration_DataBound"
...
protected void gvTillExpenseRegistration_DataBound(object sender, EventArgs e)
{
}
答案 2 :(得分:0)
您需要按照以下更改代码
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();
}
并创建了一个方法
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;
}
}
每次在gridview中创建行时,此方法都会调用。
试着让我知道。