Search code examples
asp.net.netserver-side

Image by id is not accessible on server side ASP.NET


I have been trying to call a image on server side by making it runat="server" and giving it an id. But for some reason I cannot call it on the server side.

Here is the Code:

<div class="row">
     <div class="col-lg-12" style="padding-left: 50px;">
          <img id="portalimage" src="Image/steam_logo.png" style="width: 200px; height: 160px;" runat="server" />
     </div>
</div>

and here is what I am trying to do at the server side:

private void CheckForPortal()
{
    int.TryParse(Request.QueryString["ID"], out _gameID);

    string gamePortal = ctr.GetPortal(_gameID);

    if (gamePortal == "Steam")
    {
        //here i want to call the image id
    }
}

and so on.. Basically what I am trying to is to access the image by its id so that I can change the image on the server side. But I cannot find the portalimage id on server side for some reason.

Don't know if this is relevant. But i am using a masterpage

Here is the master page code.

<body runat="server">
<form id="form1" runat="server">

        <div style="height: 928px; min-height: 400px; width: 1903px; background-color: #ffffff; margin-top: 0px;">
            <div class="navbar navbar-inverse navbar-fixed-top" style="height: 67px; width: 1920px;">
                <div class="container" style="width: 1903px; padding-left: 322px;">
                    <a class="navbar-brand" style="margin-top: -15px;" href="Home.aspx">
                        <img alt="Brand" src="Image/AFGIcon.png" />
                    </a>
                    <div class="container" style="height: 67px; width: 200px; padding-top: 15px; float: left;">
                        <asp:TextBox ID="txtSearch" CssClass="form-control" placeholder="Find a game" Width="196" runat="server"></asp:TextBox>
                    </div>
                    <div class="container" style="height: 67px; width: 48px; padding-top: 15px; float: left;">
                        <asp:Button ID="btnSearch" CssClass="btn btn-success" runat="server" Text="Go" />
                    </div>

                    <ul class="nav navbar-nav navbar-right" style="margin-right: 950px; margin-top: 10px;">
                        <li><a href="AllGames.aspx">Games</a></li>
                        <li><a href="AboutUs.aspx">About us</a></li>
                        <li><a href="Contact.aspx">Contact</a></li>
                    </ul>

                </div>
            </div>

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>

</form>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="/Scripts/JavaScript.js"></script>
<script src="Scripts/videoScript.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.2/fotorama.js"></script>


Solution

  • okay so i have made i work now. It was because the control was nested in a repeater item so this is what i came up with.

    private void CheckForPortal()
    {
        int.TryParse(Request.QueryString["ID"], out _gameID);
    
        string gamePortal = ctr.GetPortal(_gameID);
    
        foreach (RepeaterItem item in repGameInfo.Items)
        {
    
            Image img = (Image)item.FindControl("portalimage");
    
            if (gamePortal == "Steam")
            {
                img.Attributes["src"] = ResolveUrl("http://localhost:16899/Image/steam_logo.png");
            }
            else if (gamePortal == "Origin")
            {
                img.Attributes["src"] = ResolveUrl("~/Image/origin_logo.png");
            }
            else if (gamePortal == "Uplay")
            {
                img.Attributes["src"] = ResolveUrl("~/Image/UPLAY_logo_-_Small.png");
            }
            else if (gamePortal == "Battle.net")
            {
                img.Attributes["src"] = ResolveUrl("~/Image/battleNet-logo.png");
            }
        }        
    }
    

    i had to make a loop and chek for controls in the repeater item and then i mage a variable of a type image and then i can find the id. Thanks for the help guys.