Search code examples
c#.netascx

.net ASCX help with passining info & Hiding Div


Alright, I am trying to accomplish this: When a user clicks a button that is on a ascx web user control with text boses, it first displays a DIV that is hidden, this div contains a ascx web user control. Basically I want that web user control to grab what they typed in the boxes on the first web user control, and then apply to a SQL search from what the users type in the text boxes on the first page. Is this possible or do I need to rethink my strategy on this? I am programming in c# for the SQL statements.


Solution

  • It is possible.

    You can define properties of the control which accepts the text input, and expose the values using direct field access, variables, or session variables; you can then use FindControl from within the newly displayed control, and, if found, utilise the now exposed properties to gather the values required.

    For instance, your input control code-behind might look something like this:

    partial class MyControl : UserControl
    {
        public string MyFieldValue
        {
            get { return MyFieldTextBox.Text; }
        }
    }
    

    And in the next control, to use it, a little like this:

    partial class MyControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var myControl = Page.FindControl("MyControlInstanceName") as MyControl;
            if (myControl != null)
            {
                var myFieldValue = myControl.MyFieldValue;
            }
        }
    }