Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:23:42+00:00 2026-05-18T04:23:42+00:00

I have the following GridView and ObjectDataSource: <asp:GridView ID=grdTrades runat=server DataSourceID=srcTrades DataKeyNames=tradeId EnablePersistedSelection=true SelectedRowStyle-BackColor=Yellow

  • 0

I have the following GridView and 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>  

In my TemplateField I only want to display the button if screenshotId evaluates to a non-null non-zero value. If screenshotId is DbNull or 0 then I want to leave the cell blank. Where should I code this, in the ASP.NET code or do I have to write some code-behind? What’s the best way?

UPDATE

I’ve added the following code:

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

However I get these errors:

The name ‘dataItem’ does not exist in
the current context

and

The name ‘screenshotId’ does not exist
in the current context

MyRow was not recogised either. Any ideas?

UPDATE 2

I finally worked it out – here’s my code:

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;
        }            
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:23:43+00:00Added an answer on May 18, 2026 at 4:23 am

    the best way is to handle OnRowDataBound event.

    <ItemTemplate>
       <asp:Button runat="server" ID="btnShowImage" />                
    </ItemTemplate>
    

    Then in your code behind:

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

    Something like that, but you’ll have to improve it according to your needs. Don’t forget to add the event handler to your GridView markup as well.

    <asp:GridView OnRowDataBound="GridView1_RowDataBound".... />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The .aspx: <asp:GridView ID=gvFirst runat=server AutoGenerateColumns=false AllowPaging=true ondatabound=gvFirst_DataBound > <Columns> <asp:BoundField DataField=ID HeaderText=ProductID/> <asp:BoundField
I have the following code: <ListView Name=lvwYears Margin=0,32,191,29 ItemsSource={Binding Path=YearCollection}> <ListView.View> <GridView> <GridViewColumn> <GridViewColumnHeader>
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following text in a file 23456789 When I tried to replace the
I have following Code Block Which I tried to optimize in the Optimized section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.