0

グリッドビューがあり、グリッドビューのいくつかのフィールドを変更したい.このフィールドの値は、[更新]ボタンをクリックしても変更されません.ポストバックコントロールを使用しようとしましたが、この問題は続きます.この問題を解決するにはどうすればよいですか?

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 に答える