Insert of DetailsView does not refresh the page as desired.

Last post 07-05-2009 12:13 PM by zipswich. 8 replies.

Sort Posts:

  • Insert of DetailsView does not refresh the page as desired.

    07-04-2009, 5:52 PM
    • Member
      75 point Member
    • zipswich
    • Member since 01-03-2008, 3:09 PM
    • Posts 157

    I have a DetailsView just for inserting data.  Everything works fine the first time the page is displayed.  The Insert function works - data are inserted to a data table properly.

    However, after insertion, a field does not show the text provided by code-behind even though the code is executed.  If I use the refresh function of the brower, the default text will show and everything will be fine. 

    Could anyone offer some hint about how to refresh the page automatically after insert?

    Thanks in advance!

    Hong
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-04-2009, 7:17 PM
    • All-Star
      26,006 point All-Star
    • PeteNet
    • Member since 01-21-2009, 6:15 PM
    • Posts 3,686

    can you post your code?

    Regards,
    Peter
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-04-2009, 7:53 PM
    • Member
      75 point Member
    • zipswich
    • Member since 01-03-2008, 3:09 PM
    • Posts 157

    Thanks, Peter, for willing to look into this. I posted code as following.Does the insert action cause page refreshing? I guess I can find out by using a very simple DetailsView to insert a record to see if a TextBox displaying the current time refreshed.

     
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="Ticker,LogDate" 
                    DataSourceID="SqlDataSource1">
                    <ItemTemplate>
                        <tr style="background-color:#DCDCDC;color: #000000;">
                            <td>
                                <asp:Label ID="TickerLabel" runat="server" Text='<%# Eval("Ticker") %>' />
                            </td>
                            <td>
                                <asp:Label ID="LogDateLabel" runat="server" Text='<%# Eval("LogDate") %>' />
                            </td>
                            <td>
                                <asp:Label ID="CommentsLabel" runat="server" Text='<%# Eval("Comments") %>' />
                            </td>
                            <td>
                                <asp:Label ID="TargetLabel" runat="server" Text='<%# Eval("Target") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <AlternatingItemTemplate>
                        <tr style="background-color:#FFF8DC;">
                            <td>
                                <asp:Label ID="TickerLabel" runat="server" Text='<%# Eval("Ticker") %>' />
                            </td>
                            <td>
                                <asp:Label ID="LogDateLabel" runat="server" Text='<%# Eval("LogDate") %>' />
                            </td>
                            <td>
                                <asp:Label ID="CommentsLabel" runat="server" Text='<%# Eval("Comments") %>' />
                            </td>
                            <td>
                                <asp:Label ID="TargetLabel" runat="server" Text='<%# Eval("Target") %>' />
                            </td>
                        </tr>
                    </AlternatingItemTemplate>
                    <EmptyDataTemplate>
                        <table runat="server" 
                            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                            <tr>
                                <td>
                                    No data was returned.</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                    <InsertItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                                    Text="Insert" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                                    Text="Clear" />
                            </td>
                            <td>
                                <asp:TextBox ID="TickerTextBox" runat="server" Text='<%# Bind("Ticker") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="LogDateTextBox" runat="server" Text='<%# Bind("LogDate") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="CommentsTextBox" runat="server" 
                                    Text='<%# Bind("Comments") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="TargetTextBox" runat="server" Text='<%# Bind("Target") %>' />
                            </td>
                        </tr>
                    </InsertItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table ID="itemPlaceholderContainer" runat="server" border="1" 
                                        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                        <tr runat="server" style="background-color:#DCDCDC;color: #000000;">
                                            <th runat="server">
                                                Ticker</th>
                                            <th runat="server">
                                                LogDate</th>
                                            <th runat="server">
                                                Comments</th>
                                            <th runat="server">
                                                Target</th>
                                        </tr>
                                        <tr ID="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" 
                                    style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                                    <asp:DataPager ID="DataPager1" runat="server" PageSize="30">
                                        <Fields>
                                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                                ShowLastPageButton="True" />
                                        </Fields>
                                    </asp:DataPager>
                                </td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                    <EditItemTemplate>
                        <tr style="background-color:#008A8C;color: #FFFFFF;">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                                    Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                                    Text="Cancel" />
                            </td>
                            <td>
                                <asp:Label ID="TickerLabel1" runat="server" Text='<%# Eval("Ticker") %>' />
                            </td>
                            <td>
                                <asp:Label ID="LogDateLabel1" runat="server" Text='<%# Eval("LogDate") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="CommentsTextBox" runat="server" 
                                    Text='<%# Bind("Comments") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="TargetTextBox" runat="server" Text='<%# Bind("Target") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <SelectedItemTemplate>
                        <tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
                            <td>
                                <asp:Label ID="TickerLabel" runat="server" Text='<%# Eval("Ticker") %>' />
                            </td>
                            <td>
                                <asp:Label ID="LogDateLabel" runat="server" Text='<%# Eval("LogDate") %>' />
                            </td>
                            <td>
                                <asp:Label ID="CommentsLabel" runat="server" Text='<%# Eval("Comments") %>' />
                            </td>
                            <td>
                                <asp:Label ID="TargetLabel" runat="server" Text='<%# Eval("Target") %>' />
                            </td>
                        </tr>
                    </SelectedItemTemplate>
                </asp:ListView>


     

    Hong
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-04-2009, 10:26 PM
    • Member
      75 point Member
    • zipswich
    • Member since 01-03-2008, 3:09 PM
    • Posts 157

     Here is what I have found by creating a simple DetailsView and I am puzzled by it:

    The insert action does cause the update of a text control that simply displays the current time. Howerver, a ListView that displays the records of the corresponding data table does not show the inserted records at all. In other words, the ListView is not refreshed. As shown in the above code, the TextBox TemplateField is supposed to display the current date, it becomes blank after the insert action. If I click the browser's refresh button, everything will look fine.

    I have just added a GridView that does almost exactly the same as the ListView and it refreshes itself after insertion showing the new recrod, but the ListView does not change at all.

    Hong
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-05-2009, 12:31 AM
    • All-Star
      26,006 point All-Star
    • PeteNet
    • Member since 01-21-2009, 6:15 PM
    • Posts 3,686

    Hey zipswich,

    sorry I couldn't get back to you earlier ...we're celebrating Independence Day here!

    alright, I see that you're not using the InsertItemPosition property on your ListView (documentation); it can be set to either of these values: FirstItem,LastItem,None. I tried out a quick sample and yes it does show the inserted record (it is refreshed). I checked your markup again and don't see anything else different or out of place. try to set the InsertItemPosition and report back....I don't see any of the parameters in the code that you've posted so I'm assuming that you're handling the ItemCommand with those buttons...so, is there anything else that you're setting any other properties in the codebehind that we should know?

    quick note about detailsview ...it does refresh and goes to the first record in the table (with/without AllowPaging="true"

    Regards,
    Peter
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-05-2009, 7:29 AM
    • Member
      75 point Member
    • zipswich
    • Member since 01-03-2008, 3:09 PM
    • Posts 157

     Happy Independence Day! Hope you enjoyed spectacular fireworks. We do here around Boston too.

    I am very sorry but I have just noticed that I missed the code of the DetailsView that I was talking about. Actually I copied a wrong piece of code.

    I am trying to use DetailsView to insert data and use ListView to show the records with paging.  Let me clarify the problems that I am confronting:

    1. After insertion with the DetailView, the ListView does not refresh to show the inserted record even though the insertion causes postback.

    2.After insertion, a TemplateField ("LogDate") of the DetailsView is not populated by code behind even though debugging confirms the code is executed.

     

     

     

                <asp:DetailsView ID="DetailsView1" runat="server" 
            AutoGenerateRows="False" Caption="Stock Notes" CaptionAlign="Top" 
            CellPadding="4" DataKeyNames="Ticker,LogDate" DataSourceID="Finance" 
            DefaultMode="Insert" ForeColor="#333333" GridLines="None" Height="50px" 
            Width="451px">
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <Fields>
                        <asp:BoundField DataField="Ticker" HeaderText="Ticker" ReadOnly="True" 
                    SortExpression="Ticker" />
                        <asp:TemplateField HeaderText="LogDate" SortExpression="LogDate">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("LogDate") %>'></asp:Label>
                            </EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="tbLogDate" runat="server" Text='<%# Bind("LogDate") %>'></asp:TextBox>
                                <cc1:CalendarExtender ID="tbLogDate_CalendarExtender" runat="server" 
                                    Enabled="True" TargetControlID="tbLogDate">
                                </cc1:CalendarExtender>
                            </InsertItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("LogDate") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Comments" HeaderText="Comments" 
                    SortExpression="Comments">
                        </asp:BoundField>
                        <asp:BoundField DataField="Target" HeaderText="Target" 
                    SortExpression="Target" />
                        <asp:CommandField ShowInsertButton="True" />
                    </Fields>
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                </asp:DetailsView>
    


     

    Hong
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-05-2009, 10:07 AM
    • Member
      75 point Member
    • zipswich
    • Member since 01-03-2008, 3:09 PM
    • Posts 157

    Now I have solved the problem of not showing the text generated by code-behind in a TemplateField of the DetailsView. I placed the code in Page_Load() and it worked only for the first time of displaying the page. Now I place the code in the PreRender function of the DetailsView and it works fine now. 

    I still do not know how to refresh the ListView after each postback.

    Hong
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-05-2009, 12:03 PM
    Answer
    • All-Star
      26,006 point All-Star
    • PeteNet
    • Member since 01-21-2009, 6:15 PM
    • Posts 3,686

    thanks for the wishes, we did have a great time..hope you did too

    try this: set up the ItemInserted event of the DetailsView and do a DataBind on the ListView there

    Regards,
    Peter
  • Re: Insert of DetailsView does not refresh the page as desired.

    07-05-2009, 12:13 PM
    • Member
      75 point Member
    • zipswich
    • Member since 01-03-2008, 3:09 PM
    • Posts 157

     Thanks, Peter.

    The DataBind() works in refreshing the ListView.  I put the code in PreRender() which is already there.  I believe using ItemInserted event handler would work too.

    Hong
Page 1 of 1 (9 items)