Loading ...

Passing a Code behind variable to a TemplateField

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Passing a Code behind variable to a TemplateField

Passing a Code behind variable to a TemplateField

Posts under the topic: Passing a Code behind variable to a TemplateField

Posted: 6/30/2011

Lurker 30  points  Lurker
  • Joined on: 6/27/2011
  • Posts: 6

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

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74

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


Posted: 7/1/2011

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

Invoke Page.DataBind() in your PageLoad method.


Posted: 7/1/2011

Lurker 30  points  Lurker
  • Joined on: 6/27/2011
  • Posts: 6

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();

Again - thanks for taking the time to answer my questions!

Robert


tags C#, DataBind

Posted: 7/5/2011

Lurker 30  points  Lurker
  • Joined on: 6/27/2011
  • Posts: 6

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,

Robert


Page 1 of 1 (5 items)