Returning value from composite custom web control

B

Bostonasian

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?
 
A

agapeton

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

Javier G. Lozano

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top