Masked Edit Extender Format Issue - asp.net

I am using an ASP.NET AJAX Masked Edit Extender to format phone numbers
<asp:TextBox ID="tbPhoneNumber" runat="server" />
<ajaxToolkit:MaskedEditExtender TargetControlID="tbPhoneNumber" Mask="(999)999-9999" MaskType="Number"
InputDirection="LeftToRight" ClearMaskOnLostFocus="false" ClearTextOnInvalid="false" runat="server" AutoComplete="false" />
On the page load event I am trying to populate the phone textbox as follows:
protected void Page_Load(object sender, EventArgs e)
{
tbPhoneNumber.Text = "(394)456-310";
}
So there is one number which is missing at the end to make it a valid phone number. When the page loads I expected the value in the textbox to be (394)456-310_ But it displays (_39)445-6310 Why is this happening?

Take a look at the following Question:
Databinding int32 to MaskedEditExtender enabled TextBox
This seems to be a common issue.

Related

runat server field not filled when clicking on button?

I'm using an aspx file as my main page with the following code snippet:
<input type="text" runat="server" id="Password" />
<asp:Button runat="server" id="SavePassword"
Text="Save" OnClick="SavePassword_Click"/>
Then in the code behind I use:
protected void SavePassword_Click(object sender, EventArgs e)
{
string a = Password.Value.ToString();
}
Setting Password.Value in the Page_Load works as expected, but setting a to the value results in an empty string in a regardless what I type in on the page befoere I click the save button.
Am I overlooking here something?
You should add label field with name labelShow in aspx page like this
<asp:Label ID="labelShow" runat="server"></asp:Label>
Then add the string into .cs file
protected void SavePassword_Click(object sender, EventArgs e)
{
string a = Password.Value.ToString();
labelShow.Text = a.ToString();
}
I can figure several errors :
You say something about Page_Load. You should always remember Page_Load is executed at every postback. So if you write something into the textbox at Page_Load, it will erase the value typed by user. You can change this behavior by veriyfing
if(!IsPostBack)
{
// Your code
}
around your Page_Load content.
input runat="server" is not the proper way to do TextBox in ASP.Net, in most cases you should use asp:TextBox
You don't do anything with your "a" string, your current code sample does nothing, whether the Password field is properly posted or not. You can do as suggested by Ravi in his answer to display it.
I suggest you to to learn how to use VS debugger, and to read a good course on ASP.Net Webforms to learn it.

ASP.Net AjaxControlToolkit CalendarExtender not updating Textbox in code behind

Have the following:
<asp:TextBox ID="txtStart" runat="server" Enabled="false"></asp:TextBox>
<asp:Image ID="ibDateS" runat="server" ImageUrl="../SystemImages/calendar.gif" ToolTip="Click to show calendar" AlternateText="Click to show calendar" CssClass="showpointer" />
<ajaxToolkit:CalendarExtender ID="ceStart" PopupButtonID="ibDateS" Format="dd/MM/yyyy" TargetControlID="txtStart" runat="server"></ajaxToolkit:CalendarExtender>
It all works ok on the DOM and the textbox gets updated with the new date BUT when I try to get the value in code behind i.e. txtStart.Text it still has the original value set on Page_Load.
Have I missed something?
EDIT:
TextBox set originally in Page_Load (yes contained in if(!IsPostback)):
txtStart.Text = DateTime.Now.ToString("dd/MM/yyyy");
Get it later like so:
DateTime dtStart = Convert.ToDateTime(txtStart.Text);
After a bit of research apparently its an issue with setting the textbox to readonly or enabled="false" on the page. Removing this and adding the following to page_load solved the problem:
txtStart.Attributes.Add("readonly", "readonly");
if you haven't use Page.IsPostBack property of page then plz use it and try to use you pageload code inside that. It seems that it may be the problem of Page.IsPostBack,Try for that
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Your code for databind...
}
}
Hope you understand and works for you..
Enabled false preventing it to post the latest value.

onchange for the asp.net textbox is not working

i have one text box beside which there is one date picker and one dropdownlist in one user control which i'm using in one aspx page.I want to enable the regular field validator of the dropdownlist if we select the date from the datepicker in the textbox otherwise it should it should be false.
So how i should go ??
Would something basically like this work for you:
Markup:
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="true"
OnTextChanged="MyTextBox_TextChanged">
<asp:RegularExpressionValidator ID="MyValidator" runat=server
ControlToValidate="MyTextBox"
ErrorMessage="Must be in correct format"
ValidationExpression="[a-zA-Z]" /><!-- use whatever regex you need here -->
Code behind:
protected void MyTextBox_TextChanged(object sender, System.EventArgs e)
{
MyValidator.IsEnabled = !String.IsNullOrEmpty(MyTextBox.Text);
}

how to run asp.net code from string variable at runtime?

i have following code in a String variable at run-time
<form runat="server">
<asp:textbox runat="server"
ID="a1"
Text="enter text here"
/>
<asp:button runat="server"
Text="enter text here"
/>
<br />
</form>
so how can i run it to at run time on a new page by clicking on some button or so?
earliest help will be highly appreciated.
You mentioned loading from a text file. I think you should review usercontrols. It is the preferred method to load asp.net content and controls from an included file.
http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx
Put this code into a user control, and pass the control name as a string instead.
void Page_Init(object sender, System.EventArgs e)
{
//your string
string controlName = "TempControl_Samples1.ascx.cs";
// instantiate usercontrol object
MyControl myControl1 = (MyControl)LoadControl(controlName);
PlaceHolder1.Controls.Add(myControl1);
}

How to call server side code on textbox key enter in ASP.NET

How do I call server side code for textbox's in asp.net when user presses enter key? Code behnid method is creating a dynamic control based upon a LINQ query from data table.
The textbox has a TextChanged event that will fire when the focus leaves the textbox. If this isn't good enough for you, your going to need to use javascript and the onkeypress event.
As there's no description of what your doing after the event, I'm not sure what to offer as sample code.
In my opinion OP want to submit data with pressing enter in the textbox, then do sth with string from textbox.
So if I understand it right here is solution to do this:
//aspx
<asp:Panel ID="pnlSubmit" runat="server" DefaultButton="btnSubmit">
<asp:TextBox runat="server" ID = "txbText"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</asp:Panel>
//aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{
string textFromTextBox = txbText.Text;
//do sth with that string
}

Resources