【问题标题】:Gridview fields values does not change on updateGridview 字段值在更新时不会更改
【发布时间】:2013-07-24 08:51:28
【问题描述】:

我有一个 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();
        }
    }
}

【问题讨论】:

  • subcategoryLoad() 在哪里
  • 还有 UpdateSubCategory
  • 我没有看到任何重新绑定?
  • 我添加了两个方法(SubCategoryLoad 和 UpdateSubCategory)。

标签: asp.net gridview postback


【解决方案1】:

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

if(!page.ispostback)
{

} 

【讨论】:

    【解决方案2】:

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

    如果您已经更新了值,那么您应该使用更新的值再次重新绑定 GridView

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

    mGridView.DataBind();

    编辑 1:

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

    您是否将更新设置为

    commandText ="更新"

    【讨论】:

    • 我照你说的做了,但问题仍然存在。
    • 检查如它所说的 if(!page.ispostback) { }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多