[英]How to configure the index of a datalist in asp.net ?
I develop a web application in visual studio that display rooms pictures and their names from the database in one page and a rate button beside each room all in a datalist 我在Visual Studio中开发了一个Web应用程序,该应用程序可以在一页中显示数据库中的房间图片及其名称,并在数据列表中每个房间旁边显示一个速率按钮。
Then when the user click the rate button the room picture and its name should be transfered to the rate page but , 然后,当用户单击“费率”按钮时,房间图片及其名称应被转移到费率页面,但是,
What happens with me is that if I click any button only the first room picture and name displayed in the rate page :'( ! 对我来说,发生的事情是,如果我单击任何按钮,则仅显示房价页面上显示的第一张房间图片和名称:
I think it corresponds with the index of the datalist , but I don't know how to deal with it !! 我认为它与数据列表的索引相对应,但是我不知道如何处理它!
What should I do to fix it ? 我该怎么解决?
Here is the code 这是代码
webform1.aspex webform1.aspex
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="HotelG.WebForm1" EnableEventValidation="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" Width="615px" Height="439px" >
<ItemTemplate>
<table>
<tr>
<td><asp:Image ID="Img1" runat="server" ImageUrl=<%# Eval("Picture")%> Height="100" style="position: relative; top: 0px; left: 98px; width: 100" /> </td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<asp:Label ID="Label1" runat="server" Text=<%# Eval("Room_Type")%>></asp:Label>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><asp:Button ID="btn1" runat="server" Text="Rate" OnClick="Button1_Click" /></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
</div>
</form>
</body>
</html>
webform1 code-behind file webform1代码隐藏文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace HotelG
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\user\Desktop\database\Golden_Rose.mdf;Integrated Security = True; Connect Timeout = 30");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
string sel = "select Room_Type , Picture from room_details";
SqlCommand cmd = new SqlCommand(sel, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem li in DataList1.Items)
{
Image img = (Image)li.FindControl("Img1");
Label lbl = (Label)li.FindControl("Label1");
string labeltext = lbl.Text;
string url = img.ImageUrl;
Session["type"] = labeltext;
Session["img"] = url;
Response.Redirect("WebForm2.aspx");
}
}
}
}
webform2 code-behind file webform2代码隐藏文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HotelG
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["type"] != null)
{
Label1.Text = Session["type"].ToString();
Label5.Text = Session["type"].ToString();
}
if (Session["img"] != null)
{
Image1.ImageUrl = Session["img"].ToString();
Label4.Text = Session["img"].ToString();
}
}
}
}
You are redirecting in a foreach
, you know that Response.Redirect
redirects a client to a new URL immediately and throws a ThreadAbortException
upon completion? 您正在
foreach
进行重定向,您知道Response.Redirect
立即将客户端重定向到新的URL,并在完成时引发ThreadAbortException
吗?
Instead you only need to use FindControl
on the current item instead of all(actually only the first because the foreach
is not fully enumerated), it's the button's NamingContainer
: 取而代之的是,您只需要在当前项目上使用
FindControl
而不是全部(实际上仅是第一个,因为foreach
未被完全枚举),它是按钮的NamingContainer
:
protected void Button1_Click(object sender, EventArgs e)
{
DataListItem currentItem = (DataListItem)((Button) sender).NamingContainer;
Image img = (Image)currentItem.FindControl("Img1");
Label lbl = (Label)currentItem.FindControl("Label1");
string labeltext = lbl.Text;
string url = img.ImageUrl;
Session["type"] = labeltext;
Session["img"] = url;
Response.Redirect("WebForm2.aspx");
}
As mentioned by Rahul in the comment section you should also not DataBind
the DataList
on every post back but only on the initial one. 正如Rahul在评论部分中提到的那样,您也不应该在每
DataList
发帖中都对DataList
进行DataBind
,而应仅对最初的发贴。 Therefore use IsPostBack
to check it: 因此,请使用
IsPostBack
进行检查:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
con.Open();
string sel = "select Room_Type , Picture from room_details";
SqlCommand cmd = new SqlCommand(sel, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
con.Close();
}
}
Otherwise all changes are lost and events aren't triggered. 否则,所有更改都将丢失,并且不会触发事件。 That applies only if
ViewState
is enabled which is default. 仅在启用
ViewState
(默认设置)的情况下适用。 It's also good practise to use the using
-statement and a local variable for the connection instead of a field. 对连接使用
using
-statement和局部变量(而不是字段)也是一种好习惯。 That ensures that it gets closed even on error. 这样可以确保即使发生错误也可以将其关闭。
Why don't you try using DataList_ItemCommand
event to Redirect to another page for the appropriate item that is clicked. 您为什么不尝试使用
DataList_ItemCommand
事件重定向到另一个页面,以单击相应的项目。
Here is what I propose you should try : 我建议您尝试以下方法:
Command Argument
property using a LinkButton
instead of Button
. LinkButton
而不是Button
将每个房间的PrimaryKey Id存储到Command Argument
属性中。 On the code behind, create the DataList_ItemCommand
event and fetch the Command Argument of the item that is clicked like this : 在后面的代码上,创建
DataList_ItemCommand
事件并获取被单击的项目的Command Argument,如下所示:
var id = e.CommandArgument;
Use Response.Redirect
in that same function and pass this ID as a query string parameter or in Session like you are doing now and fetch that on the Redirecting page. 在同一函数中使用
Response.Redirect
,然后像现在一样在会话中将此ID作为查询字符串参数传递,并在“重定向”页面上获取该ID。
You can also take some reference for the DataList_ItemCommand
event from this link : Pass Data to other Page using DataList and Querystring 您也可以从此链接获取有关
DataList_ItemCommand
事件的一些参考: 使用DataList和Querystring将数据传递到其他页面
Hope this helps. 希望这可以帮助。
Put in CommandName and CommandArgument properties of Button Control. 放入按钮控件的CommandName和CommandArgument属性。 Put primary key of record in CommandArgument and instead of using button's click event.
将记录的主键放在CommandArgument中,而不使用按钮的click事件。 Use button's Command event.
使用按钮的Command事件。 In that event you will get an argument of type CommandEventArgs.
在这种情况下,您将获得CommandEventArgs类型的参数。 And in this event you can get the primary key of the record being rated by using CommandEventArgs arguments "CommandArgument" property and then you can do whatever you want with this particular record.
并且在这种情况下,您可以使用CommandEventArgs参数“ CommandArgument”属性来获取记录的主键,然后您可以对该特定记录执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.