在DataGrid1_ItemCreated()中:
e.Item.ItemIndex能取到值,e.Item.Cells[1].Text为空。

解决方案 »

  1.   

    在DataGrid1_ItemCommand或者DataGrid1_ItemDataBound中去取值.
      

  2.   

    DataGrid1_ItemCommand()中:
    e.Item.Cells[1].Text还是为空
      

  3.   

    这是我用的:
    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (e.CommandName == "delete")
    {
    switch (Transactions.TransactionDel(Convert.ToInt32(e.Item.Cells[0].Text.ToString())))
    {
    case -2:
    Page.RegisterStartupScript("","<script>alert('Transaction ID is not right')</script>");
    ;
    case 1:
    BindDataGrid();
    break;
    }
    }}
      

  4.   

    ItemDataBound,ItemCreated
    首先要说的是这两个事件的发生时间。
    ItemDataBound嘛,只要执行了DataBind方法,就会马上激发这个事件。
    ItemCreated呢,如果页面是第一次访问(Page.IsPostBack = false),那在第一次执行DataBind的时候,会先激发ItemCreated事件,也就是说,执行了DataBind后,首先会用ItemCreated来建立Header行,然后用ItemDataBound来绑定Header行,再用ItemCreated来建立第一行,再调用ItemDataBound来绑定第一行,也就是说ItemCreated和ItemDataBound是交替执行的。
    页面返回时,也会执行ItemCreated事件,在Page_Load之前,但是这时候就不会再执行ItemDataBound事件了。
    所以,如果你想在DataGrid里动态添加什么控件,就需要在ItemCreated事件中,而不是在ItemDataBound事件中。
      

  5.   

    cs:using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace Test
    {
    /// <summary>
    /// Summary description for list.
    /// </summary>
    public class list : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    public static string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"]; private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    BindGrid1();
    } }

    private void BindGrid1()
    {
    SqlConnection dbConnection = new SqlConnection(connectionString);
    string commandString = "Select id,Content from Title";
    try
    {

    commandString = commandString + " where cid=@cid";
    dbConnection.Open();
    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = new SqlCommand(commandString,dbConnection);
    dataAdapter.SelectCommand.CommandType = CommandType.Text;
    SqlParameter cid = dataAdapter.SelectCommand.Parameters.Add("@cid",SqlDbType.VarChar,10);
    cid.Value = Request.QueryString["classid"].ToString(); DataSet ds = new DataSet();
    dataAdapter.Fill(ds,"Title");
    DataGrid1.DataSource = ds.Tables["Title"].DefaultView;
    DataGrid1.DataBind();


    }
    catch(SqlException e)
    {
    Response.Write(e.Message);
    Response.End();
    }
    finally
    {
    dbConnection.Close();
    } } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
    this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
    this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindGrid1();
    }
    public string CheckString(string Temp,int Strcount)
    {
    Temp=Temp.Trim();
    if (Temp.Length>Strcount)
    {
    string RE;
    RE=Temp.Substring(0,Strcount-2);
    RE=RE+"...";
    return RE;
    }
    else
    {
    return Temp;
    }
    } private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    { } private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {     Response.Write(e.Item.Cells[1].Text);
        Response.End();
        return;
    } private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        Response.Write(e.Item.Cells[1].Text);
        Response.End();
        return; }
    }}
    ======================================
    aspx:
    <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="20" Font-Names="Times New Roman"
    BorderWidth="1px" BorderColor="#E0E0E0" AllowPaging="True" CellPadding="3" BackColor="White" BorderStyle="Groove"
    Font-Size="10pt" Width="410px">
    <SelectedItemStyle BackColor="LightGray"></SelectedItemStyle>
    <AlternatingItemStyle BorderColor="LightGray" VerticalAlign="Middle" BackColor="#EEEEEE"></AlternatingItemStyle>
    <HeaderStyle BorderColor="White" BackColor="#AAAADD"></HeaderStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="序号">
    <ItemTemplate>
    <asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"id")%>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="内容">
    <ItemTemplate>
    <asp:Label id=Label2 runat="server" Text='<%# CheckString((string)DataBinder.Eval(Container.DataItem,"Content"),18)%>' ToolTip='<%# DataBinder.Eval(Container.DataItem,"Content")%>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="提交">
    <ItemTemplate>
    <FONT face="宋体">
    <asp:ImageButton id="ImageButton1" runat="server" CommandName="select" ImageUrl="image\select.gif"></asp:ImageButton></FONT>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle VerticalAlign="Middle" NextPageText="&gt;&gt;" PrevPageText="&lt;&lt;" BorderStyle="Groove"
    PageButtonCount="20" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
      

  6.   

    试试看:
    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if e.CommandName == "select")
    {
    Response.Write(e.Item.Cells[1].Text);
    }
    }
      

  7.   

    你的DataGrid的第二列是什么列呢?
      

  8.   

    http://218.27.204.17/aspnet/a_user.aspx
      

  9.   

    模板列是这样是找不到的,需要findcontrol
    dim aa as htmlinputtext
    aa=datagrid.item(i).findcontrol("ID名称")
    dim bb as string
    bb=aa.text
      

  10.   

    说一下步骤吧,在数据绑定到时,Datagrid会执行这个事件ItemDataBound,在这个事件里,用findcontrol
    找到你的imageButton,然后给imageButton的属性CommandArgument,负值为你当前的行号,在或的时就简单了,在你ImageButton是事件里就可以或的了.
       private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    Response.Write(((ImageButton)sender).CommandArgument);
                                //((ImageButton)sender).CommandArgument 这个获得你行号
    }
      

  11.   

    private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {     Response.Write(e.Item.Cells[1].Text);
        Response.End();
        return;
    }中的
    Response.End();
    去掉
      

  12.   

    Response.End();
    return;
    一起去掉
      

  13.   

    Response.End();
    return;
    一起去掉,
    不会是这里的问题。
      

  14.   

    把摸板列里面的控件付上ID
    比如是TextBox的,ID="TextBox1";
    ((TextBox ) e.Item .FindControl ("TextBox1")).Text ;
    就可以了