如何使用eval会话 [英] How to use session with eval

查看:115
本文介绍了如何使用eval会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用会话来传输eval值,而不是使用查询字符串。

如何传输Eval的值以及如何重新传输它在目标页面中?

这里是源代码和目标页面中的代码。

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: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: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>

推荐答案

Git摆脱锚点标记并使用LinkBut​​ton代替。或者您可以使用ImageButton,因为您正在显示图像。您可能还需要在HiddenField控件中绑定MainID和MainTopic字段。例如:



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:

<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:

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:

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





PS:请注意,Sessions可以为null,因此您可能需要在目标页面中绑定Repeater之前检查空值。



HTH!



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

HTH!


这篇关于如何使用eval会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆