This is a migrated thread and some comments may be shown as answers.

Update controls conditionally errors

1 Answer 176 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 05 Apr 2011, 11:54 PM
I am needing that depending on a condition verified in the code behind, I update certain controls by ajax. I researched some ways and I believe that would solve, is to add AJAX settings dynamically. But still could not solve the problem.

I have based on this example http://demos.telerik.com/aspnet-ajax/ajax/examples/manager/dynamicajaxsettings/defaultcs.aspx

An example to demonstrate the problems that can not solve, basically a ComboBox that selects which of the divs should be updated:

<telerik:RadScriptManager ID="telerik_ajax_manager" runat="server" OutputCompression="Forced" />
<telerik:RadAjaxLoadingPanel ID="ajax_Loaging_Panel" Enabled="true" runat="server" MinDisplayTime="1000" BackColor="#cccccc" Transparency="50" />
 
<div id="div_1" runat="server">
    <asp:Label ID="lbl_1" runat="server" Text="Div 1"></asp:Label>
    <br />
    Update:
    <telerik:RadComboBox ID="cbx_update" runat="server">
        <Items>
            <telerik:RadComboBoxItem Value="div_2" Text="Div 2" />
            <telerik:RadComboBoxItem Value="div_3" Text="Div 3" />
        </Items>
    </telerik:RadComboBox>
</div>
<br />
<div id="div_2" runat="server">
    <asp:Label ID="lbl_2" runat="server" Text="Div 2"></asp:Label>
</div>
<br />
<div id="div_3" runat="server">
    <asp:Label ID="lbl_3" runat="server" Text="Div 3"></asp:Label>
</div>
<br />
<telerik:RadButton ID="btn_submit" runat="server" onclick="btn_submit_Click" Text="Submit"></telerik:RadButton>
 
<telerik:RadAjaxManager ID="ajax_manager" runat="server" DefaultLoadingPanelID="ajax_Loaging_Panel">
<AjaxSettings>
    <telerik:AjaxSetting AjaxControlID="btn_submit">
        <UpdatedControls>
            <telerik:AjaxUpdatedControl ControlID="div_1" />
        </UpdatedControls>
    </telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>

protected void btn_submit_Click(object sender, EventArgs e)
{
    if (cbx_update.SelectedValue == "div_2")
    {
        lbl_2.Text = "Div 2 Updated";
        ajax_manager.AjaxSettings.AddAjaxSetting(btn_submit, div_2);
    }
    else
    {
        lbl_3.Text = "Div 3 Updated";
        ajax_manager.AjaxSettings.AddAjaxSetting(btn_submit, div_3);
    }
}

That the error occurs:
Erro: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_MainContent_ctl00_MainContent_div_2Panel'. If it is being updated dynamically then it must be inside another UpdatePanel.
Arquivo-fonte: http://localhost:10677/Telerik.Web.UI.WebResource.axd?_.......

This occurs because the telerik previously did not know what I would update divs. So do not create the Panels.

If I add these tags, even if this case does not make sense. works because the telerik creates the Panels, but I think we can bring me some trouble in the future.

<telerik:AjaxSetting AjaxControlID="div_3">
    <UpdatedControls>
        <telerik:AjaxUpdatedControl ControlID="div_2" />
        <telerik:AjaxUpdatedControl ControlID="div_3" />
    </UpdatedControls>
</telerik:AjaxSetting>


I think that's why the example of the telerik site works. (<telerik:AjaxUpdatedControl ControlID="DivExternalForm"...)

Following the recommendation of the error, add RadAjaxPanels on divs:

<telerik:RadAjaxPanel ID="pnl_2" runat="server">
    <div id="div_2" runat="server">
        <asp:Label ID="lbl_2" runat="server" Text="Div 2"></asp:Label>
    </div>
</telerik:RadAjaxPanel>
<br />
<telerik:RadAjaxPanel ID="pnl_3" runat="server">
    <div id="div_3" runat="server">
        <asp:Label ID="lbl_3" runat="server" Text="Div 3"></asp:Label>
    </div>
</telerik:RadAjaxPanel>


protected void btn_submit_Click(object sender, EventArgs e)
{
    if (cbx_update.SelectedValue == "div_2")
    {
        lbl_2.Text = "Div 2 Updated";
        ajax_manager.AjaxSettings.AddAjaxSetting(btn_submit, pnl_2);
    }
    else
    {
        lbl_3.Text = "Div 3 Updated";
        ajax_manager.AjaxSettings.AddAjaxSetting(btn_submit, pnl_3);
    }
}


But now another error occurs:
Erro: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.
Arquivo-fonte: http://localhost:10677/Telerik.Web.UI.WebResource.axd?_TSM_....

Searched these errors on the forums, but have not found a solution, can anyone help?

1 Answer, 1 is accepted

Sort by
0
Accepted
Iana Tsolova
Telerik team
answered on 08 Apr 2011, 03:00 PM
Hi milak,

The desired functionality could be hardly achieved with RadAjax. Because if you want some content to be updated after ajax, the ajax settings should be added before the ajax request started, but subsequently. However you can use ASP:UpdatePanel controls for that purpose. Wrap the different divs in separate ASP:UpdatePanel controls and set their UpdateMode property to Conditional. The on the server call the Update() methods for the particular ASP:UpdatePanel.

Best wishes,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Ajax
Asked by
Daniel
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or