Posted: 6/30/2011
Hi! I'm pretty new to c# and coding in general, but anyhow I'm trying to pass a string variable "passPercent" to a TemplateField Grid. I'm pretty sure the code is right on the .aspx page, but I think something is wrong with the code behind. When I assign my public string passPercent a value of say "65" it does display the data correctly in the web page. I chose passPercent to be a string because I could not seem to add the "%" sign after a number that is calculated. Please take a look at the following code snips so you know what I mean:
<asp:TemplateField HeaderText="Percent Available %"> <ItemTemplate> <asp:Label ID="Percentage" runat="server" ForeColor="Red" Text='<%# passPercent %>' /> </ItemTemplate> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:TemplateField>
and the code behind:
namespace MyStats { public partial class Stats : System.Web.UI.Page { public string passPercent; protected void Page_Load(object sender, EventArgs e) { StatisticsSoapClient proxy = new StatisticsSoapClient(); GroupStat[] station = proxy.GetGroupedCurrentStats(); GridView1.DataSource = station; GridView1.DataBind(); // New addition for % calculation: int availableCount = station[0].availableCount; int offCount = station[0].offCount; int totalCount = station[0].totalCount; int numerator = availableCount + offCount; int percent = (numerator / totalCount) * 100; passPercent = percent.ToString("N2") + "%"; } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } } }
Also, something else I just realized is that this % calculation will only work for the first station[0]. What would be a proper method to cycle through all available stations to make this calculation for each station??
Thanks in advance for taking a look.
Robert
Posted: 7/1/2011
Hi Robert,
You should change the array for every object in your array list of objects before you bind the grid.
First add '%' after that assign Grid.Datasource, and bind the gird. The code should look like:
foreach(object st in station) { // New addition for % calculation: int availableCount = st.availableCount; int offCount = st.offCount; int totalCount = st.totalCount; int numerator = availableCount + offCount; int percent = (numerator / totalCount) * 100; passPercent = percent.ToString("N2") + "%"; } GridView1.DataSource = station; GridView1.DataBind();
Best Regards,
Gjorgji
Invoke Page.DataBind() in your PageLoad method.
Thanks for the responses!
@Gjorgji:
After changing my code, I am getting an error each for availableCount, offCount and totalCount:
int availableCount = st.availableCount; int offCount = st.offCount; int totalCount = st.totalCount;
Error 1 'object' does not contain a definition for 'availableCount' and no extension method 'availableCount' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?).
I haven't seen this error before - can you explain this in more detail for me?
@Raghav:
Do you sugesst that I add the Page.DataBind() before of after the request for info - or does it matter? - Like this?:
protected void Page_Load(object sender, EventArgs e) { StatisticsSoapClient proxy = new StatisticsSoapClient(); GroupStat[] station = proxy.GetGroupedCurrentStats(); Page.DataBind();
Posted: 7/5/2011
Just wondering if there were any other ideas as to the errors that I am receiving?
I've also done some more searching and found that in order to pass the variables more effectively, I should bind the data and use:
Text='<%# Bind("passPercent") %>'
in the ItemTemplate. However I am still having issues with the binding of the data in the Code Behind - as well as the errors I'm getting from the availableCount, offCount and totalCount variables. Any nudges in the right direction would be greatly appreciated.
Thanks,