 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 12th, 2006, 02:58 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
datalist problem
Hi,
I am new to asp.net
I had the following code in asp and now I want to convert it to asp.net
I have a datalist.How can I use these if then statements in a datalist or through page_load code?
The asp code:
<A href="profile.asp?uid=<%=rsUser("userID")%>" >
<%if rsUser("userPic") <> "" Then%><IMG alt="<%=rsUser("firstname")%>" src="<%=strAppURL%>/user_images/<%=rsUser("UserPic")%>" width=<%=intWidth%> height=<%=intHeight%> border=0>
<%else%>
<%if rsUser("gender")="male" then%>
<IMG alt="<%=rsUser("firstname")%>" src="<%=strAppURL%>/user_images/noimage_male.jpg" width=<%=intWidth%> height=<%=intHeight%> border=0>
<%else%>
<IMG alt="<%=rsUser("firstname")%>" src="<%=strAppURL%>/user_images/noimage_female.jpg" width=<%=intWidth%> height=<%=intHeight%> border=0>
<%end if%>
<%end if%>
The datalist i HAVE created:
<asp:DataList id="displayProfile" RepeatColumns="4" RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<TD class=picFrame style="MARGIN: 0px" vAlign=top align=middle><SPAN >
<DIV class=head> <A href="profile.aspx?uid=<%# DataBinder.Eval(Container.DataItem, "UserID") %>" >
<IMG alt="<%# DataBinder.Eval(Container.DataItem, "Firstname") %>" src="<%=strAppURL%>/user_images/<%# DataBinder.Eval(Container.DataItem, "UserPic") %>" width=<%=intWidth%> height=<%=intHeight%> border=0>
<BR><%# DataBinder.Eval(Container.DataItem, "firstname") %></SPAN>(<%# DataBinder.Eval(Container.DataItem, "friend_count") %>)</A></div>
</TD>
</ItemTemplate>
</asp:DataList>
|
|

July 12th, 2006, 08:59 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
You could just do this in a stored procedure:
CREATE PROCEDURE usp_getUserData as
SELECT
userID,
userFirstName as [FirstName],
Case
when userPic is Null AND gender = 'Male' then './user_images/noimage_male.jpg'
when userPic is Null and gender = 'Female' then './user_images/noimage_female.jpg'
Else
UserPic
End as [UserPic],
friend_count
From
UserTable
Where
[Insert your where clause here]
You can leave your second datalist as is. What this stored procedure does is check the column userPic to see if its null and also the gender column of the selected row.
If the userPic column is Null and gender is male, it returns the url that points to your noImage Male.
If the userPic column is Null and gender is female, it returns the url that points to your noImage Female.
Else it will just pull the contents of the userPic column.
You may also want to add a third When statement in there in the event that both the userPic column and gender column are null.
Lastly, the width and height, if your storing those in the database, you can add those columns to the select list and pull them down and assign those values to variables.
[The stored procedure syntax is TSQL (MS SQL Server) the syntax for MySQL, oracle, etc will vary slightly]
"The one language all programmers understand is profanity."
|
|

July 12th, 2006, 10:15 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks dparsons
it was a nice answer.
Is this the only way.
Can I do it without sp in the code?
|
|

July 12th, 2006, 11:05 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Yes. Depending on how you pull your data you could set up the same kind of structure that I have displayed in the stored procedure.
Create a datatable variable and add the necessary columns to it and then loop through your result set.
For each dr in dt.rows <---Main datatable that has your information
if dr.item("UserPic") = "" and dr.item("gender") = "Male" then
drTemp.item("userPic") = ./user_images/noimage_male.jpg'
elseIf dr.item("UserPic") = "" and dr.item("gender") = "female"
drTemp.item("userPic") = ./user_images/noimage_female.jpg'
end if
dtNew.Rows.Add(drTemp)
Next
Then bind your datalist to DTNew.
Be aware, you will have to setup the columns pragmatically for the new datatable like so:
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "userPic"
dc.ReadOnly = False
dc.Unique = False
dtNew.Columns.Add(dc)
You will obviously need to do this for each column that will be in the datatable.
Your variable declariation should look something like this:
Dim dt as new datatable
Dim dtNew as new datatable
Dim dr as datarow
Dim drTemp as datarow
Dim dc as datacolumn
hope this helps
"The one language all programmers understand is profanity."
|
|

July 13th, 2006, 04:52 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok.Now how can I integrate this code with my datalist.can you show me an example?
|
|

July 13th, 2006, 06:34 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Above is a nearly complete example for you. All you need to do is declare your variable and then set up the columns for dtNew, retrive your data, iterate through it and add that data to dtNew then:
datalist1.datasource = dtNew
datalist1.databind()
Besides, you have to do something! =]
"The one language all programmers understand is profanity."
|
|

July 13th, 2006, 11:17 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks a lot dparsons
I'm sure it will help
|
|
 |