我有以下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;
}
}
但是我收到了这些错误:
名称'dataItem'不存在于 当前的背景
和
名称'screenshotId'不存在 在当前的背景下
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;
}
}
}
答案 0 :(得分:2)
你能以声明方式完成所有这些吗?
<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>
答案 1 :(得分:1)
最好的方法是处理OnRowDataBound事件。
<ItemTemplate>
<asp:Button runat="server" ID="btnShowImage" />
</ItemTemplate>
然后在你的代码背后:
protected void GridView1_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");
Button btnShowImage = e.Row.FindControl("btnShowImage") as Button;
if(screenshotId != null && screenshotId > 0)
{
btnShowImage.OnClientClick =
"javascript:ShowImageInNewPage(\"DisplayImage.aspx?screenshotId=" + screenshotId.ToString() + "\");";
btnShowImage.Visible = true;
}
else
btnShowImage.Visible = false;
}
}
类似的东西,但你必须根据你的需要改进它。不要忘记将事件处理程序添加到GridView标记中。
<asp:GridView OnRowDataBound="GridView1_RowDataBound".... />