Gridview字段值在更新时不会更改

时间:2013-07-24 08:51:28

标签: asp.net gridview postback

我有一个gridview,我想修改gridview上的一些字段。当我点击Update按钮时,这个字段的值不会改变。我试过使用Postback控件但这个问题还在继续。我怎么能解决这个问题?

ASPX代码

<asp:GridView ID="gview" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" GridLines="Horizontal" OnRowDataBound="gview_RowDataBound" OnRowEditing="gview_RowEditing" OnRowUpdating="gview_RowUpdating" OnRowCancelingEdit="gview_RowCancelingEdit">
    <Columns>
        <asp:BoundField DataField="SubCategoryId" HeaderText="ID" InsertVisible="False" ReadOnly="True"
            SortExpression="SubCategoryId" />
        <asp:TemplateField HeaderText="Category">
            <ItemTemplate>
                <asp:Label ID="lblCategory" runat="server"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCategory" DataValueField="CategoryId" DataTextField="CategoryName" runat="server" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
        <asp:CommandField ButtonType="Link" EditText="Edit" HeaderText="Edit"
            ShowEditButton="True" ShowHeader="False" CancelText="Cancel" UpdateText="Update" />
    </Columns>
</asp:GridView>

C#代码

protected void gview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lbl = e.Row.FindControl("lblCategory") as Label;
        DropDownList ddl = e.Row.FindControl("ddlCategory") as DropDownList;
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            ddl.DataSource = LoadCategories();
            ddl.DataBind();
        }

        if (lbl != null)
        {
            lbl.Text = GetCategoryName(Convert.ToInt32(gview.DataKeys[e.Row.RowIndex][0]));
        }
    }
}

protected void gview_RowEditing(object sender, GridViewEditEventArgs e)
{
    gview.EditIndex = e.NewEditIndex;
    SubCategoryLoad();
}

protected void gview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int categoryId, subCategoryId;
    string categoryName;

    DropDownList ddl = (DropDownList)gview.Rows[e.RowIndex].FindControl("ddlCategory");

    subCategoryId = int.Parse(gview.Rows[e.RowIndex].Cells[0].Text);
    categoryId = int.Parse(ddl.SelectedValue);
    categoryName = gview.Rows[e.RowIndex].Cells[2].Text;

    gview.EditIndex = -1;
    UpdateSubCategory(subCategoryId,categoryName,categoryId);
    SubCategoryLoad();

}

public void SubCategoryLoad()
{
    using (SqlConnection conn = new SqlConnection(DataBase.Conn))
    {
        conn.Open();
        string query = "SELECT * FROM dbo.SubCategories";
        using (SqlDataAdapter da = new SqlDataAdapter(query, conn))
        {
            DataTable dt = new DataTable();
            da.Fill(dt);
            gview.DataSource = dt;
            gview.DataBind();
        }
    }
}

public int UpdateSubCategory(int subCategoryId, string categoryName, int categoryId)
{
    using (SqlConnection conn = new SqlConnection(DataBase.Conn))
    {
        conn.Open();
        string query = "UPDATE dbo.SubCategories SET CategoryId = @categoryId, CategoryName = @categoryName WHERE SubCategoryId = @id";
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            cmd.Parameters.AddWithValue("@id", subCategoryId);
            cmd.Parameters.AddWithValue("@categoryName", categoryName);
            cmd.Parameters.AddWithValue("@categoryId", categoryId);
            return (int)cmd.ExecuteNonQuery();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须将数据更新为实际数据源并重新绑定

如果您更新了值,则应使用更新的值再次重新绑定 GridView

  

mGridView.DataSource = {您的数据来源};

     

mGridView.DataBind();

编辑1:

这个方法被调用会在那里检查

您是否将更新设为

  

commandText =&#34;更新&#34;

答案 1 :(得分:0)

请检查是否绑定了gridview里面的

if(!page.ispostback)
{

}