3

I have a RadMultipage control on my asp.net web page.

I am loading the user controls as pages inside this control.

Below is the code to load the user controls.

 protected void RadMultiPage1_PageViewCreated(object sender, Telerik.Web.UI.RadMultiPageEventArgs e)
    {
        try
        {
            string controlName;
            int index = e.PageView.ID.Trim().IndexOf(" ");
            if (index > 0)
            { controlName = e.PageView.ID.Trim().Remove(index); }
            else
            { controlName = e.PageView.ID; }

            Control pageViewContents = LoadControl(controlName + ".ascx");
            pageViewContents.ID = e.PageView.ID + "userControl";
            e.PageView.Controls.Add(pageViewContents);
        }
        catch (Exception ex)
        {
            Utility.WalkException(this, ex, "There was an error while performing this operation.");
        }
    }

I have also enabled the view state and autoeventwireup.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" EnableViewState="true" ViewStateMode="Enabled" Inherits="VentureAssessmentApp.Default" %>

Now the problem I am having is with the button on the user control. The click event of that button is not getting fired. It just reloads the page. Even the IsPostBack is returning false.

Can any one suggest some solution ? It happens that some times the click event works and most of the time it doesn't work.

Sachin Trivedi
  • 2,033
  • 4
  • 28
  • 57
  • If You create the controls dynamically You usually should re-create them after each postback. Maybe the event You put Your code in may fire only upon control first load or first page creation. Please also write if the `Button` inside the user control is also added dynamically or it's declared in markup in **.ascx* file. – Lukasz M Jul 02 '14 at 08:50
  • @LukaszM the button is declared in the makup of the *.ascx file. But it sometimes fires the click event and some times it doesn't. – Sachin Trivedi Jul 02 '14 at 08:57
  • Please confirm if the `RadMultiPage1_PageViewCreated` method is fired each time on postback. If not, You should put the code creating the control in `Page_Init` or `Page_Load` page event handler. In the `RadMultiPage1_PageViewCreated` method just set a flag to indicate if the control should be created. – Lukasz M Jul 02 '14 at 09:02
  • @LukaszM RadMultiPage1_PageViewCreated is fired each time on postback like on page load and also on button click. but the IsPostBack is still false on button click. – Sachin Trivedi Jul 02 '14 at 09:08
  • It's likely that the control may be added too late to be working correctly, try to move the code that adds the control to the page to `Page_Init` or `Page_Load` page event handler like suggested in by *Leo*. – Lukasz M Jul 02 '14 at 09:13
  • @LukaszM Let me try that. – Sachin Trivedi Jul 02 '14 at 09:15
  • @LukaszM Thanks for helping me out. But there was some other issue in my case. I have posted the answer. – Sachin Trivedi Jul 07 '14 at 07:06
  • No problem. Great that You found solution to the issue :). – Lukasz M Jul 07 '14 at 15:20

2 Answers2

1

This is a typical page life cycle issue. I'm guessing that what's happening is that when the postback is triggered, the page has no clue about which control triggered the postback because when it does the evaluation the Button (and its parent UserControl) haven't been added to the page's control hierarchy...so, I'd point my finger to the RadMultiPage1_PageViewCreated event handler. It looks to me like an awkward place to dynamically add server controls to a page.

Suggestion

Move the control loading logic out of the RadMultiPage1_PageViewCreated event. Place this logic in the Page's Init event, or on the page's Load event if the Init event is too early.

You can determine what page has been selected in the RadMultiPage control by inspecting the SelectedIndex property or the SelectedPageView property. However, if you are using a RadTabStrip control in conjunction with the RadMultiPage control you can inspect the RadTabStrip's SelectedTab or SelectedIndex property

Example

protected override void OnLoad(EventArgs e)
{
    LoadStuff();
}

private void LoadStuff()
{
        try
        {
            string controlName;
            int index = YOUR_MULTI_PAGE_CONTROL.SelectedIndex;
            if (index > 0)
            { controlName = YOUR_MULTI_PAGE_CONTROL.PageViews[index].ID.Trim().Remove(index); }
            else
            { controlName = YOUR_MULTI_PAGE_CONTROL.PageViews[index].ID; }

            Control pageViewContents = LoadControl(controlName + ".ascx");
            pageViewContents.ID = YOUR_MULTI_PAGE_CONTROL.PageViews[index].ID + "userControl";
            YOUR_MULTI_PAGE_CONTROL.PageView.Controls.Add(pageViewContents);
        }
        catch (Exception ex)
        {
            Utility.WalkException(this, ex, "There was an error while performing this operation.");
        }
}
Community
  • 1
  • 1
Leo
  • 14,625
  • 2
  • 37
  • 55
  • yes i am using the RadTabStrip control. I will try to modify the code as you suggested. And will also wait for you example.:P Thanks. – Sachin Trivedi Jul 02 '14 at 09:04
  • @SachinTrivedi just added an example...but don't just copy/paste...think about how it should integrate to your solution – Leo Jul 02 '14 at 09:05
  • This will always cause the problem. On Init or Load event because the Multi Page Control will not have any page views inside that. So the index will be out of bound. – Sachin Trivedi Jul 02 '14 at 10:06
  • this is useful explanation so up voting it. but there seems some other problem in my case. Thanks for your concern. may be i will try to dig further and come up with real cause. – Sachin Trivedi Jul 02 '14 at 10:53
  • In that case then you'll have to provide some extra info. What are you doing with your PageViews? How are you creating them? Declaratively or imperatively? – Leo Jul 03 '14 at 01:20
  • I am gethering some information about user like email and phone number and when i click on the next button it will load the next page view. So its much more like wizard. – Sachin Trivedi Jul 03 '14 at 09:09
  • @SachinTrivedi what mean is...how are you creating the PageViews, dynamically? or are you declaring them in the page's mark-up? – Leo Jul 04 '14 at 01:28
  • i have already mentioned that i am creating them dynamically. So when my page loads for the first time in RadMultiPage1_PageViewCreated i am loading my first user control. Then when the user fills the details required in that user control and press next i am loading the next user control. – Sachin Trivedi Jul 04 '14 at 06:07
  • @SachinTrivedi...there seems to a lot going on here and it's going to be difficult to help you unless all relevant code is posted. But, I would suggest to simply the your page by NOT creating the PageViews dynamically, declare them via markup and only load the child controls dynamically. That'll be easier for you to inspect the selected page and load the necessary controls accordingly – Leo Jul 04 '14 at 06:21
  • I have posted the solution which was causing the the issue. Thanks for your help. – Sachin Trivedi Jul 07 '14 at 07:06
0

Finally i found the issue. Please go through This Answer. This was exactly what was happening in my solution.

ASP.NET 4 now renders the HTML form element’s action attribute value as an empty string when a request is made to an extensionless URL that has a default document mapped to it. For example, in earlier releases of ASP.NET, a request to http://contoso.com would result in a request to Default.aspx. In that document, the opening form tag would be rendered as in the following example:

In ASP.NET 4, a request to http://contoso.com also results in a request to Default.aspx. However, ASP.NET now renders the HTML opening form tag as in the following example:

This difference in how the action attribute is rendered can cause subtle changes in how a form post is processed by IIS and ASP.NET. When the action attribute is an empty string, the IIS DefaultDocumentModule object will create a child request to Default.aspx. Under most conditions, this child request is transparent to application code, and the Default.aspx page runs normally.

However, a potential interaction between managed code and IIS 7 or IIS 7.5 Integrated mode can cause managed .aspx pages to stop working properly during the child request.

Add this to global.ascx to fix the issue.

void Application_BeginRequest(object sender, EventArgs e)
{
    var app = (HttpApplication)sender;
    if (app.Context.Request.Url.LocalPath.EndsWith("/"))
    {
    app.Context.RewritePath(
             string.Concat(app.Context.Request.Url.LocalPath, "default.aspx"));
    }
}

Hope this will be helpful.

Thanks.

Community
  • 1
  • 1
Sachin Trivedi
  • 2,033
  • 4
  • 28
  • 57