[100+40+40]分,问个问什么。
问题:
这是更新代码:
protected void DataGrid_Upadte(object source, DataGridCommandEventArgs e)
{
try
{
con.Open();
string sqlUpdate = "update Power set PowerName=@PowerName,PowerLink=@PowerLink where PowerID=@PowerID ";
SqlCommand com = new SqlCommand(sqlUpdate, con);
com.Parameters.Add(new SqlParameter( "@PowerName ", SqlDbType.VarChar, 50));
com.Parameters.Add(new SqlParameter( "@PowerLink ", SqlDbType.VarChar, 100));
com.Parameters.Add(new SqlParameter( "@PowerID ",SqlDbType.Int));
com.Parameters[ "@PowerName "].Value =((TextBox)(e.Item.Cells[2].Controls[0])).Text;//注:要是把“SSS”等字符串赋予它就可以写入
com.Parameters[ "@PowerLink "].Value =((TextBox)(e.Item.Cells[3].Controls[0])).Text;//注:要是把“SSS”等字符串赋予它就可以写入 com.Parameters[ "@PowerID "].Value = Dg_Edit.DataKeys[(int)e.Item.ItemIndex];
com.ExecuteNonQuery();
string s = com.CommandText;
Dg_Edit.EditItemIndex = -1;
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
if (con.State != ConnectionState.Closed)
con.Close();
}
dataBindToDataGrid();
}
下面是datagrid的代码:
<asp:DataGrid ID= "Dg_Edit " runat= "server " AutoGenerateColumns= "False " DataKeyField= "PowerID " OnDeleteCommand= "DataGrid_DeleteCommand " OnEditCommand= "DataGrid_Edit " OnCancelCommand= "DataGrid_Cancel " OnUpdateCommand= "DataGrid_Upadte ">
<Columns>
<asp:BoundColumn DataField= "PowerID " HeaderText= "PowerID " ReadOnly= "True "> </asp:BoundColumn>
<asp:BoundColumn DataField= "PowerCode " HeaderText= "PowerCode " ReadOnly= "True "> </asp:BoundColumn>
<asp:BoundColumn DataField= "PowerName " HeaderText= "PowerName "> </asp:BoundColumn>
<asp:BoundColumn DataField= "PowerLink " HeaderText= "PowerLink "> </asp:BoundColumn>
<asp:EditCommandColumn CancelText= "取消 " EditText= "编辑 " UpdateText= "更新 "> </asp:EditCommandColumn>
<asp:ButtonColumn CommandName= "Delete " Text= "删除 "> </asp:ButtonColumn>
</Columns>
</asp:DataGrid>
实际上昨晚在 http://community.csdn.net/Expert/topic/5478/5478998.xml?temp=.9544031 和
http://community.csdn.net/Expert/topic/5479/5479240.xml?temp=.2504694 就问过。没有人回答。正解者,前去领分。一共180分。
[解决办法]
友情UP
[解决办法]
然后呢??你想问什么???只给出代码,没有问题噻
[解决办法]
什么问题?
[解决办法]
你是不是在Page_Load中绑定了DataGrid?如果是加上if(!this.IsPostBack){//绑定}
[解决办法]
跟踪一下,看看((TextBox)(e.Item.Cells[2].Controls[0])).Text有没有值,如果没有,看看是否索引不正确等
[解决办法]
com.Parameters[ "@PowerName "].Value =((TextBox)(e.Item.Cells[2].Controls[0])).Text;//注:要是把“SSS”等字符串赋予它就可以写入
com.Parameters[ "@PowerLink "].Value =((TextBox)(e.Item.Cells[3].Controls[0])).Text;//注:要是把“SSS”等字符串赋予它就可以写入
是不是根本就是没找到这个控件啊!
调试跟踪一下
[解决办法]
protected void DataGrid_Upadte(object source, DataGridCommandEventArgs e)
是写在aspx中的还是aspx.cs文件中的?
如果是写在aspx中的 OnUpdateCommand= "DataGrid_Upadte " 那么相应的事件注册就该在后置代码中了
(顺便提一句,是update不是upadte)
还有记得在Page_Load事件中加
if(!this.IsPostBack)
{
//绑定
}