I am just really getting into learning all this with ASP and have been head deap into books, lol. The thing I am not sure about is this. I am using the .NET controls on my page but I do not understand how to take what I have below and then do a if then statement on it.
So another words here is my code:
<asp:DataListID="DataList1"runat="server"DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LabelID="SpringBCT1Label"runat="server"Text='<%# Eval("SpringBCT1") %>'/>
<br/>
</ItemTemplate>
</asp:DataList>
Now how can I take what the results were that came back for that and do a if then statement on it? I need a way to look at the data. The data from the database will either be "On Time" or "Delayed"
So I want to do a...
If whatever = "On Time" then
Else
' do something else here
I am just not sure how to convert that control into a string of data. Code example would be great.
Big thanks in advance!
Is "whatever" the results of Eval("SpringBCT1") ?
Depending on how/when u need this value you can do two things. The first (and easiest) is just to attach an event to your SprintBCT1Label like: OnPreRender="GetMyTextValue"
protected void GetMyTextValue(object sender, EventArgs e){
Label label = sender as Label;
if (label == null)
return;
Response.Write(label.Text);//do something useful with it
}
and then you can just read out the value of the label every time the event handler fires for each label. Alternatively you can enumerate through the data lists items.
foreach (DataListItem item in DataList1.Items){
Label bct1Label = item.FindControl("SpringBCT1") as Label;
if (bct1Label == null)
continue;
Response.Write(bct1Label.Text);//do something useful
}
Hope that helps.
Yes the "watever" would be the results. I have selected to do this all in VB. The above has me lost... lol. Could you go into a bit more detail?
Thanks!
Here is how I would like to do it.. This is the way I did it in pure VB.NET
Dim MyConnAs ADODB.ConnectionDim MyRecSetAs ADODB.Recordset
Dim strTitleAsString
MyConn =New ADODB.ConnectionMyConn.ConnectionString ="Provider=sqloledb;Data Source=D032379\SQLEXPRESS;Initial Catalog=CutOffGrid;Integrated Security=True"
MyConn.Open()
MyRecSet = MyConn.Execute("SELECT * FROM Grids")DoUntil MyRecSet.EOF
strTitle = MyRecSet.Fields.Item("SpringBCT1").ValueIf strTitle ="On Time"Then
SpringBCT1.Text ="On Time"
Else
SpringBCT1.Text ="Delayed"
EndIf
MyRecSet.MoveNext()
Loop
MyConn.Close()
Problem is this does not seem to work in ASP.NET. It does not like these items:
Dim MyConnAs ADODB.ConnectionDim MyRecSetAs ADODB.Recordset
MyConn =New ADODB.Connection
And I have no idea as to how to do this then :-(
Any ideas?
Because they're old ADO objects not ADO.Net...
you can look throw the datalist like this:
Dim strTitle
For Each item As DataListItem In DataList1.Items
Dim bct1Label As Label = TryCast(item.FindControl("SpringBCT1"), Label)
If bct1Label Is Nothing Then
Continue For
End If
strTitle = bct1Label.Text
if NOT strTitle = "On Time" Then 'not sure that's how you do != in vb
bct1Label.Text = "Delayed"
End If
Next
Do you actually need the datalist or have you just done that because you experimenting?
See MSDN docs on SqlDataSource, SqlConnection and SqlDataReader
Ok ffigured it out. :-)
This is how I did it with the backend VB page...
Dim conpubsAs SqlConnectionDim cmdselectauthorsAs SqlCommand
Dim dtrauthorsAs SqlDataReaderDim sp1AsString
Dim sp2AsString
conpubs =New SqlConnection("Data Source=D032379\SQLEXPRESS;Initial Catalog=CutOffGrid;Integrated Security=True")conpubs.Open()
cmdselectauthors =New SqlCommand("select * from Grids", conpubs)dtrauthors = cmdselectauthors.ExecuteReader()
While dtrauthors.Read()sp1 = (dtrauthors("SpringBCT1")).ToString
sp2 = (dtrauthors("SpringBCT2")).ToStringSpringBCT1.Text = sp1
SpringBCT2.Text = sp2
EndWhile
dtrauthors.Close()
conpubs.Close()
Thanks for the help!
You can also do this way,
<asp:LabelID="SpringBCT1Label"runat="server" Text='<%#IIf((Eval("SpringBCT1").ToString() = "On Time"), "On Time", "Delayed") %>'/>
Thanks
-Mark post(s) as "Answer" that helped you
0 comments:
Post a Comment