Jump to content

Datalist method misunderstanding...


kreplech

Recommended Posts

I think I'm starting to understand that I have no idea what I'm doing... I mean that in the best possible way... Hopefully it's a notch on the learning curve...Anywho...So, I have a datasource and a datalist like so:<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CTS_Conn %>" SelectCommand="SELECT [PaperTitle], [FileTyp], [FileURL], [FileSiz] FROM [qryValidPapers] WHERE ([cid] = @cid)"> <SelectParameters> <asp:QueryStringParameter Name="cid" QueryStringField="CID" Type="String" /> </SelectParameters></asp:SqlDataSource><asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource3" OnLoad="DataList2_OnLoad"> <ItemTemplate> <asp:HiddenField ID="fileType" runat="server" Value='<%# Eval("FileTyp") %>' /> <asp:Image ID="fileIcon" runat="server" ImageUrl="" /> <asp:Label ID="fileTitle" runat="server" Text='<%# Eval("PaperTitle") %>'></asp:Label> <asp:Label ID="fileURL" runat="server" Text='<%# Eval("FileURL") %>'></asp:Label> <asp:Label ID="fileSize" runat="server" Text='<%# "(" + Eval("FileSiz") + ")" %>'></asp:Label> <br /> </ItemTemplate></asp:DataList>Now, what I'm trying to do is determine which icon to display in the image control fileIcon depending on the value of the label fileType... problem is, not matter what I do, I can't see any of the controls in my datalist from my method (which method you ask? any of them... i've tried them all).HOWEVER!!! I've realized that I can see any other control on the screen from the method... leading me to believe that the datalist method can't see it's own controls because they haven't been loaded, rendered, or whatever'ed yet.So... two questions:1) Am I just WAAAYYYYY off base here?2) So what to do? Not use a datalist? Hop around the dataset the same way I would've using classic asp?As usual: any help is much appreciated.Thanks again,Mand just for giggles... my OnLoad method that does little other than generate a joyful "System.NullReferenceException: Object reference not set to an instance of an object." error...protected void DataList2_OnLoad(object sender, EventArgs e) { HiddenField fileType = (HiddenField)DataList2.FindControl("fileType"); Label3.Text = ":" + fileType; // I can see Label3, but fileType is NULL... just like everything else. Image fileIcon = (Image)DataList2.FindControl("fileIcon"); if (Convert.ToString(fileType) == "PDF") // tests false because fileType is NULL { fileIcon.ImageUrl = "/images/pdf.gif"; // no error here probably because I test false for the IF conditional } else { fileIcon.ImageUrl = "/images/text.gif"; // causes the error... } }

Link to comment
Share on other sites

I never use DataLists, but I do periodically use Repeaters and I run into this problem as well. The solution is the ItemDataBound event.

<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource3" OnLoad="DataList2_OnLoad" OnItemDataBound="DataList2_ItemDataBound">

protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) {	switch(e.Item.ItemType)	{		case ListItemType.Item:		case ListItemType.AlternatingItem:			string filetype = DataBinder.Eval(e.Item.DataItem, "FileType").ToString();			switch(filetype)			{				case "txt":					(Image)e.Item.FindControl("fileIcon").ImageUrl = "someimage.gif";					break;			}			break;	}}

For more info, check out the MSDN page: http://msdn2.microsoft.com/en-us/library/s...mdatabound.aspx

Link to comment
Share on other sites

Hi Jesh,Thanks for the reply... sorry it took me so long to respond.Still can't get it to work... but...I had to make a change:string fileType = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "FileType")); // instead of .ToStringalso, now it's telling me: CS0117: 'System.Web.UI.Control' does not contain a definition for 'ImageUrl'Strange... I don't have any time to play with it right now, but I'll try a few things later to see if anything works... and update ya.Thanks again,M

Link to comment
Share on other sites

also, now it's telling me: CS0117: 'System.Web.UI.Control' does not contain a definition for 'ImageUrl'
Ah, sorry. I wrote that without testing it. Change that line to:
((Image)e.Item.FindControl("fileIcon")).ImageUrl = "someimage.gif";

Or,

Image image = (Image)e.Item.FindControl("fileIcon");image.ImageUrl = "someimage.gif";

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...