开发者

.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.


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;
        }
    }
}


Is the 2nd user control embedded in the 1st or not?

If not, you can make anything available upwards between user controls by simply adding public properties to your user controls. This means they can then be accessed from the page level or the containing user control. For example, if I have UCA, UCB, UCC

UCA contains UCB and UCC is hidden.

UCB has the following property

public string UserEnteredName
{
   get { return NameTextBox.Text; }
}

UCC has the following property and method

public string UserEnteredName { get; set; }

public BindResults()
{
    UserEnteredLiteral.Text = UserEnteredName;
}

Then tie it together with UCA:

protected MyButton_Click(object sender, EventArgs e)
{
    UCC.UserEnteredName = UCB.UserEnteredName;
... some logic herre.
    UCC.BindResults();
}

You can also raise an event from UCB that can be responded to in UCA if your button or submit action exists in UCB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜