GridView中不是有个编辑的嘛,点编辑会出现更新,点更新会出现把每一个列的值都变得像textBox一样的可以输入。
比如说我有一列值是:
名字 年龄
禁药 20假如就是这样的一列,我点更新的时候。我们可以取得名字这列的值为“禁药”,但是我想把“禁药”这东西改成“西西”请问怎么改、。 我之前都只能取得“禁药”这个值。但是把“禁药”改成“西西”点更新后,取来的值还是“禁药”,什么都没改变。大家帮帮忙下谢谢啊!!!
比如说我有一列值是:
名字 年龄
禁药 20假如就是这样的一列,我点更新的时候。我们可以取得名字这列的值为“禁药”,但是我想把“禁药”这东西改成“西西”请问怎么改、。 我之前都只能取得“禁药”这个值。但是把“禁药”改成“西西”点更新后,取来的值还是“禁药”,什么都没改变。大家帮帮忙下谢谢啊!!!
前台:
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="修改"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="更新"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
后台:
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
} /// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["GetConnection"].ToString());
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
string sqlstr = "update Product_Category set name=@name,pid=@pid,[goto]=@goto where id="+id;
SqlCommand cmd = new SqlCommand(sqlstr,con);
cmd.CommandType = CommandType.Text;
try
{
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
cmd.Parameters.Add("@pid", SqlDbType.Int).Value = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim());
cmd.Parameters.Add("@goto", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
catch { }
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
GridView1.EditIndex = -1;
Bind(); }
{
DataTable dt;
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
((TextBox)e.Row.Cells[0].Controls[0]).Text = "西西";//非模板列
//((TextBox)e.Row.Cells[0].FindControl("TextBox1")).Text="西西";//模板列 }
}
}