C#从父页面访问asp控件

时间:2016-09-26 15:09:06

标签: c# iframe findcontrol

我有一个包含iframe,ASP标签和按钮的父页面。 当用户单击父按钮时,它将在iframe中显示一些表结果。 现在我想根据iframe的结果显示具有不同文本和颜色的父标签。 所以我尝试在iframe后面的代码(C#)中从父代检索标签并为其分配一个CssClass。但无论我做什么,它总是在尝试从父级找到控件时返回null参考。 有人可以帮忙吗?

父页面代码:

#include <iostream>
#include <cstdlib> // has rand(),srand().
#include <ctime>   // has time.
#include <fstream> // enables file handling.
#include <math.h>

using namespace std;

float rand_gen(); //full function written after main().
//int pbc(int,int,int);

int main()
{
srand((int)time(0)); //sets time as the seed for the random no. generation.
int dim=5;
int i,j,k,iup,idown,jup,jdown,kup,kdown;//indices
float x;
int l[dim][dim][dim];

float M=0.0,E=0.0;

for(i=1;i<=dim;i++)
{
    for(j=1;j<=dim;j++)
    {
        for(k=1;k<=dim;k++)
        {
            x=rand_gen();
            if(x<=0.5)
            {
                l[i][j][k]=-1; //sets the value in that particular location.
            }
            else
            {
                l[i][j][k]=1; //sets the value in that particular location.
            }
            M=M+l[i][j][k];
        }
    }
}

cout<<"Total magnetisation= "<<M<<endl;

for(i=1;i<=dim;i++)
{
    for(j=1;j<=dim;j++)
    {
        for(k=1;k<=dim;k++)
        {
            if(i==1)iup=dim;    //*********************************
            else iup=i-1;
            if(i==dim)idown=1;
            else idown=i+1;

            if(j==1)jup=dim;
            else jup=j-1;      //periodic boundary condition.
            if(j==dim)jdown=1;
            else jdown=j+1;

            if(k==1)kup=dim;
            else kup=k-1;
            if(k==dim)kdown=1;
            else kdown=k+1;    // ends here//

            E=E-(l[i][j][k]*(l[iup][j][k]+l[idown][j][k]+l[i][jup][k]+l[i][jdown][k]+l[i][j][kup]+l[i][j][kdown]))/2.0;
        }
    }
}
cout<<"Average E= "<<E/(dim*dim*dim)<<endl;
//cout<<"Testing: "<<l[5][4][3]<<" "<<l[5][4][3]<<endl;;
//**************************ALL FINE TILL HERE IT SEEMS****************************************************//
int n=100; //Number of iterations.
int no,site;
int ri,rj,rk,riup,ridown,rjup,rjdown,rkup,rkdown;
float eng=0.0,mag=M;float E_check=0.0;
float E_b4,E_aft,E_diff;
float T=4.2;
float a;

ofstream file ("Energy_check");  //file created to write the results into.
//ofstream file1 ("ENERGY1.txt");

for(no=1;no<=n;no++)
{
    for(site=1;site<=(dim*dim*dim);site++)
    {
        float v1=rand_gen();
        float v2=rand_gen();
        float v3=rand_gen();

        ri= int(v1*dim)+1;
        rj= int(v2*dim)+1;
        rk= int(v3*dim)+1;

        //if((ri>=1&&ri<=dim)&&(rj>=1&&rj<=dim)&&(rk>=1&&rk<=dim))
        {if(ri==1)riup=dim;   //periodic boundary condition like above
        else riup=ri-1;
        if(ri==dim)ridown=1;
        else ridown=ri+1;

        if(rj==1)rjup=dim;
        else rjup=rj-1;
        if(rj==dim)rjdown=1;
        else rjdown=rj+1;

        if(rk==1)rkup=dim;
        else rkup=rk-1;
        if(rk==dim)rkdown=1;
        else rkdown=rk+1;    // ends here
        }

        E_b4=-(l[ri][rj][rk]*(l[riup][rj][rk]+l[ridown][rj][rk]+l[ri][rjup][rk]+l[ri][rjdown][rk]+l[ri][rj][rkup]+l[ri][rj][rkdown]))/2.0;
        E_aft=-(E_b4);
        E_diff=-2.0*(E_b4);
        E_check=E_check+E_b4;

        if(E_diff<0)
        {
            l[ri][rj][rk]=-l[ri][rj][rk];
            eng=eng+E_diff;
            mag=mag-2*l[ri][rj][rk];
        }
        else
        {
            a=rand_gen();
            if(a<pow(exp(1.0),-E_diff/T))
            {
                l[ri][rj][rk]=-l[ri][rj][rk];
                eng=eng+E_diff;
                mag=mag-2*l[i][j][k];
            }
        }


         if(file.is_open())
        { // THE FOLLOWING CODE IS SUPPOSED TO SHOW THE VALUES OF THE ARRAY CORRESPONDING TO ITS INDICES. bUT IM GETTING GARBAGE VALUES WHEN I RUN THIS.
            file<<no<<" "<<ri<<rj<<rk<<" "<<l[ri][rj][rk]<<" "<<riup<<rj<<rk<<" "<<l[riup][rj][rk]<<" "<<ridown<<rj<<rk<<" "<<l[ridown][rj][rk]<<" "<<ri<<rjup<<rk<<" "<<l[ri][rjup][rk]<<" "<<ri<<rjdown<<rk<<" "<<l[ri][rjdown][rk]<<" "<<ri<<rj<<rkup<<" "<<l[ri][rj][rkup]<<" "<<ri<<rj<<rkdown<<" "<<l[ri][rj][rkdown]<<" "<<E_b4<<" "<<E_check<<endl;
        }
    }
    E=(E+eng)/(dim*dim*dim);
    /*if(file1.is_open())
    {
        file1<<no<<"\t"<<E<<endl;
    }*/

}
file.close();
//file1.close();
cout<<"E_check_avg= "<<E_check/(dim*dim*dim)<<endl;
return 0;
}

float rand_gen() //function to generate random number.
{
return (static_cast <float> (rand()) / static_cast <float> (RAND_MAX));// random no. generated.
}

Results.aspx将动态显示一个包含一些html构建的表。 现在让我们说如果得到的表有一行,我希望Label WrapperReponse为绿色并说“1行”,但如果是2行,那么我希望它是红色并说“2行”

在我的Results.aspx.cs中,我尝试了

<div class="search-row">
  <div class="search">
      <button id="btnSearch" type="submit" class="button button-block">Search</button>
  </div>

  <div class="search" style="padding-top: 20px;">
        <asp:Label ID="wrapperResponse" CssClass="resp" runat="server">TEST</asp:Label>
   </div>
</div>


        <div id="iframeDiv">
            <iframe name="my_frame" width="100%" height="350px" src="Results.aspx" scrolling="no" frameborder="0"></iframe>
        </div>`

欢迎任何想法! 感谢

1 个答案:

答案 0 :(得分:0)

嗯,这里只需要提几点。值得深思。

  1. 要访问asp标签控件,您需要在父页面而不是结果页面上执行此操作。
  2. 我建议你重新考虑页面的设计。你真的必须在这里使用iframe吗?为什么不直接在results.page上放置按钮和标签控件?或者将results.aspx作为ASCX用户控件,因此您可以将其嵌入父页面。
  3. 希望有所帮助