GridView DataBind()无法处理“提交”按钮

时间:2013-05-14 09:45:06

标签: asp.net gridview

我在页面上有一个网格视图,点击提交按钮,一行成功添加到数据库表中 但是当我导航到另一个页面并再次返回时,网格视图不会自动更新,然后是网格视图 显示新添加的记录。

我无法弄清楚它为什么会发生,即使我使用调试模式检查网格中的行数,因此网格的Rows.Count 显示1行,但网格不能显示iT

我在代码背后没有使用GridView的数据源做任何事情,它只是在设计中绑定到SQLDatasource 图。

提交按钮后面的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
                EAuctionEntities ea = new EAuctionEntities();
                Bid b = new Bid();

                b.bidderemail = Session["UserEmail"].ToString();
                b.biddername = Session["UserName"].ToString();
                b.bidend = Convert.ToDateTime(txtEndsOn.Text);
                b.bidplacedon = DateTime.Now.Date;
                b.bidprice = Convert.ToDecimal(txtYourBid.Text.Trim());
                b.isavailable = true;
                b.prod = Convert.ToInt32(Request.QueryString["id"]);
                b.bidminprice = Convert.ToDecimal(txtBidStart.Text.Trim());
                b.iswon = false;

                ea.Bids.AddObject(b);
                if (ea.SaveChanges() == 1)
                {
                    lblBid.Text = "Bid Added Successfully !";
                    gvCurrentBids.DataBind();
                }
                ea.Dispose();
            }

gridview的Aspx代码及其数据源:

<asp:GridView ID="gvCurrentBids" runat="server" 
                    BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
                    CellPadding="3" DataSourceID="dsTotalBids" Width="100%">
                    <Columns>
                        <asp:BoundField DataField="biddername" HeaderText="biddername" 
                            SortExpression="biddername" />
                        <asp:BoundField DataField="bidprice" HeaderText="bidprice" 
                            SortExpression="bidprice" />
                        <asp:BoundField DataField="bidend" HeaderText="bidend" 
                            SortExpression="bidend" />
                    </Columns>
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" CssClass="HeaderFreez" Font-Bold="True" 
                        ForeColor="White" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                    <RowStyle ForeColor="#000066" />
                    <SelectedRowStyle BackColor="PeachPuff" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#00547E" />
                </asp:GridView>
                <asp:SqlDataSource ID="dsTotalBids" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EAuctionConnectionString %>" 
                    SelectCommand="SELECT [biddername], [bidprice], [bidend] FROM [Bid] WHERE (([isavailable] = @isavailable) AND ([prod] = @prod))">
                    <SelectParameters>
                        <asp:Parameter DefaultValue="true" Name="isavailable" Type="Boolean" />
                        <asp:QueryStringParameter Name="prod" QueryStringField="id" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>

我已经尝试在页面加载中调用grid的databind方法,但是在提交按钮单击后它无法自动更新

0 个答案:

没有答案