none
display data from codebehind in aspx page RRS feed

  • Question

  • Hi really basic question here.

    Ive got a code behind file, and Ive managed to to do some calculations and store the calculations into variable fields. I now need to work out how to display those variable fields from my .cs file into my .aspx file. Can someone help me achieve this basic task please.

    Codebehind
    
    //I've cut out the loops and calculations, however I know these variables contain data as I have used the Response.Write command to out the values.
    
    
    displayTitle = contentItem.Title;
    
    displayTeaser = contentItem.Teaser;
    
    displayImage = srcPath;
          
    number = number + 1;
    This is the code I have for the front end aspx page. I'm trying to put it in a ListView as the codebehind, the variables are looped.
    <asp:ListView ID="ListView1" runat="server" >
     <LayoutTemplate>
    
      <div id="menu">
          <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
      </div>
     
     </LayoutTemplate>
     <ItemTemplate>
       <div id="Image"><% %></div>
       <div id="Title"><% %></div>
       <div id="Teaser"><% %></div>   
     </ItemTemplate>
    </asp:ListView>



    • Moved by Lie You Monday, July 11, 2011 9:02 AM asp.net (From:Visual C# Language)
    Wednesday, June 29, 2011 10:28 PM

All replies

  • hope this one is a sol for you

     

    http://forums.asp.net/t/1250540.aspx


    Mark Answered, if it solves your question
    Rohit Arora
    Thursday, June 30, 2011 5:43 AM
  • Hi,

    You can put a server side placeholder inside your all three DIV or else you may want to put lables and image controls as required. And then you can display its value by changing it from your code behind.


    Krunal Doshi
    Thursday, June 30, 2011 10:41 AM
  • Thanks, I've changed the aspx template so it now has

     <ItemTemplate>
       <div id="Image"><%=displayTitle%></div>
       <div id="Title"><%=displayTeaser%></div>
       <div id="Teaser"><%=displayImage %></div>   
     </ItemTemplate>


    but doesnt work. I think thats what Rohit link was taking me to.

    Im trying to work out how to do what Kunal has suggested..... might see if I could bind it, or mybe I should use stringbuilder and build my code in the code behind and the response.write out to the front?

    Thursday, June 30, 2011 11:57 AM
  • Hi,

    Try below code

    <ItemTemplate>
      <div id="Image"><asp:Image ID="img1" runat="server" /></div>
      <div id="Title"><asp:Label ID="lblTitle" runat="server" Text=""></asp:Label></div>
      <div id="Teaser"><asp:Label ID="lblTeaser" runat="server" Text=""></asp:Label></div>  
    </ItemTemplate>
    

    and, then in your code behind

    img1.ImageUrl = displayImage;
    lblTitle.Text = displayTitle;
    lblTeaser.Text = displayTeaser;
    

     


    Krunal Doshi
    Thursday, June 30, 2011 1:21 PM
  • Hi Krunal,

    Thankyou. Ive changed the code to what you have described above, but I get an error about the img1 etc not appearing in the current context. I have therefore added img1,lblTitle,lblTeaser as string, but thats causing another error? I think I just need to define these variables ok and it will work.

    Ive been reading up more on the subject. I think it maybe because may variable is in the protected area? How do I change this 

    protected

     

    void Page_Load(object sender, EventArgs e)

    so that the variable works with the front end aspx page?

    Thursday, June 30, 2011 1:39 PM
  • Hi,

    Sorry to hear, that code didn't worked for you. Can you try declaring the hidden control and assign the value from your code behind? In that case your earlier code should work I think.

    <input id ="ctl1" type="hidden" runat="server" />
    
    OR
    
    <asp:HiddenField ID="ctl1" runat="server" />
    

     


    Krunal Doshi
    Thursday, June 30, 2011 3:14 PM
  • Here are few things that I want to share.

    1) Whenever you are trying to update something from the code-behind, after having a page displayed to the client - you need to make sure that you have the concept of PostBack, and that it updates the contents. (Please check through this concept, in case you dont know about it)

    2) In case you want your client side scripts (such as javascript), to actually get a value from the code behind, then you need to make your javascript to reference the C# function from the source code page. ( I think its <%=MethodName %> or <%= MethodName() %>

    3) Since you said, you are performing calculations, you can directly using javascript (which could be written in the source code), to actually perform the calculations at the client end instead of server end - if and only if-  the calculations are based on the client side data presented to the client.


    4) Use ASP controls instead of plain HTML controls in the source code to directly communicate with the code behind (aspx + code behind is actually compiled as one class - as they are partial classes). But ultimately, you will need some sort of postback control such as a button to update the UI.
    Thursday, June 30, 2011 8:25 PM
  • Thankyou all for your contributions. Nabil you are vrey knowledgeable and I really appreciate you chiming in. I will do a little bit more fact finding on some of things you have suggested namely 1 and 4. I've found some code which does something similar to what I am trying to acheive using literals as Krunal has suggested, I think that will hopefully help me understand how to do what I want to do.

     

    Friday, July 1, 2011 1:52 PM
  • Hello AJK11,

    Have you found a good solution? If so, could share it here which will be very beneficial for other community members who have similar questions.

    If not, you can consider posting it at the following more appropriate forums for more efficient responses, where experts live in.

                    Web Forms Data Controls : The Official Microsoft ASP.NET Forums

    Thanks for your understanding and support.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 5, 2011 3:11 AM
  • thank you for the kind words AJK, but I got a very longgg way to go :)

    Friday, July 8, 2011 9:15 PM