gpt4 book ai didi

c# - 如果 Eval 不为空,如何在 GridView 中有选择地显示按钮?

转载 作者:行者123 更新时间:2023-11-29 20:20:35 25 4
gpt4 key购买 nike

我有以下 GridView 和 ObjectDataSource:

<asp:GridView 
ID="grdTrades"
runat="server"

DataSourceID="srcTrades"
DataKeyNames="tradeId"
EnablePersistedSelection="true"
SelectedRowStyle-BackColor="Yellow"
AllowPaging="true"
AllowSorting="true"
PageSize = "10"
AutoGenerateColumns="false"
>
<Columns>
<asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" />
<asp:BoundField DataField="tradeId" HeaderText="TradeId" ReadOnly="True" SortExpression="tradeId" />
<asp:BoundField DataField="symbol" HeaderText="Pair" SortExpression="symbol" />
<asp:BoundField DataField="chartTimeFrame" HeaderText="TF" SortExpression="chartTimeFrame" />
<asp:BoundField DataField="tradeSetupId" HeaderText="Setup" SortExpression="tradeSetupId" />
<asp:BoundField DataField="tradeType" HeaderText="Trade Type" SortExpression="tradeType" />
<asp:BoundField DataField="side" HeaderText="Side" ReadOnly="True" SortExpression="side" />
<asp:BoundField DataField="units" HeaderText="Units" ReadOnly="True" SortExpression="units" />
<asp:BoundField DataField="price" HeaderText="Price" SortExpression="price" />
<asp:BoundField DataField="orderDateTime" HeaderText="Order Date/Time" SortExpression="orderDateTime" />
<asp:BoundField DataField="pipsProfit" HeaderText="Pips profit" DataFormatString="{0:F1}" SortExpression="pipsProfit" />
<asp:BoundField DataField="riskPips" HeaderText="Pips risked" DataFormatString="{0:F1}" SortExpression="riskPips" />
<asp:BoundField DataField="pipsInMove" HeaderText="Pips in move" DataFormatString="{0:F1}" SortExpression="pipsInMove" />
<asp:BoundField DataField="rewardRiskRatio" HeaderText="R/R" DataFormatString="{0:F1}" SortExpression="rewardRiskRatio" />
<asp:BoundField DataField="pctAccountRisked" HeaderText="% risk" DataFormatString="{0:F1}" ReadOnly="True" SortExpression="pctAccountRisked" />
<asp:BoundField DataField="pctAccountChange" HeaderText="% AcctChange" DataFormatString="{0:F2}" SortExpression="pctAccountChange" />
<asp:TemplateField>
<ItemTemplate>
<input type="button" size="x-small" value="View" onclick="javascript:ShowImageInNewPage('DisplayImage.aspx?screenshotId=<%# Eval("screenshotId") %>');" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField
Text="Edit"
DataNavigateUrlFields="tradeId"
DataNavigateUrlFormatString="EditTrade.aspx?screenshotId={0}" />
<CustomControls:DeleteButtonField ConfirmText="Delete this trade?" Text="Del" />
</Columns>
</asp:GridView>

<CustomControls:CustomObjectDataSource
id="srcTrades"
TypeName="DatabaseComponent.DBUtil"
SelectMethod="GetTrades"
DeleteMethod="DeleteTrade"
runat="server">
<DeleteParameters>
<asp:ControlParameter Name="tradeId" ControlId="grdTrades" PropertyName="SelectedValue" />
</DeleteParameters>
</CustomControls:CustomObjectDataSource>

在我的 TemplateField 中,如果 screenshotId 的计算结果为非空非零值,我只想显示按钮。如果 screenshotId 是 DbNull 或 0,那么我想将该单元格留空。我应该在哪里编写代码,在 ASP.NET 代码中,还是必须编写一些代码隐藏?什么是最好的方法?

更新

我添加了以下代码:

protected void grdTrades_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataitem = DataBinder.Eval(e.Row.DataItem, "screenshotId");
Button btnShowImage = e.Row.FindControl("btnShowImage") as Button;
if (dataItem.screenshotId != null && screenshotId > 0)
{
btnShowImage.OnClientClick = "javascript:ShowImageInNewPage(\"DisplayImage.aspx?screenshotId=" + dataItem.screenshotId + "\");";
btnShowImage.Visible = true;
}
else
btnShowImage.Visible = false;
}
}

但是我得到了这些错误:

The name 'dataItem' does not exist in the current context

The name 'screenshotId' does not exist in the current context

MyRow 也没有被识别。有什么想法吗?

更新 2

我终于解决了 - 这是我的代码:

protected void grdTrades_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// If you do it like this, you already get the screenshotId value;
var screenshotId = DataBinder.Eval(e.Row.DataItem, "screenshotId");
Response.Write("ScreenshotId: " + screenshotId.ToString());

Button btnShowImage = e.Row.FindControl("btnShowImage") as Button;

if (screenshotId is System.DBNull)
btnShowImage.Visible = false;
else
{
btnShowImage.Text = screenshotId.ToString();
btnShowImage.OnClientClick =
"javascript:ShowImageInNewPage(\"DisplayImage.aspx?screenshotId=" + screenshotId.ToString() + "\");";
btnShowImage.Visible = true;
}
}
}

最佳答案

你能以声明的方式完成这一切吗?

    <asp:TemplateField>
<ItemTemplate>
<input runat="server" visible='<%# Eval("screenshotId") != DBNull.Value %>' type="button" size="x-small" value="View" onclick='<%# "ShowImageInNewPage(\"DisplayImage.aspx?screenshotId=" + Eval("screenshotId") + "\")" %>' />
</ItemTemplate>
</asp:TemplateField>

关于c# - 如果 Eval 不为空,如何在 GridView 中有选择地显示按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4132471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com