数据插入后如何刷新GridView?

时间:2010-11-09 09:20:53

标签: c# asp.net data-binding gridview c#-4.0

我有一个GridView,当插入一些数据时,我在GridView上调用了DataBind()方法,并且GridView绑定了ObjectDataSource,但GridView不显示新数据。我如何让它刷新?

这是我的ASP.NET代码:

<asp:GridView 
    ID="grdOrderSignals" 
    runat="server" 
    DataSourceID="srcOrderSignals" 
    DataKeyNames="signal"
    EmptyDataText="There are no signals for this order"
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="signal" HeaderText="Signal Id"  ReadOnly="True" />
        <asp:BoundField DataField="description" HeaderText="Description"  ReadOnly="True" />
        <CustomControls:DeleteButtonField ConfirmText="Delete this order signal?" Text="Del" />
    </Columns>
</asp:GridView>  

<CustomControls:CustomObjectDataSource
    id="srcOrderSignals" 
    TypeName="DatabaseComponent.DBUtil" 
    SelectMethod="GetOrderSignals" 
    DeleteMethod="DeleteOrderSignal" 
    EnableCaching="false"

    runat="server">
    <SelectParameters>
        <asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedValue" />
    </SelectParameters> 
    <DeleteParameters>
        <asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedValue" />        
        <asp:ControlParameter Name="signal" ControlID="grdOrderSignals" PropertyName="SelectedValue" />                    
    </DeleteParameters>           
</CustomControls:CustomObjectDataSource> 

<asp:Button ID="btnAddOrderSignal" runat="server" Text="Add Order Signal" OnClick="btnAddOrderSignal_Click" />
<asp:DropDownList ID="ddlSignals" runat="server" Visible="false" />
<asp:Button ID="btnInsertOrderSignal" runat="server" Text="Add" Visible="false" OnClick="btnInsertOrderSignal_Click" />

这是我处理点击事件的代码隐藏:

protected void btnAddOrderSignal_Click(object sender, EventArgs e)
{
    FillSignals();
    btnAddOrderSignal.Visible = false;
    ddlSignals.Visible = true;
    btnInsertOrderSignal.Visible = true;

}

protected void btnInsertOrderSignal_Click(object sender, EventArgs e)
{
    InsertOrderSignal();
    btnAddOrderSignal.Visible = true;
    ddlSignals.Visible = false;
    btnInsertOrderSignal.Visible = false;
    srcOrderSignals.DataBind();
    grdOrderSignals.DataBind();      
}

protected void InsertOrderSignal()
{
    DBUtil DB = new DBUtil();
    int orderId = (int)grdOrders.SelectedValue;
    string signalId = ddlSignals.SelectedValue;
    DB.InsertOrderSignal(orderId, signalId);
}

protected void FillSignals()
{
    DBUtil DB = new DBUtil();
    DataTable od = DB.GetOrderDetails((int)grdOrders.SelectedValue);
    int strategyId = (int)od.Rows[0]["strategyId"];

    ddlSignals.DataSource = DB.GetSignals(strategyId);
    ddlSignals.DataTextField = "signalId";
    ddlSignals.DataValueField = "signalId";
    ddlSignals.DataBind();      
}

我是否需要在两个对象上调用DataBind()?我在这做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:6)

在您的方法btnInsertOrderSignal_Click中,您将再次添加新项目和数据绑定。这是正确的,但您没有在数据绑定()之前更新数据源。这意味着它将绑定到原始数​​据源。

似乎需要再次调用填充CustomControl的内容。 GetOrderSignals()?