asp.net怎么从DataList中取一个值?
<asp:DataListID="DataList1"runat="server"RepeatColumns="15"RepeatDirection="Horizonta...
<asp:DataList ID="DataList1" runat="server" RepeatColumns="15" RepeatDirection="Horizontal"
Width="666px">
<ItemTemplate>
<table>
<tr>
<td style="width: 100px">
<asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Eval("State") %>'
ImageUrl='<%# Eval("State", "~/images/RoomState/{0}.jpg") %>' OnClick="ImageButton1_Click" /></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Number") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
下面是我处理的代码:
/// <summary>
/// 点击图片的触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//获得点击客房的状态
//ImageButton imag = DataList1.SelectedItem.FindControl("ImageButton1") as ImageButton;
//string aa = e.GetType().ToString();
//string state = imag.CommandArgument;
//Response.Write(aa);
ImageButton img = (ImageButton)DataList1.SelectedItem.FindControl("ImageButton1");
string state = img.CommandArgument;
//装在Session里面
Session["state"] = state;
if (Session["state"].ToString() != "empty")
{
Response.Write("<script>alert(\"对不起,已经有人了~!\")</script>");
}
}
在 imageButton 处 报了一个为将对象设置引用到实例的错误~! 展开
Width="666px">
<ItemTemplate>
<table>
<tr>
<td style="width: 100px">
<asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Eval("State") %>'
ImageUrl='<%# Eval("State", "~/images/RoomState/{0}.jpg") %>' OnClick="ImageButton1_Click" /></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Number") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
下面是我处理的代码:
/// <summary>
/// 点击图片的触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//获得点击客房的状态
//ImageButton imag = DataList1.SelectedItem.FindControl("ImageButton1") as ImageButton;
//string aa = e.GetType().ToString();
//string state = imag.CommandArgument;
//Response.Write(aa);
ImageButton img = (ImageButton)DataList1.SelectedItem.FindControl("ImageButton1");
string state = img.CommandArgument;
//装在Session里面
Session["state"] = state;
if (Session["state"].ToString() != "empty")
{
Response.Write("<script>alert(\"对不起,已经有人了~!\")</script>");
}
}
在 imageButton 处 报了一个为将对象设置引用到实例的错误~! 展开
展开全部
用DataList 本身的onitemcommand事件。
代码实例:
<asp:DataList ID="DataList1" runat="server" onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:ImageButton ID="button1" CommandArgument='<%#Eval("ID")%>' CommandName="Edit" runat="server" />
</ItemTemplate>
</asp:DataList>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("ID", typeof(int)));
data.Columns.Add(new DataColumn("Description", typeof(string)));
DataRow row = data.NewRow();
row.ItemArray = new object[] { 2, "Description2" };
data.Rows.Add(row);
DataRow row2 = data.NewRow();
row2.ItemArray = new object[] { 1, "Description1" };
data.Rows.Add(row2);
this.DataList1.DataSource = data;
this.DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
代码实例:
<asp:DataList ID="DataList1" runat="server" onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:ImageButton ID="button1" CommandArgument='<%#Eval("ID")%>' CommandName="Edit" runat="server" />
</ItemTemplate>
</asp:DataList>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable data = new DataTable();
data.Columns.Add(new DataColumn("ID", typeof(int)));
data.Columns.Add(new DataColumn("Description", typeof(string)));
DataRow row = data.NewRow();
row.ItemArray = new object[] { 2, "Description2" };
data.Rows.Add(row);
DataRow row2 = data.NewRow();
row2.ItemArray = new object[] { 1, "Description1" };
data.Rows.Add(row2);
this.DataList1.DataSource = data;
this.DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataList1_ItemCommand(object source, DataListCommandEventArgs e)
把单击事件里面的代买写在上面这个方法里面,即可
关键是淡季事件取值的顺序搞清楚就行了
把单击事件里面的代买写在上面这个方法里面,即可
关键是淡季事件取值的顺序搞清楚就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ImageButton img = (ImageButton)DataList1.SelectedItem.FindControl("ImageButton1");
改为:ImageButton img=(ImageButton)sender;
改为:ImageButton img=(ImageButton)sender;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ImageButton img = sender as ImageButton;
if(img != null)
{
string state = img.CommandArgument;
//装在Session里面
Session["state"] = state;
}
看看这样
if(img != null)
{
string state = img.CommandArgument;
//装在Session里面
Session["state"] = state;
}
看看这样
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询