Saturday, March 24, 2012
Text Masking
String formatting in C# (http://www.stevex.org/CS/blogs/dottext/articles/158.aspx) -> C# article but still applies to VB.NET. It even has an example for phone numbers!
HTH
DJ
Thanks, that helps on the output. Still looking for thoughts on Input Masking as well. I've converting an old Access App to a SQL/ASP.NET app, and alot of the fields were masked to a certain input. I can do regular expression validators, but what a hassle to right them for every input and even then it doesn't seem as intuitive.
Could you give some more details of what you are doing - I don't understand why the above wouldn't work for the situation you describe. Perhaps I've misunderstood.
DJ
No, that will work great for when I output from the database. I'm just trying to figure out a way to mask data on input. For example, in this Access app I am converting to ASP.NET, for a phone number, it has an text field that looks something like: (___) ___-____ , where it restricts (masks) input.
Your best bet is to use javascript. I don't know if you are good with JS or not, but I am sure you could find sample code for formatting a phone number using it.
I recommend this because it will be 100% client side formatting, and won't be a drag on your webserver. If you were to make the input box run at the server, and put code in its textchanged event, each time they type a number in the box it would post back to your webserver. Javascript will simply do everything client side. it would be the onchange event of the input tag that you would want to perform the formatting. Basically you look at the entered string, get all the numbers in it, and if the numbers length is 10, then you have potentially a valid phone number and should format it, otherwise do not.
Thursday, March 22, 2012
TextBox
Two possible approaches. I prefer the first
Option 1. in your layout, add the needed rows, make them actual table rows. you will add runat=server to the rows and give them an id. When you need them to be visible simply do a postback with the add rows button and make them visible when you need them.
If you need to do it without a postback, simply hide / show them using javascript instead of running the <tr> on the server
Option 2
add a placeholder where you need the textboxes to be and then add the textboxes in the code. Here is a sample.
private void AddTextBox(int TextBoxNumber)
{
txtItem = new TextBox();
txtItem.ID = "txtItem" + TextBoxNumber.ToString();
thePlaceHolder.Add(txtItem);
}
I thought about the option 1. But what if the customer service wants to add more records. I don;t want to restrict the textbox instead have them request as they need. I tried using the second option and on the button click I do get 3 rows of text boxes but if I try to click the button again it doesn;t add more textbox after the 3 was already added. Does that make sense? let me know I will try and re phrase the question.
This post shows you how to dynamically add rows to a table server-side:
http://forums.asp.net/thread/1264968.aspx
NC...
You must re-add the controls on each subsequent post-back. To do this, you will need a counter, and you will need a method that will add the number of textboxes in the counter.
example: create a label on the page and set the value = 0 and visible = false. This will be your counter.
create a method that runs in page_load that creates the same number of textboxes in your counter. When you click to add a textbox, increment your counter and add your new text box. Viewstate will still remember the values in the textboxes.
I am really new to this. Could you possibly provide me a sample code. Thanks.
Thanks for the pseudocode. It worked. Now I have another question. ON similar lines to the previous one, How do I create a table or table row with two text boxes in each row (in 2 different columns) based on button click. Any suggestions.
The link that I posted earlier shows exactly how to do that.
NC...
Here is the code sample and it gives me an error on the highlighted code in dark gray , saying "Value of type 'System.Web.UI.WebControls.TableRow' cannot be converted to 'System.Data.DataRow'. Help Please
Id. is the a number that passsed in this function. It is the counter which occurs on the click of the button
------------------
Dim
iAsInteger = 0DoWhile (i < dynamicTable.Rows.Count)Dim dataRowAs DataRowDataRow = dynamicTable.Rows(i)
Dim newRowAs TableRow =New TableRowDim newCell00As TableCell =New TableCellDim textboxesAsNew TextBoxtextboxes.ID =
"ProductId" + Id.ToString()textboxes.Width = Unit.Pixel(32)
Dim literalControlAsNew LiteralControlliteralControl.Text =
"<p>"form1.Controls.Add(literalControl)
form1.Controls.Add(textboxes)
Dim newCell01As TableCell =New TableCellDim textboxes1AsNew TextBoxtextboxes.ID =
"ITemId" + Id.ToString()textboxes.Width = Unit.Pixel(32)
Dim literalControl1AsNew LiteralControlliteralControl.Text =
"<p>"form1.Controls.Add(literalControl)
form1.Controls.Add(textboxes)
newRow.Cells.Add(newCell00)
newRow.Cells.Add(newCell01)
Me.dynamicTable.Rows.Add(newRow)i = (i + 1)
LoopDuplicate post -- Please don't do that.
http://forums.asp.net/thread/1264968.aspx
NC...