locked
How refer to element inside custom control? RRS feed

  • Question

  • User-634781587 posted

    Hi,

    I have a custom control that contains a hidden asp field. In my code behind file I need to get the value of the hidden field. My problem is that I cannot find a way to refer to the hidden field since it is inside the custom control. Only the custom control is visible to the code behind. I tried myCustomControl.FindControl("hiddenField") but I could not figure out how to get the value from the Control object.

    Can someone help me please?

    Kind Regards

     

     

     

    Monday, May 18, 2009 6:48 AM

Answers

  • User1289018300 posted

     do you find object or null using myCustomControl.FindControl("hiddenField")?

    If you find object thank clast it to HiddenField and get the value out of it

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2009 7:15 AM

All replies

  • User1289018300 posted

     do you find object or null using myCustomControl.FindControl("hiddenField")?

    If you find object thank clast it to HiddenField and get the value out of it

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2009 7:15 AM
  • User-634781587 posted

    Thanks! It works great!

    Monday, May 18, 2009 7:43 AM
  • User-634781587 posted

     Hi,

     

    I just realised that I still have a problem with FindControl. The hidden field is not found with JDialog.FindControl("hiddenField"). Since I can refer to JDialog, the controls inside it should be rendered and therefore accessible as well. Can you think of a reason why the hidden field cannot be found?

     Custom control rendered in class in separate library:

    1          protected override void RenderContents(HtmlTextWriter writer)
    2            {
    3                StringBuilder sb = new StringBuilder();
    4                
    5                sb.Append("&lt;div id=\"icon\" style=\"float:left;margin-right:2em;\"&gt;</div>");
    6                sb.Append("&lt;div id=\"message\"&gt;</div>");
    7                sb.Append("&lt;br /><br />");
    8                sb.Append("&lt;div id=\"cbDiv\" style=\"display:none\"&gt;<input id=\"cb1\" type=\"checkbox\"/>Do not display this message again</div>");
    9                sb.Append("&lt;br /><br /><br />");
    10               sb.Append("&lt;asp:Button ID=\"b1\" runat=\"server\" style=\"display:none\" OnClientClick=\"b1Click();return false;\"/>");
    11               sb.Append("&lt;asp:Button ID=\"b2\" runat=\"server\" style=\"display:none\" OnClientClick=\"b2Click();return false;\"/>");
    12               sb.Append("&lt;asp:Button ID=\"b3\" runat=\"server\" style=\"display:none\" OnClientClick=\"b3Click();return false;\"/>");
    13               sb.Append("&lt;asp:HiddenField ID=\"dialogResult\" runat=\"server\"&gt;</asp:HiddenField>");
    14               sb.Append("&lt;asp:HiddenField ID=\"checkboxInfo\" runat=\"server\"&gt;</asp:HiddenField>");
    15               sb.Append("&lt;asp:HiddenField ID=\"postback\" runat=\"server\"&gt;</asp:HiddenField>");
    16               sb.Append("&lt;/div>");
    17   
    18               writer.Write(sb.ToString());
    19           }
    

      

     Page load for the web form where the custom control is added:

    1    protected void Page_Load(object sender, EventArgs e)
    2            {
    3                string message = "Are you sure you want to delete the list?";
    4                button.OnClientClick = "CreateDialog(" + (int)JDialogIcons.Alert + ", " +
    5                                                         (int)JDialogSize.Medium + ", " +
    6                                                         (int)JDialogDuration.UntilClose + ", " +
    7                                                         (int)JDialogButtons.YesNoCancel + ", \"" +
    8                                                         message + "\", \"" +
    9                                                         button.ID +
    10                                                        "\");return false;";
    11      
    12               HiddenField postback = (HiddenField)JDialog.FindControl("postback");
    13               if (postback != null)
    14               {
    15                   postback.Value = Page.ClientScript.GetPostBackEventReference(button, string.Empty);
    16               }    
    17           }
    
      

    Regards

    Cecilia

     

    Tuesday, May 19, 2009 4:51 AM
  • User1289018300 posted

     I think you need to diclare postback object in your control

    Like

    public HiddenField  postback= new HiddenField ();

    At top of the class

    Tuesday, May 19, 2009 7:23 AM