PDA

View Full Version : Problem with ListView


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]

wisemx
03-18-2008, 07:53 AM
Use a few simple debug methods since this is with an instance of an object.
For example, place a textbox on the page and add outputs to it as events fire.
You should be able to track down the culprit that way.
Salute,
Mark

DJO
03-21-2008, 03:59 AM
Nothing happened, I think it's related to the fact I called the button in listviewitembound. Only problem is, why does it work with the votebutton and not the edit button

wisemx
03-21-2008, 07:03 AM
I really like working with the listview, even created an ajax shoutbox with one.
And I'm willing to help you get through this, but, I'm so far behind in everything I might pop. http://community.discountasp.net/emoticons/tongue.gif
At any rate, if you get flustered with it I'll try and take a look at the code for ya.
Salute,
Mark