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
- protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
- {
- HiddenField commentidfield = e.Item.FindControl("commentidfield") as HiddenField;
- HiddenField userloginfield = e.Item.FindControl("userloginfield") as HiddenField;
- ImageButton votebutton = e.Item.FindControl("commentvotebutton") as ImageButton;
- ImageButton editbutton = e.Item.FindControl("editbutton") as ImageButton;
- if (HttpContext.Current.User.Identity.IsAuthenticated == true)
- {
- int commentidtemp = Convert.ToInt32(commentidfield.Value);
- int userloginid = Convert.ToInt32(userloginfield.Value);
- int commentcheck = Convert.ToInt32(commentratingclass.CheckCommentVote(Profile.userid, commentidtemp).GetValueOrDefault(0));
- int editcheck = Convert.ToInt32(commentclass.checkcommentbyuserid(Profile.userid, commentidtemp).GetValueOrDefault(0));
- if (userloginid == Profile.userid)
- {
- votebutton.Visible = false;
- }
- else if (commentcheck > 0)
- {
- votebutton.ImageUrl = "Images/commentvoted.png";
- votebutton.Enabled = false;
- }
- else
- {
- votebutton.ImageUrl = "Images/commentvote.png";
- votebutton.Enabled = true;
- }
- if (editcheck == 0)
- {
- editbutton.Visible = false;
- }
- else
- {
- editbutton.Visible = true;
- }
- }
- else
- {
- votebutton.Visible = false;
- editbutton.Visible = false;
- }
- }
Expand|Select|Wrap|Line Numbers
- 1 <asp:ListView ID="ListView1" runat="server" DataKeyNames="CommentID" DataSourceID="SqlDataSource1"
- 2 OnItemDataBound="ListView1_ItemDataBound"
- 3 onitemupdating="ListView1_ItemUpdating">
- 4 <LayoutTemplate>
- 5 <table runat="server">
- 6 <tr runat="server">
- 7 <td runat="server">
- 8 <table id="itemPlaceholderContainer" runat="server" border="0" style="">
- 9 <tr id="itemPlaceholder" runat="server">
- 10 </tr>
- 11 </table>
- 12 </td>
- 13 </tr>
- 14 <tr runat="server">
- 15 <td runat="server" style="">
- 16 <center>
- 17 <asp:DataPager ID="DataPager1" runat="server">
- 18 <Fields>
- 19 <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False"
- 20 ShowPreviousPageButton="False" />
- 21 <asp:NumericPagerField />
- 22 <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False"
- 23 ShowPreviousPageButton="False" />
- 24 </Fields>
- 25 </asp:DataPager>
- 26 </center>
- 27 </td>
- 28 </tr>
- 29 </table>
- 30 </LayoutTemplate>
- 31 <EmptyDataTemplate>
- 32 <table runat="server" style="">
- 33 <tr>
- 34 <td>
- 35 No Answers! Add one Now!
- 36 </td>
- 37 </tr>
- 38 </table>
- 39 </EmptyDataTemplate>
- 40 <EditItemTemplate>
- 41 <table class="comments">
- 42 <tr>
- 43 <td class="commentsheader" rowspan="2">
- 44 <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("Login") %>' />
- 45 answered:
- 46 <div id="details">
- 47 <br />
- 48 Rating:
- 49 <asp:Label ID="Usersratinglabel" runat="server" Text='<%# Eval("Rating") %>' />
- 50 <br />
- 51 Posts:
- 52 <asp:Label ID="Usersanswers" runat="server" Text='<%# Eval("Answers") %>' />
- 53 <asp:HiddenField ID="commentidfield" runat="server" Value='<%# Eval("CommentID") %>' />
- 54 <asp:HiddenField ID="userloginfield" runat="server" Value='<%# Eval("UserID") %>' />
- 55 </td>
- 56 <td class="commentsmain">
- 57 <asp:TextBox ID="updatetext" runat="server" Text='<%# Bind("Comment") %>' TextMode="MultiLine"
- 58 Width="500px" Height="150px" />
- 59 </td>
- 60 </tr>
- 61 <tr>
- 62 <td class="commentfooter">
- 63 <div class="commentfooter">
- 64 <asp:Button ID="updatebutton" runat="server" Text="Update" CommandName="Update" />
- 65 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
- 66 </td>
- 67 </tr>
- 68 </td> </tr>
- 69 </table>
- 70 </EditItemTemplate>
- 71 <ItemTemplate>
- 72 <table class="comments">
- 73 <tr>
- 74 <td class="commentsheader" rowspan="2">
- 75 <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("Login") %>' />
- 76 answered:
- 77 <div id="details">
- 78 <br />
- 79 Rating:
- 80 <asp:Label ID="Usersratinglabel" runat="server" Text='<%# Eval("Rating") %>' />
- 81 <br />
- 82 Posts:
- 83 <asp:Label ID="Usersanswers" runat="server" Text='<%# Eval("Answers") %>' />
- 84 <asp:HiddenField ID="commentidfield" runat="server" Value='<%# Eval("CommentID") %>' />
- 85 <asp:HiddenField ID="userloginfield" runat="server" Value='<%# Eval("UserID") %>' />
- 86 </td>
- 87 <td class="commentsmain">
- 88 <asp:Label ID="CommentLabel" runat="server" Text='<%# Eval("Comment") %>' />
- 89 </td>
- 90 </tr>
- 91 <tr>
- 92 <td class="commentfooter">
- 93 <asp:ImageButton ID="editbutton" CommandName="Edit" runat="server" ImageUrl="Images/commentedit.png" />
- 94 <asp:ImageButton ID="commentvotebutton" runat="server" OnCommand="commentvote_Click" ImageUrl="Images/commentvote.png"
- 95 CommandArgument='<%# Eval("CommentID") %>' CommandName="commentvote" />
- 96 </td>
- 97 </tr>
- 98 </td> </tr>
- 99 </table>
- 100 </ItemTemplate>
- 101 </asp:ListView>