Returning value from composite custom web control

Discussion in 'ASP .Net Building Controls' started by Bostonasian, Dec 11, 2005.

  1. Bostonasian

    Bostonasian Guest

    Lads,
    I've created a custom web control that's consisted of dropdown box and
    text box.
    In drop down there are following items:
    [text] | [value]
    "is" | "{val}"
    "starts with"| "{val}%"
    "ends with" | "%{val}"
    "contains" | "%{val}%"

    Upon postback, I want to replace {val} with what's entered in text box.

    Therefore if "sky" being entered in text box and "ends with" selected
    in dropdown list,
    "%sky" will be returned.

    In custom web controls, I've got following code,

    public class SearchTextBox : Table,INamingContainer
    {
    ConditionDropDown conDd = new ConditionDropDown();
    TextBox searchTxtBx = new TextBox();
    protected override HtmlTextWriterTag TagKey
    {
    get { return HtmlTextWriterTag.Table; }
    }
    protected override void CreateChildControls()
    {
    TableRow row = new TableRow();
    TableCell ddCell = new TableCell();
    TableCell txtBxCell = new TableCell();

    ddCell.HorizontalAlign = HorizontalAlign.Right;
    txtBxCell.HorizontalAlign = HorizontalAlign.Left;

    ddCell.Controls.Add(conDd);
    txtBxCell.Controls.Add(searchTxtBx);
    row.Cells.Add(ddCell);
    row.Cells.Add(txtBxCell);
    Rows.Add(row);
    }

    protected override void OnInit(EventArgs e)
    {
    conDd.ID = "ddCondition";
    searchTxtBx.ID = "txtBxSearchStr";

    base.OnInit(e);
    EnsureChildControls();
    }
    }

    What do I need to add/modify in order to return processed value in
    implemented page?
     
    Bostonasian, Dec 11, 2005
    #1
    1. Advertising

  2. Bostonasian

    Guest

    I would tap into the world of delegates and events. Think of the
    delegate as being a "tunnel" from one part of the application to
    another. The event will help with notification and the thing actually
    happening.
     
    , Dec 12, 2005
    #2
    1. Advertising

  3. The quickest way of getting what you want is to implement
    method/property on your control that does the replacement for you. For
    example, you can have something like this:

    public string ConstructCondition()
    {
    string conditionString = null;
    string searchToken = (searchTxtBx != null) ? searchTxtBx.Text :
    string.Empty;
    string searchPattern = (conDb != null) ? conDb.SelectedValue :
    string.Empty;

    if (searchPattern != null && searchPatter != string.Empty)
    {
    conditionString = searchPattern.Replace("{val}", searchToken);
    }

    return conditionString;
    }

    Frorm here, on your code behind were you handle your form's submit, you
    can then call the method directly:

    // Code behind for button click
    // This assumes that the ID for your SearchTextBox control is
    searchTxtBx
    string conditionString = searchTxtBx.ConstructCondition();

    // Check if you got something back
    if (conditionString != null)
    {
    // Do something with your string!
    }

    Hope this helps you out!
     
    Javier G. Lozano, Dec 12, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    739
  2. sleigh
    Replies:
    1
    Views:
    2,725
    sleigh
    Feb 12, 2004
  3. shapper
    Replies:
    6
    Views:
    970
    shapper
    Dec 26, 2007
  4. Replies:
    1
    Views:
    400
    Teemu Keiski
    Jul 2, 2003
  5. Chad
    Replies:
    0
    Views:
    242
Loading...

Share This Page