views:

359

answers:

2

I have the webform with dropdown list, label and textbox. Like below:

<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true">
</asp:DropDownList> 

<asp:Label ID="lblTest" runat="server" Text="Some text">
</asp:Label>
<asp:TextBox ID="edtTest" runat="server">
</asp:TextBox> 

I want to show/hide label and text box depending on the value selected on dropdown list. So I've added RadAjaxManader:

<rad:RadAjaxManager ID="RadAjaxManager1" runat="server">  
    <AjaxSettings>   
        <rad:AjaxSetting AjaxControlID="ddlTest">   
            <UpdatedControls> 
                <rad:AjaxUpdatedControl ControlID="lblTest" /> 
                <rad:AjaxUpdatedControl ControlID="edtTest" /> 
            </UpdatedControls> 
        </rad:AjaxSetting> 
    </AjaxSettings> 
</rad:RadAjaxManager>  

and procedure "SetupVisibility" which takes value from the dropdown list, does some walidations and desides whether to show or hide the label and the text box. When I use the procedure like this:

Protected Sub ddlTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTest.SelectedIndexChanged  
    SetupVisibility()  
End Sub 

it works good, but now I want to call SetupVisibility when the page is loaded:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    SetupVisibility()
End Sub

The problem occurs in such a scenario:

  1. SetupVisibility() hides textbox and label while page is loaded.
  2. I change value on ddlTest dropdown list.
  3. SetupVisibility() wants to show textbox and label, but then I get the error: Microsoft JScript - runtime error: Cannot update control with ID: ctl00_CPH1_lblTest. The control does not exist. Where is the problem?
A: 

Page_Init is fired when the page is initialising. During this event there is no guarentee that the controls have been created..

Try putting your code in the Page_Load as controls are guaranteed to have been created by then.

starskythehutch
I already do it in Page_Load. I've just made a mistake in my question. Corrected.
Lukasz Lysik
A: 

I've solved the problem. I've wrapped the textbox and the label into div and panel:

<div id="panelTest_DIV" runat="server">
  <asp:Panel ID="panelTest" runat="server">
    <asp:Label ID="lblTest" runat="server" Text="Some text..."></asp:Label><br />
    <asp:TextBox ID="edtTest" runat="server"></asp:TextBox>
  </asp:Panel>
</div>

and AjaxManager:

<rad:AjaxSetting AjaxControlID="ddlTest">
  <UpdatedControls>
    <rad:AjaxUpdatedControl ControlID="panelTest_DIV" />
  </UpdatedControls>
</rad:AjaxSetting>

inside SetupVisibility() I set panelTest.Visibility. It solved the problem.

Lukasz Lysik