GridView模板按钮

时间:2013-06-04 13:47:43

标签: asp.net gridview sql-update

这是我的gridview:

   <asp:GridView ID="gvconsumer" runat="server" AutoGenerateColumns="False" Width="50%" >
        <Columns>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Label ID="lblid" runat="server" Text='<%# Bind("id") %>' Visible="false" ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Consumer">
                <ItemTemplate>
                    <asp:Label ID="lblconsumer" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="lblname" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:Label ID="lblstatus" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DateCreated">
                <ItemTemplate>
                    <asp:Label ID="lbldatecreaterd" runat="server" Text='<%# Bind("DateCreated") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button ID="btnReject" runat="server" Text="Reject" OnClick="btnApprove_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

这是我的代码:( AcceptButton)

 if (btnApprove_Click)
    GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;

    string id = ((Label)row.FindControl("lblid")).Text;
    Response.Write(row.RowIndex);
    string ApprovedStatus = "Approved";

    string status = ClassBiller.ConsumerStatus(int.Parse(id), ApprovedStatus, DateTime.Now);

这是ConsumerStatus()的数据层:

        string retUpdateStatus = "";

        string consumerStatus = "UPDATE MerchantConsumer SET Status = @Status, DateAccepted = @DateAccepted WHERE ConsumerID = @Consumerid";

        string[] param = { "@Consumerid", "@Status", "@DateAccepted" };
        object[] paramVal = { ConsumerID, Status, DateAccepted };

        try
        {
            ClassDBQuery.ExecNonQuery(consumerStatus, param, paramVal);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return retUpdateStatus;

这就是GridView的绑定方式:

    DataTable gridViewConsumer = new DataTable();
    gridViewConsumer = ClassConsumer.GetRows();

    gvconsumer.DataSource = gridViewConsumer;
    gvconsumer.DataBind();

我的问题是,我必须双击按钮更新gridView的状态字段。但是当我检查数据库时(第一次点击),状态字段已经更新.. 请帮助....

1 个答案:

答案 0 :(得分:2)

成功进行数据库操作后,您需要再次绑定网格。