Telerik grid with a JavaScript confirm box to update a column
I have a Telerik grid. For each row there is a details table. The row is of type NominationTypeClass
and the rows in the details table is of type Nomination
. So what this means for each nomination type there is a list of nominations. The grid's code:
<telerik:RadGrid
AllowPaging="true"
AllowSorting="true"
AutoGenerateColumns="false"
GridLines="None"
ID="rgMyNominations"
OnDetailTableDataBind="rgMyNominations_DetailTableDataBind"
OnItemDataBound="rgMyNominations_ItemDataBound"
OnNeedDataSource="rgMyNominations_NeedDataSource"
OnUpdateCommand="rgMyNominations_UpdateCommand"
PageSize="5"
runat="server"
ShowHeader="false"
ShowStatusBar="true">
<MasterTableView DataKeyNames="NominationTypeID" HierarchyDefaultExpanded="true" Width="100%">
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<b><asp:Label ID="lblNominationType" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.NominationType") %>' /></b>
</ItemTemplate>
<ItemStyle Width="100%" />
</telerik:GridTemplateColumn>
</Columns>
<NoRecordsTemplate>No Nomination Types.</NoRecordsTemplate>
<DetailTables>
<telerik:GridTableView PageSize="5" Name="Nominations" GridLines="None" Width="100%" ShowHeader="true" DataKeyNames="NominationID">
<Columns>
<telerik:GridTemplateColumn HeaderText="Person / Team">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# GetName(DataBinder.Eval(Container, "DataItem")) %>' />
</ItemTemplate>
<ItemStyle VerticalAlign="Top" Width="20%" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Date Nominated">
<ItemTemplate>
<asp:Label ID="lblNominationDate" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container, "DataItem.NominationDate")) %>' />
</ItemTemplate>
<ItemStyle VerticalAlign="Top" Width="14%" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Action" UniqueName="Action_Column">
<ItemTemplate>
<b><asp:HyperLink ID="hlEdit" runat="server" Text="Edit" /></b><br />
<b>
<asp:LinkButton
CausesValidation="false"
CommandName="Update"
ID="lbWithdrawnStatus"
runat="server"
Text="Withdraw"
OnClientClick="javascript:return ConfirmWithdrawnStatusChange();" />
</b>
</ItemTemplate>
<ItemStyle VerticalAlign="Top" Width="7%" />
</telerik:GridTemplateColumn>
</Columns>
<NoRecordsTemplate>No Nominations.</NoRecordsTemplate>
</telerik:GridTableView>
</DetailTables>
</MasterTableView>
<ClientSettings AllowExpandCollapse="true"></ClientSettings>
</telerik:RadGrid>
Here is how I populate my rows:
protected void rgMyNominations_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
try
{
if (!e.IsFromDetailTable)
{
rgMyNominations.DataSource = GetNominationTypes();
}
}
catch (Exception ex)
{
// Handle exceptions
}
}
Here is how I populate my details table:
protected void rgMyNominations_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
{
try
{
GridDataItem gridDataItem = (GridDataItem)e.DetailTableView.ParentItem;
if (e.DetailTableView.Name == "Nominations")
{
int nominationTypeID = int.Parse(gridDataItem.GetDataKeyValue("NominationTypeID").ToString());
List<Nomination> nominations = new Lis开发者_如何学Pythont<Nomination>();
// For each nomination type, add the nomination
foreach (Nomination n in myNominationsList)
{
if (n.NominationType.NominationTypeID == nominationTypeID)
{
nominations.Add(n);
}
}
e.DetailTableView.DataSource = nominations;
}
}
catch (Exception ex)
{
// Handle exceptions
}
}
I have an action column that has a link that says Withdrawn. When clicked I have a JavaScript confirm box
with a Yes or No option. If yes, then the nomination status is updated to withdrawn
. Then I want the grid to be refreshed to show the updated status. I used the grid's update command to to show the show the JavaScript's command box. It updates, but is it the correct way to do it?
protected void rgMyNominations_UpdateCommand(object source, GridCommandEventArgs e)
{
try
{
StatusManager.InsertStatus( /* required parameters */ );
// Refresh grid
rgMyNominations.DataSource = GetNominationTypes();
rgMyNominations.DataBind();
}
catch (Exception ex)
{
// Handle exceptions
}
}
The binding of the grid doesn't want to work properly after the status was updated. The grid row is of type NominationTypeClass
and the details table is of type Nomination
. I debugged, and there where it set the datasource for each it is correct, but when the view is rendered for:
<asp:Label ID="lblNominationDate" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container, "DataItem.NominationDate")) %>' />
...it says that NominationDate is not a property of NominationTypeClass
. This is wrong, I don't know why it is taking the type for the row to be the type of the details table? NominationDate is a property of Nomination. It seems like it is overriding the datasources.
Are there any online samples of what I am trying to accomplish? Any advice would be appreciated.
One thing that sticks out a bit, and I'm not sure this is where the issue is coming from, is that you specifically set the DataSource and call DataBind() within the UpdateCommand event of the RadGrid.
First of all, when refreshing the RadGrid (if you're still binding to the same source) you should only need to call the .Rebind() function, instead of setting the data source and calling .Databind()
Secondly, the OnUpdateCommand should call .Rebind() without you having to specifically call it, and using that (or the method you use above) could lead to various odd issues. This could potentially be the source.
Aside from that, have you tried submitting a support ticket over at Telerik? I believe that their support team will be your best bet at handling this issue.
精彩评论