ListView InsertItemPosition not changing on Databound event












0















I am using a ListView to show user info, stored in a table. I only want to show one record.



When the user first starts using the site, there will not be an initial record, so I need the InsertItemTemplate to be visible. Once a record is inserted, I need the InsertItemTemplate to disappear.



Based upon research, I have used the DataBound event to check for ListView.Item.Count and set the InsertItemPosition to either None or LastItem accordingly. I have also set a label on the page temporarily to display the results of the count.



The label is getting updated correctly. However, the InsertItemPosition is not changing until after a second postback occurs. So the result is the opposite of what it should be. It works correctly when the page loads, but then stays visible after I insert data, and then disappears if I delete the record. Any help would be appreciated. I have posted relevant code below.



protected void ListView1_DataBound(object sender, EventArgs e)
{
if (ListView1.Items.Count > 0)
{
lblRecordCount.Text = "DataBound - Records exist";
ListView1.InsertItemPosition = InsertItemPosition.None;

}
else
{
lblRecordCount.Text = "DataBound - No records exist";
ListView1.InsertItemPosition = InsertItemPosition.LastItem;


}

}





 Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
<br />

<asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >












share|improve this question





























    0















    I am using a ListView to show user info, stored in a table. I only want to show one record.



    When the user first starts using the site, there will not be an initial record, so I need the InsertItemTemplate to be visible. Once a record is inserted, I need the InsertItemTemplate to disappear.



    Based upon research, I have used the DataBound event to check for ListView.Item.Count and set the InsertItemPosition to either None or LastItem accordingly. I have also set a label on the page temporarily to display the results of the count.



    The label is getting updated correctly. However, the InsertItemPosition is not changing until after a second postback occurs. So the result is the opposite of what it should be. It works correctly when the page loads, but then stays visible after I insert data, and then disappears if I delete the record. Any help would be appreciated. I have posted relevant code below.



    protected void ListView1_DataBound(object sender, EventArgs e)
    {
    if (ListView1.Items.Count > 0)
    {
    lblRecordCount.Text = "DataBound - Records exist";
    ListView1.InsertItemPosition = InsertItemPosition.None;

    }
    else
    {
    lblRecordCount.Text = "DataBound - No records exist";
    ListView1.InsertItemPosition = InsertItemPosition.LastItem;


    }

    }





     Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
    <br />

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >












    share|improve this question



























      0












      0








      0








      I am using a ListView to show user info, stored in a table. I only want to show one record.



      When the user first starts using the site, there will not be an initial record, so I need the InsertItemTemplate to be visible. Once a record is inserted, I need the InsertItemTemplate to disappear.



      Based upon research, I have used the DataBound event to check for ListView.Item.Count and set the InsertItemPosition to either None or LastItem accordingly. I have also set a label on the page temporarily to display the results of the count.



      The label is getting updated correctly. However, the InsertItemPosition is not changing until after a second postback occurs. So the result is the opposite of what it should be. It works correctly when the page loads, but then stays visible after I insert data, and then disappears if I delete the record. Any help would be appreciated. I have posted relevant code below.



      protected void ListView1_DataBound(object sender, EventArgs e)
      {
      if (ListView1.Items.Count > 0)
      {
      lblRecordCount.Text = "DataBound - Records exist";
      ListView1.InsertItemPosition = InsertItemPosition.None;

      }
      else
      {
      lblRecordCount.Text = "DataBound - No records exist";
      ListView1.InsertItemPosition = InsertItemPosition.LastItem;


      }

      }





       Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
      <br />

      <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >












      share|improve this question
















      I am using a ListView to show user info, stored in a table. I only want to show one record.



      When the user first starts using the site, there will not be an initial record, so I need the InsertItemTemplate to be visible. Once a record is inserted, I need the InsertItemTemplate to disappear.



      Based upon research, I have used the DataBound event to check for ListView.Item.Count and set the InsertItemPosition to either None or LastItem accordingly. I have also set a label on the page temporarily to display the results of the count.



      The label is getting updated correctly. However, the InsertItemPosition is not changing until after a second postback occurs. So the result is the opposite of what it should be. It works correctly when the page loads, but then stays visible after I insert data, and then disappears if I delete the record. Any help would be appreciated. I have posted relevant code below.



      protected void ListView1_DataBound(object sender, EventArgs e)
      {
      if (ListView1.Items.Count > 0)
      {
      lblRecordCount.Text = "DataBound - Records exist";
      ListView1.InsertItemPosition = InsertItemPosition.None;

      }
      else
      {
      lblRecordCount.Text = "DataBound - No records exist";
      ListView1.InsertItemPosition = InsertItemPosition.LastItem;


      }

      }





       Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
      <br />

      <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >








       Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
      <br />

      <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >





       Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
      <br />

      <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >






      c# asp.net listview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 7:45









      JustLearning

      99321635




      99321635










      asked Nov 22 '18 at 4:05









      Tim_007Tim_007

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Add your ListView inside update panel and then try to show/hide your InsertItemTemplate.






          share|improve this answer


























          • Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

            – Tim_007
            Nov 23 '18 at 3:47











          • However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

            – Tim_007
            Nov 23 '18 at 3:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423730%2flistview-insertitemposition-not-changing-on-databound-event%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Add your ListView inside update panel and then try to show/hide your InsertItemTemplate.






          share|improve this answer


























          • Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

            – Tim_007
            Nov 23 '18 at 3:47











          • However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

            – Tim_007
            Nov 23 '18 at 3:47
















          0














          Add your ListView inside update panel and then try to show/hide your InsertItemTemplate.






          share|improve this answer


























          • Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

            – Tim_007
            Nov 23 '18 at 3:47











          • However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

            – Tim_007
            Nov 23 '18 at 3:47














          0












          0








          0







          Add your ListView inside update panel and then try to show/hide your InsertItemTemplate.






          share|improve this answer















          Add your ListView inside update panel and then try to show/hide your InsertItemTemplate.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 7:17









          JustLearning

          99321635




          99321635










          answered Nov 22 '18 at 6:29









          A.M. PatelA.M. Patel

          629




          629













          • Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

            – Tim_007
            Nov 23 '18 at 3:47











          • However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

            – Tim_007
            Nov 23 '18 at 3:47



















          • Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

            – Tim_007
            Nov 23 '18 at 3:47











          • However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

            – Tim_007
            Nov 23 '18 at 3:47

















          Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

          – Tim_007
          Nov 23 '18 at 3:47





          Thank you for the suggestion. Unfortunately, I ran into a similar issue where it would work initially, then not work. I believe there may be an error with my logic when I delete the one existing row. My assumption is that the listview does not execute the databound event if the datasource is empty or there is a timing issue.

          – Tim_007
          Nov 23 '18 at 3:47













          However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

          – Tim_007
          Nov 23 '18 at 3:47





          However, your recommendation helped me come up with a backdoor solution. I did use a updatepanel and 2 separate listviews, each nested in a division. I toggle between the divisions depending if the listview is empty or not. For some reason, the timing on making the div visible or not is accurate, similiar to the label, even though the InsertItemPosition did not get updated properly using this same method.

          – Tim_007
          Nov 23 '18 at 3:47


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423730%2flistview-insertitemposition-not-changing-on-databound-event%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen