C# onClick Event on ASP.NET Button not initially fired but postsbacks… and eventually fired after multiple clicks

StackOverflow https://stackoverflow.com/questions/8229107

Pergunta

I'm having a weird issue and i'm not sure where to look or how to debug. I have a Parent Page A which has a Combo box control with 7 provinces; Each Province has a User Control ( View ). By default , the selected index is province "SK" which loads in the same page a user control View dynamically.

Below is my HTML and Code behind for the Parent Page

<telerik:RadComboBox ID="cmbProvince" runat="server" OnSelectedIndexChanged="cmbProvince_SelectedIndexChanged" AutoPostBack="true">
<Items>
<telerik:RadComboBoxItem runat="server" Text="Quebec" Value="QC" />
<telerik:RadComboBoxItem runat="server" Text="Ontario" Value="ON" />
<telerik:RadComboBoxItem runat="server" Text="Saskatchewan" Value="SK" />
<telerik:RadComboBoxItem runat="server" Text="Manitoba" Value="MB" />
<telerik:RadComboBoxItem runat="server" Text="Yukon" Value="YT" />
<telerik:RadComboBoxItem runat="server" Text="Nuvanut" Value="NU" />
</Items>
 </telerik:RadComboBox>

protected void Page_PreInit(object sender, EventArgs e)
{
    MasterPage master = this.Master;

    _currentProvince = GetCurrentOperatingProvince(IsPostBack);
    cmbProvince.SelectedValue = _currentProvince;
    txtReferenceNo.Text = "TECHLOS" + DateTime.Now.ToString("MMddyyyy-HHMMss");

    //Reload Dynamic Control on Every Page Creation
    LoadDetailControl();
}

private void LoadDetailControl()
{
    UserControl ucPPSAControl = (UserControl)LoadControl(GetUserControlPath(_currentProvince));
    IPPSAView provView = ucPPSAControl as IPPSAView;

    ApplicationContext.TargetProvince = _currentProvince;

    PPSAPresenter presenter = new PPSAPresenter(provView, ApplicationContext);
    provView.AttachPresenter(presenter, cmbProvince.SelectedValue, txtReferenceNo.Text);

    phPPSAControl.Controls.Add(ucPPSAControl);
}

    protected void cmbProvince_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        phPPSAControl.Controls.Clear();
        _currentProvince = e.Value;
        LoadDetailControl();
    }

In each UC by province, it has a Search button with an OnClick Event. The first time I land on the default province, the Search button works as expected and fires the OnClick and the Event Handler Search_Click is ran.

Below is that of a typical UC for each province

HTML button.
 <telerik:RadButton ID="btnSearch" runat="server" Text="Search" OnClick="Search_OnClick">
                            </telerik:RadButton>

and Code behind

        protected void Search_OnClick(object sender, EventArgs e)
        {
            if (isValidSearchRequest())
            {
                DisplayResultsFromResponse(presenter.Search());
            }
            else
            {
                phResponse.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>Invalid Search Request</span>")));
            }
        }

Now when I change the province to "ON" ( or any other province has the same behaviour), When I click on the Search Button on the newly generated UC , it only posts back without triggering the OnClick Event. Clicking Twice , does the same thing... and on the 3rd time, It does trigger the OnClick... and ran as normal!!!

This behaviour occurs whether I have "ON" View the first time it loads and "SK" View ( or any other View for that matter ) the sencond time, where i can assume something with the initial creation of the View makes the button work correctly... but subsequent reloading of a new control will cause the OnClick to fail.

Another point to note, Once i change the province, even if i come back to the original province, the OnClick won't work...Also I'm using Telerik controls with RadAjaxManager

How I can debug this to find out where my problem is... even better how can i fix this?

Foi útil?

Solução

After 3 days spent on this... it looks like the problem was related to the dynamic control . I created it without assigning an ID...so on recreation, somehow it posted back a few times probably because it was using the ASP's generated ID and each time it wasn't the same, so my onCLick on the button couldnt find the button that originally called it.. i guess on the 3rd time, the id is the same and thats when it called it properly.

Anyways, i know this fixed the problem i had

Outras dicas

after the ScriptManager place this code

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="cmbProvince" />
                <telerik:AjaxUpdatedControl ControlID="btnSearch" />
            </UpdatedControls>
        </telerik:AjaxSetting>

    </AjaxSettings>

may help you

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top