By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

HELP! Listview not working with edit button ASP.NET

P: 4
Posted this on Asp.Net, seems to be a hard problem, I still can't work it out

I have a edit button, this works fine, until I add the code that prevents the edit button from being seen by users who did not post that ticket. I cannot understand why this is happening. I get an error on the button says 'object reference not set in instance of object'



It happens when I add the following code:


Expand|Select|Wrap|Line Numbers
  1. protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
  2.     {
  3.         HiddenField commentidfield = e.Item.FindControl("commentidfield") as HiddenField;
  4.         HiddenField userloginfield = e.Item.FindControl("userloginfield") as HiddenField;
  5.  
  6.         ImageButton votebutton = e.Item.FindControl("commentvotebutton") as ImageButton;
  7.         ImageButton editbutton = e.Item.FindControl("editbutton") as ImageButton;
  8.  
  9.  
  10.  
  11.         if (HttpContext.Current.User.Identity.IsAuthenticated == true)
  12.         {
  13.             int commentidtemp = Convert.ToInt32(commentidfield.Value);
  14.             int userloginid = Convert.ToInt32(userloginfield.Value);
  15.  
  16.             int commentcheck = Convert.ToInt32(commentratingclass.CheckCommentVote(Profile.userid, commentidtemp).GetValueOrDefault(0));
  17.             int editcheck = Convert.ToInt32(commentclass.checkcommentbyuserid(Profile.userid, commentidtemp).GetValueOrDefault(0));
  18.  
  19.  
  20.             if (userloginid == Profile.userid)
  21.             {
  22.                 votebutton.Visible = false;
  23.             }
  24.             else if (commentcheck > 0)
  25.             {
  26.                 votebutton.ImageUrl = "Images/commentvoted.png";
  27.                 votebutton.Enabled = false;
  28.             }
  29.             else
  30.             {
  31.                 votebutton.ImageUrl = "Images/commentvote.png";
  32.                 votebutton.Enabled = true;
  33.             }
  34.  
  35.             if (editcheck == 0)
  36.             {
  37.                 editbutton.Visible = false;
  38.             }
  39.             else
  40.             {
  41.                 editbutton.Visible = true;
  42.             }
  43.         }
  44.         else
  45.         {
  46.             votebutton.Visible = false;
  47.             editbutton.Visible = false;
  48.         }
  49.  
  50.  
  51.     }
  52.  
  53.  
  54.  
Listview:

Expand|Select|Wrap|Line Numbers
  1.  
  2. 1    <asp:ListView ID="ListView1" runat="server" DataKeyNames="CommentID" DataSourceID="SqlDataSource1"
  3. 2                    OnItemDataBound="ListView1_ItemDataBound" 
  4. 3                    onitemupdating="ListView1_ItemUpdating">
  5. 4                    <LayoutTemplate>
  6. 5                        <table runat="server">
  7. 6                            <tr runat="server">
  8. 7                                <td runat="server">
  9. 8                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
  10. 9                                        <tr id="itemPlaceholder" runat="server">
  11. 10                                       </tr>
  12. 11                                   </table>
  13. 12                               </td>
  14. 13                           </tr>
  15. 14                           <tr runat="server">
  16. 15                               <td runat="server" style="">
  17. 16                                   <center>
  18. 17                                       <asp:DataPager ID="DataPager1" runat="server">
  19. 18                                           <Fields>
  20. 19                                               <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False"
  21. 20                                                   ShowPreviousPageButton="False" />
  22. 21                                               <asp:NumericPagerField />
  23. 22                                               <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False"
  24. 23                                                   ShowPreviousPageButton="False" />
  25. 24                                           </Fields>
  26. 25                                       </asp:DataPager>
  27. 26                                   </center>
  28. 27                               </td>
  29. 28                           </tr>
  30. 29                       </table>
  31. 30                   </LayoutTemplate>
  32. 31                   <EmptyDataTemplate>
  33. 32                       <table runat="server" style="">
  34. 33                           <tr>
  35. 34                               <td>
  36. 35                                   No Answers! Add one Now!
  37. 36                               </td>
  38. 37                           </tr>
  39. 38                       </table>
  40. 39                   </EmptyDataTemplate>
  41. 40                   <EditItemTemplate>
  42. 41                    <table class="comments">
  43. 42                           <tr>
  44. 43                               <td class="commentsheader" rowspan="2">
  45. 44                                   <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("Login") %>' />
  46. 45                                   answered:
  47. 46                                   <div id="details">
  48. 47                                       <br />
  49. 48                                       Rating:
  50. 49                                       <asp:Label ID="Usersratinglabel" runat="server" Text='<%#  Eval("Rating") %>' />
  51. 50                                       <br />
  52. 51                                       Posts:
  53. 52                                         <asp:Label ID="Usersanswers" runat="server" Text='<%#  Eval("Answers") %>' />
  54. 53                                       <asp:HiddenField ID="commentidfield" runat="server" Value='<%# Eval("CommentID") %>' />
  55. 54                                       <asp:HiddenField ID="userloginfield" runat="server" Value='<%# Eval("UserID") %>' />
  56. 55                               </td>
  57. 56                               <td class="commentsmain">
  58. 57                                   <asp:TextBox ID="updatetext" runat="server" Text='<%# Bind("Comment") %>' TextMode="MultiLine"
  59. 58                                       Width="500px" Height="150px" />
  60. 59                               </td>
  61. 60                           </tr>
  62. 61                           <tr>
  63. 62                               <td class="commentfooter">
  64. 63                                   <div class="commentfooter">
  65. 64                                       <asp:Button ID="updatebutton" runat="server" Text="Update"  CommandName="Update" />
  66. 65                                       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
  67. 66                               </td>
  68. 67                           </tr>
  69. 68                           </td> </tr>
  70. 69                       </table>
  71. 70                   </EditItemTemplate>
  72. 71                   <ItemTemplate>
  73. 72                       <table class="comments">
  74. 73                           <tr>
  75. 74                               <td class="commentsheader" rowspan="2">
  76. 75                                   <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("Login") %>' />
  77. 76                                   answered:
  78. 77                                   <div id="details">
  79. 78                                       <br />
  80. 79                                       Rating:
  81. 80                                       <asp:Label ID="Usersratinglabel" runat="server" Text='<%#  Eval("Rating") %>' />
  82. 81                                       <br />
  83. 82                                       Posts:
  84. 83                                       <asp:Label ID="Usersanswers" runat="server" Text='<%#  Eval("Answers") %>' />
  85. 84                                       <asp:HiddenField ID="commentidfield" runat="server" Value='<%# Eval("CommentID") %>' />
  86. 85                                       <asp:HiddenField ID="userloginfield" runat="server" Value='<%# Eval("UserID") %>' />
  87. 86                               </td>
  88. 87                               <td class="commentsmain">
  89. 88                                   <asp:Label ID="CommentLabel" runat="server" Text='<%# Eval("Comment") %>' />
  90. 89                               </td>
  91. 90                           </tr>
  92. 91                           <tr>
  93. 92                               <td class="commentfooter">
  94. 93                                   <asp:ImageButton ID="editbutton" CommandName="Edit" runat="server" ImageUrl="Images/commentedit.png" />
  95. 94                                   <asp:ImageButton ID="commentvotebutton" runat="server" OnCommand="commentvote_Click" ImageUrl="Images/commentvote.png"
  96. 95                                       CommandArgument='<%# Eval("CommentID") %>' CommandName="commentvote" />
  97. 96                               </td>
  98. 97                           </tr>
  99. 98                           </td> </tr>
  100. 99                       </table>
  101. 100                  </ItemTemplate>
  102. 101              </asp:ListView>
  103.  
  104.  
  105.  
Mar 20 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.