Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi ,
i'm trying to use session to transfer eval value , instead of using query string .
how can i transfer value of Eval and how to recive it in destination page ?
here my code in source and destination page .
ASP.NET
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
               <ItemTemplate>
                   <li>
                       <a href='<%# "../mid_topics/mid_topics.aspx?MainID="+Eval("MainID")+"&MainTopic="+Eval("MainTopic") %>'>
                           <img src='<%# "../AppImages/Main/" + Eval("MainID")  + ".jpg" %>' style="width: 210px; height: 170px;" alt="" /></a>
                       <b>
                           <asp:Label ID="Label2" Text='<%# Eval("MainTopic") %>' runat="server" Visible="True"> </asp:Label>
                       </b>
                       <asp:Label ID="Label1" runat="server" Text= '<%# Eval("MainID") %>'  ></asp:Label>
                   </li>
               </ItemTemplate>
           </asp:Repeater>

//in destination page
ASP.NET
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
              <HeaderTemplate>
                                 </HeaderTemplate>
              <ItemTemplate>
                  <li><a href="index.html">
                      <img src='<%# "../AppImages/Mid/"+ Request.QueryString["MainID"].ToString()+"/" + Eval("MidID")  + ".jpg" %>' width="96" height="96" alt="spa" title="spa" /></a>
                      <h2><a href="index.html">
                          <asp:Label ID="Label2" Text='<%# Eval("MidTopic") %>' runat="server"> </asp:Label>
                      </a></h2>
                  </li>
              </ItemTemplate>
             

          </asp:Repeater>
Posted
Updated 27-Jul-16 20:53pm
v2
Comments
ZurdoDev 27-Jul-16 9:32am    
Somewhere you need to do Session["someKey"] = someValue;

Then you get it back by doing someVariable = Session["someKey"]

1 solution

Git rid of the anchor tag and use a LinkButton instead. Or you may use ImageButton since you are displaying images. You may also need to bind your MainID and MainTopic fields in a HiddenField controls. For example:

HTML
<ItemTemplate>
        <li>
         <asp:ImageButton runat="server" ID="ImageButton1" ImageUrl='<%# "~/AppImages/Main/" + Eval("MainID")  + ".jpg" %> ‘ OnClick="ImageButton1_Click"
 />
      <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "MainID") %>' />
      <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "MainTopic") %>' />
        </li>
</ItemTemplate>


Then on your code behind file, you could set the value to a Session like:

C#
protected void ImageButton1_Click(object sender, ImageClickEventArgs e){
        ImageButton b = (ImageButton)sender;
        RepeaterItem item = (RepeaterItem)b.NamingContainer;
        //find the Hidden values
        HiddenField mainID = (HiddenField)item.FindControl("HiddenField1");
        HiddenField mainTopic = (HiddenField)item.FindControl("HiddenField2");
        Session["MainID"] = mainTopic.Value;

        //redirect to other page
        Response.Redirect("~/mid_topics/mid_topics.aspx");
}


On your destination page, you could try something like this:

HTML
<a href="index.html">
                      <img src='<%# "../AppImages/Mid/"+ Session["MainID"].ToString() +"/" + Eval("MidID")  + ".jpg" %>' width="96" height="96" alt="spa" title="spa" /></a>


PS: Beware that Sessions can be null, so you may want to check for nulls before binding your Repeater in the destination page.

HTH!
 
Share this answer
 
v4
Comments
alaa deego 28-Jul-16 3:00am    
Vincent Maverick Durano
could you pealse look again in the code
Vincent Maverick Durano 28-Jul-16 7:26am    
I've updated the solution. Please check. By the way, did you down voted my solution?
ZurdoDev 28-Jul-16 7:44am    
The OP has this inside a repeater. It is highly unlikely, but possible, that they want to show the same exact image over and over for every single record. I seriously doubt this is what the OP needs.
Vincent Maverick Durano 28-Jul-16 8:00am    
The sample solution provided is inside a Repeater. If you noticed it's inside an ItemTemplate.

The ImageButton will render rows of images based on the source. And since he wanted to use Session, the only way is to set it is at code behind, and have the redirect on the server-side too.

I'm guessing you're the one down voting the solution. jeez
ZurdoDev 28-Jul-16 8:15am    
"If you noticed it's inside an ItemTemplate." - That is exactly my point. Because the code is inside the ItemTemplate, as I said, the code will "show the same exact image over and over for every single record." Do you think the OP wants the same exact image for every single record? And if OP did, why not hardcode it or include it in the sql instead of the Session?

"I'm guessing you're the one down voting the solution." - No, I didn't because this is what the OP asked for. My point is, I don't think the OP actually wants what they asked for.

And don't worry about people downvoting. It's the internet, it is going to happen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900