how to reference bind variable value in code file

  • Thread starter Thread starter duancg
  • Start date Start date
D

duancg

Hi,

I wonder if someone could help since I wasn't able to find the answer
through search. I have a simple .aspx page that shows data from a
database table, as a table in UI. Now the data uses 1~12 to represent
months, but I want to show them as Jan/.../Dec. So I thought I could
write a simple function to convert it and call that function in the
Bind expression of DataList. However, I just couldn't find a way to
pass the bind variable value (1~12) into my function. Here is the
code:

.....
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="C:\db2.mdb"
SelectCommand="SELECT [name], [month] FROM [events]" />
<asp:DataList ID="DataList1" runat="server"
DataSourceID="AccessDataSource1">
<ItemTemplate>
name:
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name")
%>'></asp:Label><br />
month:
<asp:Label ID="monthLabel" runat="server" Text='<%#
month_itoa(Eval("month")) %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
.....

CodeFile:
.....
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { }
public string month_itoa(string i) {
if (i == '1') { return "Jan"; }
if (i == '2') { return "Feb"; }
if (i == '3') { return "Mar"; }
if (i == '4') { return "Apr"; }
if (i == '5') { return "May"; }
if (i == '6') { return "Jun"; }
if (i == '7') { return "Jul"; }
if (i == '8') { return "Aug"; }
if (i == '9') { return "Sep"; }
if (i == '10') { return "Oct"; }
if (i == '11') { return "Nov"; }
if (i == '12') { return "Dec"; }
return "unknow";
}
}

Note, I wanted to pass value of Eval("month") to the function
month_itoa() in the content file, but obviously it didn't work. I
guess there may be a way to get that value within code file without
need to pass the value, but couldn't find any document either. Any
help is appreciated!

Chenggang
 
Chenggang,

I think you just need to add a ToString() in order to get the right
parameter type for you function.
....
<asp:Label ID="monthLabel" runat="server"
Text='<%# month_itoa(Eval("month").ToString()) %>'></asp:Label>
....

I also had to change the single quotes to double quotes in the month_itoa()
function.

public string month_itoa(string i) {
if (i == "1") { return "Jan"; }
if (i == "2") { return "Feb"; }
if (i == "3") { return "Mar"; }
if (i == "4") { return "Apr"; }
if (i == "5") { return "May"; }
if (i == "6") { return "Jun"; }
if (i == "7") { return "Jul"; }
if (i == "8") { return "Aug"; }
if (i == "9") { return "Sep"; }
if (i == "10") { return "Oct"; }
if (i == "11") { return "Nov"; }
if (i == "12") { return "Dec"; }
return "unknow";
}

Another way to do it is to add a datetime column to your database table and
sql query. Then use the custom datetime format string of "MMM" to get the
3-character month abbreviation.

Add a datetime column to events called [evt_date]. This column holds the
full date (ie. 1/1/2007). Then modify your sql to:

SelectCommand="SELECT [month], [name], [evt_date] FROM dbo.events"
....
<asp:Label ID="monthLabel2" runat="server" Text='<%#
Convert.ToDateTime(Eval("evt_date")).ToString("MMM") %>'></asp:Label>
.....

I used sql express to test this instead of access but it should work the
same for the binding. Let me know if you have any problems.

hope this helps,
jason vermillion

Hi,

I wonder if someone could help since I wasn't able to find the answer
through search. I have a simple .aspx page that shows data from a
database table, as a table in UI. Now the data uses 1~12 to represent
months, but I want to show them as Jan/.../Dec. So I thought I could
write a simple function to convert it and call that function in the
Bind expression of DataList. However, I just couldn't find a way to
pass the bind variable value (1~12) into my function. Here is the
code:

.....
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="C:\db2.mdb"
SelectCommand="SELECT [name], [month] FROM [events]" />
<asp:DataList ID="DataList1" runat="server"
DataSourceID="AccessDataSource1">
<ItemTemplate>
name:
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name")
%>'></asp:Label><br />
month:
<asp:Label ID="monthLabel" runat="server" Text='<%#
month_itoa(Eval("month")) %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
.....

CodeFile:
.....
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { }
public string month_itoa(string i) {
if (i == '1') { return "Jan"; }
if (i == '2') { return "Feb"; }
if (i == '3') { return "Mar"; }
if (i == '4') { return "Apr"; }
if (i == '5') { return "May"; }
if (i == '6') { return "Jun"; }
if (i == '7') { return "Jul"; }
if (i == '8') { return "Aug"; }
if (i == '9') { return "Sep"; }
if (i == '10') { return "Oct"; }
if (i == '11') { return "Nov"; }
if (i == '12') { return "Dec"; }
return "unknow";
}
}

Note, I wanted to pass value of Eval("month") to the function
month_itoa() in the content file, but obviously it didn't work. I
guess there may be a way to get that value within code file without
need to pass the value, but couldn't find any document either. Any
help is appreciated!

Chenggang
 
Thanks, Jason! The ToString() trick works. Sorry about the single
quotes, some copy/paste messed up on my side. And thanks for the
alternative approach as well, unfortunately, I can not change the DB
format for some reason.
 
Back
Top