DJO
03-18-2008, 01:07 AM
Posted this on ASP.Net forums but thought someone here might have an idea:
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:
[quote]
protectedvoidListView1_ItemDataBound(objectsender, ListViewItemEventArgse)
{
HiddenFieldcommentidfield=e.Item.FindControl('comm entidfield')asHiddenField;
HiddenFielduserloginfield=e.Item.FindControl('user loginfield')asHiddenField;
ImageButtonvotebutton=e.Item.FindControl('commentv otebutton')asImageButton;
ImageButtoneditbutton=e.Item.FindControl('editbutt on')asImageButton;
if(HttpContext.Current.User.Identity.IsAuthenticat ed==true)
{
intcommentidtemp=Convert.ToInt32(commentidfield.Va lue);
intuserloginid=Convert.ToInt32(userloginfield.Valu e);
intcommentcheck=Convert.ToInt32(commentratingclass .CheckCommentVote(Profile.userid,commentidtemp).Ge tValueOrDefault(0));
inteditcheck=Convert.ToInt32(commentclass.checkcom mentbyuserid(Profile.userid,commentidtemp).GetValu eOrDefault(0));
if(userloginid==Profile.userid)
{
votebutton.Visible=false;
}
elseif(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;
}
}
</CODE>
Listview:
[quote]
1<asp:ListViewID='ListView1'runat='server'DataKeyNam es='CommentID'DataSourceID='SqlDataSource1'
2OnItemDataBound='ListView1_ItemDataBound'
3onitemupdating='ListView1_ItemUpdating'>
4<LayoutTemplate>
5<tablerunat='server'>
6<trrunat='server'>
7<tdrunat='server'>
8<tableid='itemPlaceholderContainer'runat='server'bo rder='0'style=''>
9<trid='itemPlaceholder'runat='server'>
10</tr>
11</table>
12</td>
13</tr>
14<trrunat='server'>
15<tdrunat='server'style=''>
16<center>
17<asp:DataPagerID='DataPager1'runat='server'>
18<Fields>
19<asp:NextPreviousPagerFieldButtonType='Button'ShowF irstPageButton='True'ShowNextPageButton='False'
20ShowPreviousPageButton='False'/>
21<asp:NumericPagerField/>
22<asp:NextPreviousPagerFieldButtonType='Button'ShowL astPageButton='True'ShowNextPageButton='False'
23ShowPreviousPageButton='False'/>
24</Fields>
25</asp:DataPager>
26</center>
27</td>
28</tr>
29</table>
30</LayoutTemplate>
31<EmptyDataTemplate>
32<tablerunat='server'style=''>
33<tr>
34<td>
35NoAnswers!AddoneNow!
36</td>
37</tr>
38</table>
39</EmptyDataTemplate>
40<EditItemTemplate>
41<tableclass='comments'>
42<tr>
43<tdclass='commentsheader'rowspan='2'>
44<asp:LabelID='UserIDLabel'runat='server'Text='<%#Eval('Login')%>'/>
45answered:
46<divid='details'>
47[b]
48Rating:
49<asp:LabelID='Usersratinglabel'runat='server'Text='<%#Eval('Rating')%>'/>
50[b]
51Posts:
52<asp:LabelID='Usersanswers'runat='server'Text='<%#Eval('Answers')%>'/>
53<asp:HiddenFieldID='commentidfield'runat='server'Va lue='<%#Eval('CommentID')%>'/>
54<asp:HiddenFieldID='userloginfield'runat='server'Va lue='<%#Eval('UserID')%>'/>
55</td>
56<tdclass='commentsmain'>
57<asp:TextBoxID='updatetext'runat='server'Text='<%#Bind('Comment')%>'TextMode='MultiLine'
58Width='500px'Height='150px'/>
59</td>
60</tr>
61<tr>
62<tdclass='commentfooter'>
63<divclass='commentfooter'>
64<asp:ButtonID='updatebutton'runat='server'Text='Upd ate'CommandName='Update'/>
65<asp:ButtonID='CancelButton'runat='server'CommandNa me='Cancel'Text='Cancel'/>
66</td>
67</tr>
68</td></tr>
69</table>
70</EditItemTemplate>
71<ItemTemplate>
72<tableclass='comments'>
73<tr>
74<tdclass='commentsheader'rowspan='2'>
75<asp:LabelID='UserIDLabel'runat='server'Text='<%#Eval('Login')%>'/>
76answered:
77<divid='details'>
78[b]
79Rating:
80<asp:LabelID='Usersratinglabel'runat='server'Text='<%#Eval('Rating')%>'/>
81[b]
82Posts:
83<asp:LabelID='Usersanswers'runat='server'Text='<%#Eval('Answers')%>'/>
84<asp:HiddenFieldID='commentidfield'runat='server'Va lue='<%#Eval('CommentID')%>'/>
85<asp:HiddenFieldID='userloginfield'runat='server'Va lue='<%#Eval('UserID')%>'/>
86</td>
87<tdclass='commentsmain'>
88<asp:LabelID='CommentLabel'runat='server'Text='<%#Eval('Comment')%>'/>
89</td>
90</tr>
91<tr>
92<tdclass='commentfooter'>
93<asp:ImageButtonID='editbutton'CommandName='Edit'ru nat='server'ImageUrl='Images/commentedit.png'/>
94<asp:ImageButtonID='commentvotebutton'runat='server 'OnCommand='commentvote_Click'ImageUrl='Images/commentvote.png'
95CommandArgument='<%#Eval('CommentID')%>'CommandName='commentvote'/>
96</td>
97</tr>
98</td></tr>
99</table>
100</ItemTemplate>
101</asp:ListView>
</CODE>[code]
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:
[quote]
protectedvoidListView1_ItemDataBound(objectsender, ListViewItemEventArgse)
{
HiddenFieldcommentidfield=e.Item.FindControl('comm entidfield')asHiddenField;
HiddenFielduserloginfield=e.Item.FindControl('user loginfield')asHiddenField;
ImageButtonvotebutton=e.Item.FindControl('commentv otebutton')asImageButton;
ImageButtoneditbutton=e.Item.FindControl('editbutt on')asImageButton;
if(HttpContext.Current.User.Identity.IsAuthenticat ed==true)
{
intcommentidtemp=Convert.ToInt32(commentidfield.Va lue);
intuserloginid=Convert.ToInt32(userloginfield.Valu e);
intcommentcheck=Convert.ToInt32(commentratingclass .CheckCommentVote(Profile.userid,commentidtemp).Ge tValueOrDefault(0));
inteditcheck=Convert.ToInt32(commentclass.checkcom mentbyuserid(Profile.userid,commentidtemp).GetValu eOrDefault(0));
if(userloginid==Profile.userid)
{
votebutton.Visible=false;
}
elseif(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;
}
}
</CODE>
Listview:
[quote]
1<asp:ListViewID='ListView1'runat='server'DataKeyNam es='CommentID'DataSourceID='SqlDataSource1'
2OnItemDataBound='ListView1_ItemDataBound'
3onitemupdating='ListView1_ItemUpdating'>
4<LayoutTemplate>
5<tablerunat='server'>
6<trrunat='server'>
7<tdrunat='server'>
8<tableid='itemPlaceholderContainer'runat='server'bo rder='0'style=''>
9<trid='itemPlaceholder'runat='server'>
10</tr>
11</table>
12</td>
13</tr>
14<trrunat='server'>
15<tdrunat='server'style=''>
16<center>
17<asp:DataPagerID='DataPager1'runat='server'>
18<Fields>
19<asp:NextPreviousPagerFieldButtonType='Button'ShowF irstPageButton='True'ShowNextPageButton='False'
20ShowPreviousPageButton='False'/>
21<asp:NumericPagerField/>
22<asp:NextPreviousPagerFieldButtonType='Button'ShowL astPageButton='True'ShowNextPageButton='False'
23ShowPreviousPageButton='False'/>
24</Fields>
25</asp:DataPager>
26</center>
27</td>
28</tr>
29</table>
30</LayoutTemplate>
31<EmptyDataTemplate>
32<tablerunat='server'style=''>
33<tr>
34<td>
35NoAnswers!AddoneNow!
36</td>
37</tr>
38</table>
39</EmptyDataTemplate>
40<EditItemTemplate>
41<tableclass='comments'>
42<tr>
43<tdclass='commentsheader'rowspan='2'>
44<asp:LabelID='UserIDLabel'runat='server'Text='<%#Eval('Login')%>'/>
45answered:
46<divid='details'>
47[b]
48Rating:
49<asp:LabelID='Usersratinglabel'runat='server'Text='<%#Eval('Rating')%>'/>
50[b]
51Posts:
52<asp:LabelID='Usersanswers'runat='server'Text='<%#Eval('Answers')%>'/>
53<asp:HiddenFieldID='commentidfield'runat='server'Va lue='<%#Eval('CommentID')%>'/>
54<asp:HiddenFieldID='userloginfield'runat='server'Va lue='<%#Eval('UserID')%>'/>
55</td>
56<tdclass='commentsmain'>
57<asp:TextBoxID='updatetext'runat='server'Text='<%#Bind('Comment')%>'TextMode='MultiLine'
58Width='500px'Height='150px'/>
59</td>
60</tr>
61<tr>
62<tdclass='commentfooter'>
63<divclass='commentfooter'>
64<asp:ButtonID='updatebutton'runat='server'Text='Upd ate'CommandName='Update'/>
65<asp:ButtonID='CancelButton'runat='server'CommandNa me='Cancel'Text='Cancel'/>
66</td>
67</tr>
68</td></tr>
69</table>
70</EditItemTemplate>
71<ItemTemplate>
72<tableclass='comments'>
73<tr>
74<tdclass='commentsheader'rowspan='2'>
75<asp:LabelID='UserIDLabel'runat='server'Text='<%#Eval('Login')%>'/>
76answered:
77<divid='details'>
78[b]
79Rating:
80<asp:LabelID='Usersratinglabel'runat='server'Text='<%#Eval('Rating')%>'/>
81[b]
82Posts:
83<asp:LabelID='Usersanswers'runat='server'Text='<%#Eval('Answers')%>'/>
84<asp:HiddenFieldID='commentidfield'runat='server'Va lue='<%#Eval('CommentID')%>'/>
85<asp:HiddenFieldID='userloginfield'runat='server'Va lue='<%#Eval('UserID')%>'/>
86</td>
87<tdclass='commentsmain'>
88<asp:LabelID='CommentLabel'runat='server'Text='<%#Eval('Comment')%>'/>
89</td>
90</tr>
91<tr>
92<tdclass='commentfooter'>
93<asp:ImageButtonID='editbutton'CommandName='Edit'ru nat='server'ImageUrl='Images/commentedit.png'/>
94<asp:ImageButtonID='commentvotebutton'runat='server 'OnCommand='commentvote_Click'ImageUrl='Images/commentvote.png'
95CommandArgument='<%#Eval('CommentID')%>'CommandName='commentvote'/>
96</td>
97</tr>
98</td></tr>
99</table>
100</ItemTemplate>
101</asp:ListView>
</CODE>[code]