ASP.NET - Calling a page PreRender event from a function.

Asked By Ryan Gomez on 08-Sep-09 11:46 AM
Hi everyone,

How do I go about calling a PreRender event from a function? What I have in mind is something like this:

protected void btnGetSomething_Click(object sender,EventArgs e)
{
        //call the PreRender event
        listView_PreRender(object sender, EventArgs e)
}

protected void listViewView_PreRender(object sender, EventArgs e)
{
        bindGroupList();

}

protected void bindGroupList()
{
        //binding happens here
}

However, I'm not entirely sure what parameters I should be passing on to the PreRender event. How do I go about successfully calling a PreRender event from a Button control? Any help would be appreciated. Thanks again.

re - Web Star replied to Ryan Gomez on 08-Sep-09 11:57 AM

You can define every event of page directly in code behind.


protected void Page_Load(object sender, EventArgs e)
{

}

protected void Page_PreInit(object sender, EventArgs e)
{

}

protected void Page_PreRender(object sender, EventArgs e)
{

}

In VB.Net, there is a list to show every event of page on the right side of
VS.
In C#, there isn't a shootcut like this to do. But there is a workround to
build this kind of event:
For example, you can type "Page." in Page_Load event, it will autocomplete
to show every attributes and event of page instance. Then you can select
one of the events it lists. Assuming that you select PreRender event, it
will show Page.PreRender. After that, you can input "+=". It will prompt
you press "Tab" key to insert the event handler. Then you can press "Tab"
key, it will generate "Page.PreRender+=new EventHandler(Page_PreRender);"
automatically, and when you press "Tab" once more, it will generate the
event handler as below if this event handler is not defined.
void Page_PreRender(object sender, EventArgs e)
{
throw new NotImplementedException();
}

Append "protected" before "void".

protected void Page_PreRender(object sender, EventArgs e)
{
//throw new NotImplementedException();
}

At last, don't forget to remove "Page.PreRender+=new
EventHandler(Page_PreRender);" what we built in Page_Load. We don't need it
any more.

That is a workround to build an Event Handler in C#.

But why would you do that? - [)ia6l0 iii replied to Ryan Gomez on 08-Sep-09 01:31 PM

you can associate with a simple line like,
btnGetSomething.Click += listViewView_PreRender; 

But why would you do that?
The PreRender method is called for the page and then recursively called for all controls on the page...you should instead look at databind methods of the listview at probably load stage in the pagelife cycle

btnClick has to call PreRender - Ryan Gomez replied to [)ia6l0 iii on 08-Sep-09 02:55 PM

I'm using ASP.NET Ajax to refresh only part of the page. The scenario I have is that when I click on a button, I get back a list of 10 records with a DataPager serving as the paging control a ListView. However, DataPager only works properly when it is called from PreRender. So what I need to happen is that on the btnClick, it calls the PreRender which in turn calls the DataPager. Basically I need the PreRender to call the DataPager as shown below. I only want the PreRender method to be called on btnClick.

I'm probably going about this the wrong way and so any pointers would be really helpful! Thanks again.

btnClick ---> PreRender ---> DataPager
Yes, you are correct. - [)ia6l0 iii replied to Ryan Gomez on 08-Sep-09 11:28 PM
Paging would only work if we bind the listview to the datasource in the dataPager's Prerender event. 

So the first option that strikes me , is to place the Listview bind call in a separate function 

<asp:DataPager ID="DataPagerSampleData" runat="server" PagedControlID="lvSampleData"
    PageSize="3" OnPreRender="DataPagerSampleData_PreRender">
    <Fields>
      ..
    </Fields>
</asp:DataPager>

And the prerender method would call the binddata function. 
protected void DataPagerSampleData_PreRender(object sender, EventArgs e)
{
    binddata();
}

private void binddata()
{
  this.lvSampleData.DataSource = db.GellAll();
    this.lvSampleData.DataBind();
}

and in the button click, set the pageproperties to the current index. I think that should trigger the PreRender method of the datapager. If it does not call binddata() method. 

protected void button_click(object sender, EventArgs e)
{
    int PageSize = DataPagerSampleData.PageSize;
    DataPagerSampleData.SetPageProperties(currentpageindex * PageSize, PageSize, true);
}

I will let you know more when i do a sample with this.
Solved it....I think. - Ryan Gomez replied to [)ia6l0 iii on 09-Sep-09 05:16 AM
I got paging to work but I'm not sure if this is the most elegant way of going about it. What do you reckon?

protected void btnGetGroups_Click(object sender,EventArgs e)
{
       
        EventArgs eventButtonClick = e;
        groupSearchView_PreRender(this, eventButtonClick);
 }

protected void groupSearchView_PreRender(object sender, EventArgs e)
{
      bindList();
 }

protected void bindList()
{
      //binding to datasource
}