ListView InsertItemPosition not changing on Databound event
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" >
c# asp.net listview
add a comment |
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" >
c# asp.net listview
add a comment |
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" >
c# asp.net listview
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
c# asp.net listview
edited Nov 22 '18 at 7:45
JustLearning
99321635
99321635
asked Nov 22 '18 at 4:05
Tim_007Tim_007
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Add your ListView
inside update panel
and then try to show/hide your InsertItemTemplate
.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Add your ListView
inside update panel
and then try to show/hide your InsertItemTemplate
.
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
add a comment |
Add your ListView
inside update panel
and then try to show/hide your InsertItemTemplate
.
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
add a comment |
Add your ListView
inside update panel
and then try to show/hide your InsertItemTemplate
.
Add your ListView
inside update panel
and then try to show/hide your InsertItemTemplate
.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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