How to refresh GridView after data insert?
I have a GridView and when some data has been inserted I have called the DataBind() method on the GridView and the ObjectDataSource the GridView is bound to, but the GridView does not display the new data. How do I get it to refresh?
Here's my ASP.NET code:
<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" />
Here's my code-behind to handle the click events:
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.Vi开发者_如何学编程sible = 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();
}
Do I need to call DataBind() on both objects? What have I done wrong here?
Thanks!
In your method btnInsertOrderSignal_Click you are adding a new item and databinding again. This is correct but you are not updating your datasource before databinding(). This means that it will bind to the original datasource.
It seems that what populates your CustomControl needs to be called again. GetOrderSignals()?
精彩评论