0

我有一个gridview,我想修改gridview上的一些字段。当我单击更新按钮时,此字段值不会改变。我尝试使用回发控件,但这个问题一直存在。我该如何解决这个问题?

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();
        }
    }
}
4

2 回答 2

1

请检查你是否在里面绑定了gridview

if(!page.ispostback)
{

} 
于 2013-07-24T09:16:16.837 回答
0

您必须将数据更新到实际数据源并重新绑定它。

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

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

mGridView.DataBind();

编辑1:

这个方法是否被称为 break 在那里检查

你设置更新为

commandText =“更新”

于 2013-07-24T08:56:20.927 回答