刚刚开始学.NET,遇到这样一个问题,希望大家帮我看看,谢谢了DataGrid中数据更新的问题我的DataGrid是这样定义的<asp:DataGrid id="awoke" runat="server" AutoGenerateColumns="False" PageSize="15"
HorizontalAlign="Center" DataKeyField="id" OnCancelCommand="awoke_cancel" OnEditCommand="awoke_edit"
OnDeleteCommand="awoke_delete" OnUpdateCommand="awoke_update"> <Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
</asp:EditCommandColumn> <asp:BoundColumn HeaderText="时间" DataField="awokedate">
<ItemStyle HorizontalAlign="Center" Width="25%"></ItemStyle>
</asp:BoundColumn> <asp:BoundColumn HeaderText="提醒事件" DataField="re">
<ItemStyle HorizontalAlign="Left" Width="58%"></ItemStyle>
</asp:BoundColumn> <asp:ButtonColumn Text="删除" CommandName="Delete">
<HeaderStyle Width="7%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn> </Columns>
</asp:DataGrid>相应的CS文件里面的代码为:private void Page_Load(object sender, System.EventArgs e)
{
string id=Request.QueryString["id"].ToString();
HyperLink2.NavigateUrl="client_show.aspx?id=" + id;
awokeadd.NavigateUrl="awoke_add.aspx?id=" + id;
bindmessage();
}
public void awoke_delete(object sender,DataGridCommandEventArgs e)
{
string id=awoke.DataKeys[e.Item.ItemIndex].ToString();
obj.dataopen();
string sql="delete from [awoke] where id='"+id+"'";
myCommand=new SqlCommand(sql,obj.myConn);
myCommand.ExecuteNonQuery();
bindmessage();
obj.myConn.Close();
}
public void awoke_edit(object sender,DataGridCommandEventArgs e)
{
awoke.EditItemIndex=(int)e.Item.ItemIndex;
bindmessage();
} public void awoke_cancel(object sender,DataGridCommandEventArgs e)
{
awoke.EditItemIndex = -1;
bindmessage();
} public void awoke_update(object sender,DataGridCommandEventArgs e)
{
string id = awoke.DataKeys[e.Item.ItemIndex].ToString();
//应该是以下代码存在问题,请大家帮我看看(提交更改后数据并没有更新!) int awokedate = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);
int re = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text); obj.dataopen();
string sql="update [awoke] set awokedate="+awokedate+",re="+re+" where id=1";
myCommand=new SqlCommand(sql,obj.myConn);
myCommand.ExecuteNonQuery();
awoke.EditItemIndex = -1;
bindmessage();
obj.myConn.Close();
}
public void bindmessage()
{
string id=Request.QueryString["id"].ToString();
obj.dataopen();
string sql="select * from [awoke] where company='"+id+"'";
myCommand=new SqlCommand(sql,obj.myConn);
myReader=myCommand.ExecuteReader();
awoke.DataSource=myReader;
awoke.DataBind();
obj.myConn.Close();
}
HorizontalAlign="Center" DataKeyField="id" OnCancelCommand="awoke_cancel" OnEditCommand="awoke_edit"
OnDeleteCommand="awoke_delete" OnUpdateCommand="awoke_update"> <Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
</asp:EditCommandColumn> <asp:BoundColumn HeaderText="时间" DataField="awokedate">
<ItemStyle HorizontalAlign="Center" Width="25%"></ItemStyle>
</asp:BoundColumn> <asp:BoundColumn HeaderText="提醒事件" DataField="re">
<ItemStyle HorizontalAlign="Left" Width="58%"></ItemStyle>
</asp:BoundColumn> <asp:ButtonColumn Text="删除" CommandName="Delete">
<HeaderStyle Width="7%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn> </Columns>
</asp:DataGrid>相应的CS文件里面的代码为:private void Page_Load(object sender, System.EventArgs e)
{
string id=Request.QueryString["id"].ToString();
HyperLink2.NavigateUrl="client_show.aspx?id=" + id;
awokeadd.NavigateUrl="awoke_add.aspx?id=" + id;
bindmessage();
}
public void awoke_delete(object sender,DataGridCommandEventArgs e)
{
string id=awoke.DataKeys[e.Item.ItemIndex].ToString();
obj.dataopen();
string sql="delete from [awoke] where id='"+id+"'";
myCommand=new SqlCommand(sql,obj.myConn);
myCommand.ExecuteNonQuery();
bindmessage();
obj.myConn.Close();
}
public void awoke_edit(object sender,DataGridCommandEventArgs e)
{
awoke.EditItemIndex=(int)e.Item.ItemIndex;
bindmessage();
} public void awoke_cancel(object sender,DataGridCommandEventArgs e)
{
awoke.EditItemIndex = -1;
bindmessage();
} public void awoke_update(object sender,DataGridCommandEventArgs e)
{
string id = awoke.DataKeys[e.Item.ItemIndex].ToString();
//应该是以下代码存在问题,请大家帮我看看(提交更改后数据并没有更新!) int awokedate = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);
int re = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text); obj.dataopen();
string sql="update [awoke] set awokedate="+awokedate+",re="+re+" where id=1";
myCommand=new SqlCommand(sql,obj.myConn);
myCommand.ExecuteNonQuery();
awoke.EditItemIndex = -1;
bindmessage();
obj.myConn.Close();
}
public void bindmessage()
{
string id=Request.QueryString["id"].ToString();
obj.dataopen();
string sql="select * from [awoke] where company='"+id+"'";
myCommand=new SqlCommand(sql,obj.myConn);
myReader=myCommand.ExecuteReader();
awoke.DataSource=myReader;
awoke.DataBind();
obj.myConn.Close();
}
{
string id = awoke.DataKeys[e.Item.ItemIndex].ToString();
//应该是以下代码存在问题,请大家帮我看看(提交更改后数据并没有更新!) int awokedate = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);
int re = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text); obj.dataopen();
string sql="update [awoke] set awokedate="+awokedate+",re="+re+" where id=1";
myCommand=new SqlCommand(sql,obj.myConn);
myCommand.ExecuteNonQuery();
awoke.EditItemIndex = -1;
bindmessage();
obj.myConn.Close();
}应该是这个函数有问题,我把 string sql="update [awoke] set awokedate="+awokedate+",re="+re+" where id=1"; 改成
string sql="update [awoke] set awokedate=1,re="+re+" where id=1";
运行后awokedate的数据更新为1了,说明是
int awokedate = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);
int re = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);
这两句并没有获得值,请教:怎么才能获得值?