Pregunta

Hope this comes out clear enough.

I am new to this all.

I have a asp.net and c# project, in the app_code i have a class userInterface.cs, what i need to do is the folowing:

In that class i need to get a certain page

NewPage.aspx

, and to modify some asp elements on that page.

Currently i have this:

Page p = (Page)HttpContext.Current.Handler;    

Not sure what else i need in order to get the page. the page i want is called NewPage.aspx.

I will appreciated any answer.

even something to google in order to find will be great. i dont know were to start from....

¿Fue útil?

Solución

You may be able to access the page through HttpContext, but that's probably not a good approach. As other posters have noted, just use a method and pass in a reference to the control.

To answer your question though, you can try something like this:

if (HttpContext.Current.Handler is Page)
{
    Page currentPage = (Page)HttpContext.Current.Handler;
    if (currentPage != null)
    {
        //depending on where the control is located, you may need to use recursion
        GridView gridView = currentPage.FindControl("GridView1");
    }
}

I must reiterate that this probably isn't a good approach though, for a myriad of reasons. I did want someone to answer your actual question though, so there it is.

Otros consejos

You can create method in App_Code class and call this method from any event at your code behind. You can pass your GridView or your complete Page to this method.

This is your Page_Load event located at your code behind file.

protected void Page_Load(object sender, EventArgs e)
{
    UserInterface.UpdateGrid(ref GridView1);
}

This is the static method located in your .cs file.

public static void UpdateGrid(ref GridView view)
{
    // update your GridView here
}

I think you cannot do that within your web site but you can create a method within your App_Code file that you will be able to call from your page and pass the control to this method to access it from there.

Update

I your App_Code file

using System.Web.UI.WebControls;

public static void AddColumn(ref GridView gv)
    {
        BoundField field1=new BoundField();
        field1.HeaderText="Header Text";
        field1.DataField = "DataFieldName";
        gv.Columns.Add(field1);


        BoundField  field2 = new BoundField();
        field2.HeaderText = "Header Text";
        field2.DataField = "DataFieldName";
        gv.Columns.Add(field2);
    }

in your page

Test.AddColumn(ref MyGridView);
MyGridView.DataSource = names;// assign your datasource here
MyGridView.DataBind();

If you are trying to get access to an .aspx page from your App_Code class, there is something wrong with your code. Your are not supposed to do this. The classes from App_Code are for serving your .aspx pages, not reverse. In App_Code you are keeping utilities classes, for example.

If the content page (.aspx) is bound under a Master Page . You need to consider locating the Parent control of the controls inside the content page. For example, If the master page contains content place holder and inside that holder you are binding pages, it is necessary to locate the content place holder first.

Page p = HttpContext.Current.Handler as Page;
Label lbl = p.Form.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1").FindControl("lblTest") as Label

While compiling a request to response, ASP.Net always go through the master page file (if there is any) to reach to a content page. So while requesting a page control under any .aspx file from a class it is necessary locate the Parent control inside the Master page first.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top