小妹求救,在datalist控件中添加了一个textbox和一个button,button的方法是CommandBtn_Click,当点击button时,将textbox的值赋为空,这段语句怎么写. 在datalist控件中添加了一个textbox和一个button,button的方法是CommandBtn_Click,当点击button时,将textbox的值赋为空,这段语句怎么写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DataList中的Button不能直接双击添加代码。应该给这个Button的CommandName复值,然后在DataList的ItemCommand事件中判断CommandName的值,并写代码。 例如: <asp:Button id="btnAdd" runat="Server" Text="增加" CommandName="Add" > </asp:Button> 然后再DataList的ItemCommand事件中写: if(e.CommandName=="Add") { //在此写上面按钮的单机事件。 } 1首先给button的CommandName赋值比如edit;2在datalist的itemcommand事件里面通过e.CommandName来判断是否触发了按钮的单击事件 楼主用textBox的话一般是在编辑界面内((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text="";1楼里面的代码 <%@ Page Language="C#" AutoEventWireup="true" enableEventValidation="false" CodeFile="DataList.aspx.cs" Inherits="DataList" %><!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"> <div> <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"> <ItemTemplate> <asp:TextBox ID='txt' runat="server" Text='<%# Eval("学生姓名") %>'></asp:TextBox> <asp:Button ID="CommandBtn" runat="server" Text="Button" OnClick="CommandBtn_Click" /> </ItemTemplate> </asp:DataList></div> </form></body></html>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 DataList : System.Web.UI.Page{ protected ICollection CreateDataSource() { System.Data.DataTable dt = new System.Data.DataTable(); System.Data.DataRow dr; dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal))); for (int i = 0; i < 11; i++) { System.Random rd = new System.Random(Environment.TickCount * i); ; dr = dt.NewRow(); dr[0] = "班级" + i.ToString(); dr[1] = "学生" + i.ToString(); dr[2] = System.Math.Round(rd.NextDouble() * 100, 2); dr[3] = System.Math.Round(rd.NextDouble() * 100, 2); dr[4] = System.Math.Round(rd.NextDouble() * 100, 2); dr[5] = System.Math.Round(rd.NextDouble() * 100, 2); dt.Rows.Add(dr); } System.Data.DataView dv = new System.Data.DataView(dt); return dv; } protected void Page_Load(object sender, EventArgs e) { DataList1.DataSource = CreateDataSource(); DataList1.DataBind(); } protected void CommandBtn_Click(object sender, EventArgs e) { Button btn = sender as Button; if (btn != null) { int index=Convert.ToInt32( btn.CommandArgument); TextBox txt = DataList1.Items[index].FindControl("txt") as TextBox; if (txt != null) { txt.Text = ""; } } } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { Button btn = e.Item.FindControl("CommandBtn") as Button; if (btn != null) { btn.CommandArgument = e.Item.ItemIndex.ToString(); } } }} 同2楼或google 事件冒泡或 http://www.cnblogs.com/huobazi/archive/2006/04/08/TwoDropDownListInDataGridAndBubbleEvent.html 诚心求教各位发邮件的问题 C#做B/S系统,实现串口通信... 关于两个域名使用ajax的问题。 问个asp.net mvc的问题 求救:谁知道这种照片处理怎么实现的 ASP.NET/C#如何指定Cookie访问第三方网站 网站发布之后的问题,请进来看来一下。 跪求ASP单个页面的浏览统计代码 求好的数据统计插件推荐~~~~ 请教大侠,急。。。谢谢 2003中可以,到了2005就不行~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Repeater中Container.DataItem中的值能传递到变量中么
应该给这个Button的CommandName复值,然后在DataList的ItemCommand事件中判断CommandName的值,并写代码。
例如:
<asp:Button id="btnAdd" runat="Server" Text="增加" CommandName="Add" > </asp:Button>
然后再DataList的ItemCommand事件中写:
if(e.CommandName=="Add")
{
//在此写上面按钮的单机事件。
}
2在datalist的itemcommand事件里面通过e.CommandName来判断是否触发了按钮的单击事件
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:TextBox ID='txt' runat="server" Text='<%# Eval("学生姓名") %>'></asp:TextBox>
<asp:Button ID="CommandBtn" runat="server" Text="Button" OnClick="CommandBtn_Click" />
</ItemTemplate>
</asp:DataList></div>
</form>
</body>
</html>
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 DataList : System.Web.UI.Page
{
protected ICollection CreateDataSource()
{ System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal))); for (int i = 0; i < 11; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "学生" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
} protected void Page_Load(object sender, EventArgs e)
{
DataList1.DataSource = CreateDataSource();
DataList1.DataBind();
}
protected void CommandBtn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
int index=Convert.ToInt32( btn.CommandArgument);
TextBox txt = DataList1.Items[index].FindControl("txt") as TextBox;
if (txt != null)
{
txt.Text = "";
}
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Button btn = e.Item.FindControl("CommandBtn") as Button;
if (btn != null)
{
btn.CommandArgument = e.Item.ItemIndex.ToString();
}
}
}
}
或google 事件冒泡
或 http://www.cnblogs.com/huobazi/archive/2006/04/08/TwoDropDownListInDataGridAndBubbleEvent.html