小妹求救,在datalist控件中添加了一个textbox和一个button,button的方法是CommandBtn_Click,当点击button时,将textbox的值赋为空,这段语句怎么写.

kaqia2003 2008-02-26 10:25:14
在datalist控件中添加了一个textbox和一个button,button的方法是CommandBtn_Click,当点击button时,将textbox的值赋为空,这段语句怎么写
...全文
166 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
回.到.未来 2008-02-26
  • 打赏
  • 举报
回复
1楼正解
阿非 2008-02-26
  • 打赏
  • 举报
回复


<%@ 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();
}
}
}
}


buller 2008-02-26
  • 打赏
  • 举报
回复
楼主用textBox的话一般是在编辑界面内

((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text="";

1楼里面的代码
jimu8130 2008-02-26
  • 打赏
  • 举报
回复
1首先给button的CommandName赋值比如edit;
2在datalist的itemcommand事件里面通过e.CommandName来判断是否触发了按钮的单击事件

zhuanshen712 2008-02-26
  • 打赏
  • 举报
回复
DataList中的Button不能直接双击添加代码。
应该给这个Button的CommandName复值,然后在DataList的ItemCommand事件中判断CommandName的值,并写代码。
例如:
<asp:Button id="btnAdd" runat="Server" Text="增加" CommandName="Add" > </asp:Button>
然后再DataList的ItemCommand事件中写:
if(e.CommandName=="Add")
{
//在此写上面按钮的单机事件。
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧