I am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. I have written the code for it inside TextBox_TextChanged event but it's not working. The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. Do I have to trigger the event manually? Here is a glimpse of what I am doing:
protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
{
String firstNameEntered = FirstN_TextBox.Text;
if (firstNameEntered != null)
{
if (firstNameEntered.Length <= 20)
{
MessageBox.Show("Inside text box");
}
else
{
}
}
else
{
FirstN_TextBox.Focus();
MessageBox.Show("Please fill the marked field");
}
change it like this:
From:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>
To:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>
Related
I'm trying to make a web page with a login screen. I have 2 textboxes that are used to type the username and the password. I'm using an AJAX Update Panel, so in case that username doesn't exist (I'm using SQL Server), it makes visible a label that shows the error. The problem is that when the user types in the username textbox, AutoPostBack fires and when you move to the password textbox, you have to click twice in order to start typing in the password textbox.
C# code:
protected void txtUsername_TextChanged(object sender, EventArgs e)
{
string msgError = businessLogin.checkUsername(txtUsername.Text);
if (msgError != "")
{
lblWrongUser.Text = msgError;
lblWrongUser.Visible = true;
btnLogin.Enabled = false;
}
else
{
lblWrongUser.Visible = false;
btnLogin.Enabled = true;
}
}
ASP.Net:
<asp:TextBox ID="txtUsername" clientidmode="Static" runat="server" OnTextChanged="txtUsername_TextChanged" AutoPostBack="True"></asp:TextBox>
I have GridView to display data as Label
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%# GetIcon((String)Eval("IS_ACTIVE"))%>' SkinID='<%# GetSkinId((String)Eval("IS_ACTIVE"))%>' />
</ItemTemplate>
c#
protected string GetSkinId(string name)
{
if (name == "Y")
{
return "sknActive";
}
else
return "sknInactive";
}
but I get error I can't set SkinID Programmatically, any idea how I can allow SkinID in code behind?
Updated
I decide to not make SkinID, so I'm doing this
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%# GetIcon((String)Eval("IS_ACTIVE"))%>'
ForeColor='<%# GetColor((String)Eval("IS_ACTIVE"))%>' />
</ItemTemplate>
And my function on c# to get color
protected string GetColor(string name)
{
if (name == "Y")
{
return "#99099";
}
else
return "#03211";
}
I get error that
string can not convert to System.Drawing
The error message is self explanatory. Based on the data source you have the control in your gridview is dynamically getting created and after this it is trying to set the SkinId property and thus the error.
You can achieve this when the row is getting created in your gridview. Yes you can use the RowCreated event of gridvew like this:-
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblIsActive = e.Row.FindControl("lblIsActive") as Label;
if(lblIsActive.Text == "Y")
lblIsActive.SkinID = "sknActive";
else
lblIsActive.SkinID = "sknInactive";
}
}
Please note that this won't work in RowDataBound event since this event is fired after the row is created and when a data row is bound to data.
Update:
First of all your assumption is wrong that we are looping actually we are not. We are simply handling the event which is raised by the gridview control. Anyways since now you have changed your mind and switched to ForeColor approach the problem with your code is that the ForeColor property expects a System.Drawin.Color enum but your passing a string thus the error. For correctint you will have to return Color instead of string like this:-
protected Color GetColor(string name)
{
if (name == "Y")
return Color.Red;
else
return Color.Green;
}
Here I am returning sample colors but you need to replace them with actual intended colors. If you just have the hex string and not sure about the Color enum value then you can use the method mentioned in this answer to do so.
The error message tells you what you need to do: move the initializing of the SkinID property to the Page_PreInit stage of the page lifecycle.
Basically, this entails adding the following event handler to your code behind:
protected void Page_PreInit(object sender, EventArgs e)
{
lblIsActive.SkinId = GetSkinID(IS_ACTIVE); // no need for eval here
}
I have a multiline textbox in c#/asp.net, which I want to add the number of each line when the user press Enter....
I tried to do that but it didn't work.
add a inputchanged event on the textbox, and whenever the input changes, see if the enter was pressed or not. If yes, then get the current text of textbox and append /n to it
Though not a foolproof solution - the following will add the line number.
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onKeyup="onTextBox1Change(this)"></asp:TextBox>
function onTextBox1Change(ele) {
if (event.keyCode == 13) {
var lnNumber = (ele.value.match(/\n/g) || []).length;
ele.value = ele.value + (lnNumber+1);
}
}
You will still need to handle the edit (for example what happens if the user goes back to first line and then presses enter from somewhere in between)
Add onkeypress event to the textBox like this
<asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
and in the EnterEvent method like this
function EnterEvent(e) {
if (e.keyCode == 13) {
// get the textbox text and append /n to it
}
}
It would be easier if you provide your code here. KeyDown event would help you in this case.
Codebehind:
private void txtBox1_KeyDown(object sender, KeyEventArgs e)
{
//Insert the code you want to run when the text changes here!
if(e.KeyData == Keys.Enter)
{
//try from the below
//txtShowCount = ViewState["count"] + 1;
txtBox1.Text = txtBox1.Text + "\r\n";
txtBox1.Text = txtBox1.Text + Environment.NewLine;
}
}
The above may help you if its not data binding
I'm doing a page navigation control. After typing a new number in text box and Enter,
void pageNoTextBox_TextChanged(object sender, EventArgs e)
The above event is called.But I am still getting the old text from the text box in the following statement
newPageNumber = Int32.Parse(pageNoTextBox.Text.Trim());
for example if the textbox had 1 and and I entered 12, what I am getting is 1 in the TextChanged event where as javascript returns new value.
<asp:TextBox ReadOnly="false" CssClass="txtfld_s" ForeColor="white" ID="pageNoTextBox" runat="server" Text="" AutoPostBack="true" CausesValidation="True" OnTextChanged="pageNoTextBox_TextChanged"></asp:TextBox>
This is the text box. Is there any way to get the newly entered text using C#?
Well I think this will do the thing you want.
private void pageNoTextBox_TextChanged(object sender, EventArgs e)
{
TextBox textbox = sender as TextBox;
if(textbox != null)
{
string theText = textbox.Text;
}
}
I have a username TextBox and a Label which should update to (V or X) when the TextBox text is changed. The label is updated only if, for example I press a button which automatically refreshes the page.
Here is the code:
<asp:TextBox ID="username" runat="server" OnTextChanged="checkUsername" Width="80%"></asp:TextBox>
<asp:Label ID="usernameCheck" runat="server" CssClass="checkL"></asp:Label>
And the aspx.cs
protected void checkUsername(object sender, EventArgs e)
{
if (username.Text.Length < 3 || username.Text.Length > 15)
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
if (myBl.checkUsername(Convert.ToString(username)))
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
//---Label = V (in green)
usernameCheck.Text = "\u2713";
}
}
}
Thanks for any help.
You need to add AutoPostBack="true" to your TextBox. This will cause it to post back and for that server side event to fire.
There are much better ways of doing what you are trying to accomplish though, most of which do not require a full page postback. I would try to make an AJAX call using the javascript change event, and using something like a callback method.