I've got LinkButtons in my ListView. When the specific ListViewItem's LinkButton is clicked, that LinkButton is disabled. The problem is that the LinkButton's text contains data that is updated by the click of the LinkButton itself.
At the moment the LinkButton is disabled, but the data in the LinkButton's text that was updated is not updated on the LinkButton, because to do that I have to Databind() the ListView and this resets all the LinkButtons in the ListView to be enabled again.
How can I solve this problem?
The datasource is updated in the ListView's ItemCommand and the LinkButtons are disabled in the LinkButton's OnClick event.
I'm updating the database in the ItemCommand, because the updating should happen when the LinkButtons are clicked.
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
If e.CommandName = "Agree" Then
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Using comm As New SqlCommand("UPDATE Table1 SET Agrees=Agrees+1 WHERE ID=@ID", conn)
comm.Parameters.AddWithValue("@ID", e.CommandArgument)
conn.Open()
comm.ExecuteNonQuery()
End Using
End Using
End If
If e.CommandName = "Disagree" Then
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Using comm As New SqlCommand("UPDATE Table1 SET Disagrees=Disagrees+1 WHERE ID=@ID", conn)
comm.Parameters.AddWithValue("@ID", e.CommandArgument)
conn.Open()
comm.ExecuteNonQuery()
End Using
End Using
End If
End Sub
The LinkButtons are disabled in their OnClick events.
Protected Sub LinkButton_Click(sender As Object, e As EventArgs)
Dim lb As LinkButton = DirectCast(sender, LinkButton)
If lb.ID = "Agree" Then
Dim lb2 As LinkButton = DirectCast(DirectCast(lb.NamingContainer, ListViewItem).FindControl("Disagree"), LinkButton)
lb.Enabled = False
lb2.Enabled = False
Else
Dim lb2 As LinkButton = DirectCast(DirectCast(lb.NamingContainer, ListViewItem).FindControl("Agree"), LinkButton)
lb.Enabled = False
lb2.Enabled = False
End If
End Sub
On RowDataBound event you can set the HyperLink. Enabled = true or false based on the text value retrieved from DB. In that case you can get both updated value and disable/enabled status on databind.
databindlistviewLinkButton
Kindly mark this post as "Answer", if it helped you.
What do you mean that I can set the LinkButton to be disabled "based on the text value retrieved from the DB"?
And also, I also want to disable the LinkButton in the ListViewDataItem that was clicked, so not all the LinkButtons in the ListView, only the one clicked.
Yes I got what you say. When you click a link button, value bound to its text property is get updated /Changed and Link button should get disabled showing the updated text value Right?
Tell me what should the the text it should show when it is enabled and what should be the text when it is disabled. hope I can show you some code to do that.
databindlistviewLinkButton
Kindly mark this post as "Answer", if it helped you.
Visitors of the website can vote to either Agree or Disagree. The LinkButtons' text contains data (an integer, the amount of votes) extracetd from an SQL Server Database. When you Agree, the amount of Agrees in the database increases by one and the specific
LinkButton's text should display this updated integer, and both LinkButtons should be disabled.
Best thing I can suggest is to add one more filed in table that indicates whether an user has voted or not. It may be a true/false field. When user clicks it also should be updated to true value. and in RowDataBound event you can check for this value
and set the Enabled property of Link buttons accordingly.
Kindly mark this post as "Answer", if it helped you.
frmeyer
Member
19 Points
67 Posts
How to Databind ListView without enabling LinkButtons
Apr 13, 2012 04:31 PM|LINK
Hi there
I've got LinkButtons in my ListView. When the specific ListViewItem's LinkButton is clicked, that LinkButton is disabled. The problem is that the LinkButton's text contains data that is updated by the click of the LinkButton itself.
At the moment the LinkButton is disabled, but the data in the LinkButton's text that was updated is not updated on the LinkButton, because to do that I have to Databind() the ListView and this resets all the LinkButtons in the ListView to be enabled again.
How can I solve this problem?
The datasource is updated in the ListView's ItemCommand and the LinkButtons are disabled in the LinkButton's OnClick event.
databind listview LinkButton
basheerkal
Star
10672 Points
2426 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 13, 2012 05:13 PM|LINK
Need more details to give you an answer..
Are You updating database in In ItemCommand? or using Update Button?
No problem even if you databind again. You can set enable/disable based on the text value while databinding.
Anyway show your updating code.
databind listview LinkButton
(Talk less..Work more)
frmeyer
Member
19 Points
67 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 13, 2012 05:25 PM|LINK
I'm updating the database in the ItemCommand, because the updating should happen when the LinkButtons are clicked.
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand If e.CommandName = "Agree" Then Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Using comm As New SqlCommand("UPDATE Table1 SET Agrees=Agrees+1 WHERE ID=@ID", conn) comm.Parameters.AddWithValue("@ID", e.CommandArgument) conn.Open() comm.ExecuteNonQuery() End Using End Using End If If e.CommandName = "Disagree" Then Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Using comm As New SqlCommand("UPDATE Table1 SET Disagrees=Disagrees+1 WHERE ID=@ID", conn) comm.Parameters.AddWithValue("@ID", e.CommandArgument) conn.Open() comm.ExecuteNonQuery() End Using End Using End If End SubThe LinkButtons are disabled in their OnClick events.
Protected Sub LinkButton_Click(sender As Object, e As EventArgs) Dim lb As LinkButton = DirectCast(sender, LinkButton) If lb.ID = "Agree" Then Dim lb2 As LinkButton = DirectCast(DirectCast(lb.NamingContainer, ListViewItem).FindControl("Disagree"), LinkButton) lb.Enabled = False lb2.Enabled = False Else Dim lb2 As LinkButton = DirectCast(DirectCast(lb.NamingContainer, ListViewItem).FindControl("Agree"), LinkButton) lb.Enabled = False lb2.Enabled = False End If End Subdatabind listview LinkButton
basheerkal
Star
10672 Points
2426 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 13, 2012 05:39 PM|LINK
Sorry, I cannot get your logic. Are there three LinkButtons in a row? which event calls sub LinkButton_Click ?can you please explain the situation?..
databind listview LinkButton
(Talk less..Work more)
frmeyer
Member
19 Points
67 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 13, 2012 07:22 PM|LINK
There are two LinkButtons in the ListView's ItemTemplate. The LinkButton_Click event is both LinkButtons' OnClick event.
databind listview LinkButton
basheerkal
Star
10672 Points
2426 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 14, 2012 02:56 AM|LINK
OK.
On RowDataBound event you can set the HyperLink. Enabled = true or false based on the text value retrieved from DB. In that case you can get both updated value and disable/enabled status on databind.
databind listview LinkButton
(Talk less..Work more)
frmeyer
Member
19 Points
67 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 14, 2012 04:09 PM|LINK
Sorry, I'm not sure what you mean.
What do you mean that I can set the LinkButton to be disabled "based on the text value retrieved from the DB"?
And also, I also want to disable the LinkButton in the ListViewDataItem that was clicked, so not all the LinkButtons in the ListView, only the one clicked.
databind listview LinkButton
basheerkal
Star
10672 Points
2426 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 14, 2012 04:34 PM|LINK
Yes I got what you say. When you click a link button, value bound to its text property is get updated /Changed and Link button should get disabled showing the updated text value Right?
Tell me what should the the text it should show when it is enabled and what should be the text when it is disabled. hope I can show you some code to do that.
databind listview LinkButton
(Talk less..Work more)
frmeyer
Member
19 Points
67 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 14, 2012 06:13 PM|LINK
Okay.
The LinkButtons:
<asp:LinkButton ID="Agree" runat="server" OnClick="LinkButton_Click" CommandName="Agree" CommandArgument='<%# Eval("ID") %>' >Agree(<%# Eval("Agrees")%>)</asp:LinkButton> <asp:LinkButton ID="Disagree" runat="server" OnClick="LinkButton_Click" CommandName="Disagree" CommandArgument='<%# Eval("ID") %>'>Meh...(<%# Eval("Disagrees")%>)</asp:LinkButton>Visitors of the website can vote to either Agree or Disagree. The LinkButtons' text contains data (an integer, the amount of votes) extracetd from an SQL Server Database. When you Agree, the amount of Agrees in the database increases by one and the specific LinkButton's text should display this updated integer, and both LinkButtons should be disabled.
Example:
Agree(0) Disagree(0)
If the Agree LinkButton is clicked:
Agree(1) Disagree(0) ----LinkButtons disabled
databind listview LinkButton
basheerkal
Star
10672 Points
2426 Posts
Re: How to Databind ListView without enabling LinkButtons
Apr 15, 2012 02:21 AM|LINK
Best thing I can suggest is to add one more filed in table that indicates whether an user has voted or not. It may be a true/false field. When user clicks it also should be updated to true value. and in RowDataBound event you can check for this value and set the Enabled property of Link buttons accordingly.
(Talk less..Work more)