在DataGrid1_ItemCreated()中:
e.Item.ItemIndex能取到值,e.Item.Cells[1].Text为空。
e.Item.ItemIndex能取到值,e.Item.Cells[1].Text为空。
解决方案 »
- 网站运行一段时间后报错, 只要重启IIS马上就好, 请教各位大侠这是怎么回事,谢谢!
- 我想把Panel1中的控件添加到panel2中去,请问我该怎么做??
- 关于对<%# Bind("fieldtName") %>的处理方法!
- 数据库中:如何在存储过程中为一个变量,通过查询语句获赋值?
- 这vb 真的不如java 好用
- 能者顶,分就是你的了!~~~~如何在.ascx中实现DropDownList的AutoPostBack属性
- 帮帮我吧,全文索引的问题!粉急,,我真的郁闷啊。。。。怪事连连!
- 可不可以让水晶报表导出的word不在浏览器打开,而是用word打开啊(再贴100分,两个问题解决,立马结贴)
- GridView查询,对查询结果编辑,可是每次一点“编辑”全部数据都显示了出来
- 紧急求助!关于ShowModalDialog页面传递数组
- 一系列问题,难~100分
- ◆◇◆救命啊◆◇◆江湖救急◆◇◆ 倾我所有分,在线等待。关于修改 Web.Config.xml 文件中的数据的问题!
e.Item.Cells[1].Text还是为空
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;
}
}}
首先要说的是这两个事件的发生时间。
ItemDataBound嘛,只要执行了DataBind方法,就会马上激发这个事件。
ItemCreated呢,如果页面是第一次访问(Page.IsPostBack = false),那在第一次执行DataBind的时候,会先激发ItemCreated事件,也就是说,执行了DataBind后,首先会用ItemCreated来建立Header行,然后用ItemDataBound来绑定Header行,再用ItemCreated来建立第一行,再调用ItemDataBound来绑定第一行,也就是说ItemCreated和ItemDataBound是交替执行的。
页面返回时,也会执行ItemCreated事件,在Page_Load之前,但是这时候就不会再执行ItemDataBound事件了。
所以,如果你想在DataGrid里动态添加什么控件,就需要在ItemCreated事件中,而不是在ItemDataBound事件中。
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=">>" PrevPageText="<<" BorderStyle="Groove"
PageButtonCount="20" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if e.CommandName == "select")
{
Response.Write(e.Item.Cells[1].Text);
}
}
dim aa as htmlinputtext
aa=datagrid.item(i).findcontrol("ID名称")
dim bb as string
bb=aa.text
找到你的imageButton,然后给imageButton的属性CommandArgument,负值为你当前的行号,在或的时就简单了,在你ImageButton是事件里就可以或的了.
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Write(((ImageButton)sender).CommandArgument);
//((ImageButton)sender).CommandArgument 这个获得你行号
}
{ Response.Write(e.Item.Cells[1].Text);
Response.End();
return;
}中的
Response.End();
去掉
return;
一起去掉
return;
一起去掉,
不会是这里的问题。
比如是TextBox的,ID="TextBox1";
((TextBox ) e.Item .FindControl ("TextBox1")).Text ;
就可以了