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

解决方案 »

  1.   

    DataList中的Button不能直接双击添加代码。
    应该给这个Button的CommandName复值,然后在DataList的ItemCommand事件中判断CommandName的值,并写代码。 
    例如: 
    <asp:Button   id="btnAdd"   runat="Server"     Text="增加"   CommandName="Add"   > </asp:Button> 
    然后再DataList的ItemCommand事件中写: 
    if(e.CommandName=="Add") 

        //在此写上面按钮的单机事件。 
      

  2.   

    1首先给button的CommandName赋值比如edit;
    2在datalist的itemcommand事件里面通过e.CommandName来判断是否触发了按钮的单击事件
      

  3.   

    楼主用textBox的话一般是在编辑界面内((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text="";1楼里面的代码
      

  4.   

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

  5.   

    同2楼
    或google 事件冒泡
    或 http://www.cnblogs.com/huobazi/archive/2006/04/08/TwoDropDownListInDataGridAndBubbleEvent.html