我在更新"景点名"字段是可以更新,但在更新"说明"字段时在一定长度内可以更新,但超过一定长度更新就没用,望好人帮忙,谢谢!
.aspx文件
<asp:datagrid id="news" runat="server"
AutoGenerateColumns="False"
OnPageIndexChanged="DataGrid_page" "
OnEditCommand="DataGrid_edit"
OnUpdateCommand="DataGrid_update"
DataKeyField="id">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="景点名"></asp:BoundColumn>
<asp:BoundColumn DataField="show" HeaderText="说明"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑" ItemStyle-Width="55"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
.aspx.cs文件
public void DataGrid_update(object sender,DataGridCommandEventArgs e)
{
string updateStr = "Update news set title=@title,show=@show where id=@id";
SqlCommand cm = new SqlCommand(updateStr,cn);
cm.Parameters.Add(new SqlParameter("@title",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@show",SqlDbType.Text,16));
cm.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@title"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@show"].Value=colvalue;
cm.Parameters["@id"].Value=news.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
news.EditItemIndex=-1;
}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
BindGrid();
}