如何找到Datalist中EditItemTemplate中的控件????
取不到EditItemTemplate中控件的值总是找不到控件我是在ItemCommand中判断CommandName再从EditItemTemplate中FindCon...
取不到EditItemTemplate中控件的值
总是找不到控件
我是在ItemCommand中判断CommandName
再从EditItemTemplate中FindControl
但是找不到
该怎么办呢???? 展开
总是找不到控件
我是在ItemCommand中判断CommandName
再从EditItemTemplate中FindControl
但是找不到
该怎么办呢???? 展开
2个回答
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 在ItemCommand事件中查找指定ID的控件
/// </summary>
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
bool isChecked = false;
if (e.CommandName == "你自定义的CommandName")
{
CheckBox checkBox1 = (CheckBox)(e.Item.FindControl("CheckBox1"));
isChecked = checkBox1.Checked;
if (isChecked)
{
ProductID = this.DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(ProductID.ToString());
}
}
}
/// <summary>
/// 在DataList的每项进行数据绑定的事件中查找指定ID的控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int ProductID = 0;
bool isChecked = ((CheckBox)e.Item.FindControl("CheckBox1")).Checked;
if (isChecked)
{
ProductID = this.DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(ProductID.ToString());
}
}
}
/// <summary>
/// 通过遍历DataList的项来查找其中的控件
/// </summary>
/// <param name="DL"></param>
/// <returns></returns>
protected bool GetSelectedCheckBox(DataList DL)
{
int itemCount = DL.Items.Count;
int ProductID = 0;
bool isChecked = false;
for (int i = 0; i < itemCount; i++)
{
isChecked = ((CheckBox)DL.Items[i].FindControl("CheckBox1")).Checked;
if (isChecked)
{
ProductID = this.DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(ProductID.ToString());
}
}
}
}
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList runat="server" ID="DataList1" OnItemCreated="DataList1_ItemCreated" DataKeyField="ProductID">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<%# DataBinder.Eval(Container.DataItem,"ProductName" %>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<%# DataBinder.Eval(Container.DataItem,"ProductName" %>
</AlternatingItemTemplate>
</asp:datalist>
</form>
</body>
</html>
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 在ItemCommand事件中查找指定ID的控件
/// </summary>
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
bool isChecked = false;
if (e.CommandName == "你自定义的CommandName")
{
CheckBox checkBox1 = (CheckBox)(e.Item.FindControl("CheckBox1"));
isChecked = checkBox1.Checked;
if (isChecked)
{
ProductID = this.DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(ProductID.ToString());
}
}
}
/// <summary>
/// 在DataList的每项进行数据绑定的事件中查找指定ID的控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int ProductID = 0;
bool isChecked = ((CheckBox)e.Item.FindControl("CheckBox1")).Checked;
if (isChecked)
{
ProductID = this.DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(ProductID.ToString());
}
}
}
/// <summary>
/// 通过遍历DataList的项来查找其中的控件
/// </summary>
/// <param name="DL"></param>
/// <returns></returns>
protected bool GetSelectedCheckBox(DataList DL)
{
int itemCount = DL.Items.Count;
int ProductID = 0;
bool isChecked = false;
for (int i = 0; i < itemCount; i++)
{
isChecked = ((CheckBox)DL.Items[i].FindControl("CheckBox1")).Checked;
if (isChecked)
{
ProductID = this.DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(ProductID.ToString());
}
}
}
}
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList runat="server" ID="DataList1" OnItemCreated="DataList1_ItemCreated" DataKeyField="ProductID">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<%# DataBinder.Eval(Container.DataItem,"ProductName" %>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<%# DataBinder.Eval(Container.DataItem,"ProductName" %>
</AlternatingItemTemplate>
</asp:datalist>
</form>
</body>
</html>
展开全部
你要找的是DataList的EditItem的子控件,可以这样:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "test")
{
DataList dl=((DataList)source);
Label label =(Label)( dl.Items[dl.EditItemIndex].FindControl("Label1"));
label.Text = "adfad";
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "test")
{
DataList dl=((DataList)source);
Label label =(Label)( dl.Items[dl.EditItemIndex].FindControl("Label1"));
label.Text = "adfad";
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询