How to remove a class from a textbox that was changed - c#

ASP.net:
<asp:TextBox ID="tbFirst" OnTextChanged="AddClass" runat="server" CssClass="tbStyle colorBlue" Text='<%# Eval("theFirstName") %>'></asp:TextBox>
<asp:TextBox ID="tbLast" OnTextChanged="AddClass" runat="server" CssClass="tbStyle colorBlue" Text='<%# Eval("theLastName") %>'></asp:TextBox>
<asp:TextBox ID="tbAdd1" OnTextChanged="AddClass" ClientIDMode="Static" runat="server" CssClass="tbStyle colorBlue" Text='<%# Eval("theAddress1") %>'></asp:TextBox>
CSS:
.colorRed
{
color: #CC0000;
}
.colorBlue
{
color: #0000CC;
}
C#:
public void AddClass() {
//add the 'colorRed' class to the textbox that was changed and remove the 'colorBlue' class.
}
How can I remove one of the class and add a new class to the respective textbox that was changed.

Use JS instead of call server:
<asp:TextBox ... Onchange="addClass(this)"></asp:TextBox> <!-- fires after losing focus-->
<asp:TextBox ... Oninput="addClass(this)"></asp:TextBox> <!-- fires after key pressing-->
<script>
function addClass(sender) {
$(sender).addClass('colorRed');
}
</script>
It will be faster

You'll probably need to specify a valid Signature for the OnTextChanged event handler.
See below.
protected void AddClass(object sender, EventArgs e)
{
((TextBox)sender).CssClass = "tbStyle colorRed";
}
We changed the value of the CssClass

Related

ASP.Net jquery TextBox Event

Can I call textbox changed event as text changing using jquery in asp.net?
So, I want the action to be done while the user is entering the number.
Default.aspx
<asp:TextBox ID="txtCount" CssClass="form-control" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPrice" CssClass="form-control" runat="server" OnTextChanged="OnTextChanged"></asp:TextBox>
<asp:Label ID="lblTotal" CssClass="price" runat="server"></asp:Label>
Default.aspx.cs
protected void txtIskontoTutari_TextChanged(object sender, EventArgs e)
{
double price1=double.Parse(txtPrice.Text);
double count=double.Parse(txtCount.Text);
double total=0;
total=price1*count;
lblTotal.Text = string.Format("{0:#,##0.00}", total);
}
This could be the solution:
You have do take the ID Attribute of your asp tag.
$(document).ready(function () {
$('#txtPrice').change(function(){
$(this).....
}
});

Is there a Simple way to html decode several bound fields in formview?

Friends,
In my formview InsertTemplate we have several textboxes. When data is saved we run a loop to sanitize the data using a loop during ItemInserting:
Server.HtmlEncode(e.Values[a].ToString());
Thus, a value such as
<script> alert('Hello, World!') </script>
is converted and saved as
<script> alert('Hello, World!') </script>
We show the same value to the user at the time of EDITTEMPLATE. This is is done by firing up the below code attached to each text box in EDITTEMPLATE.
OnDataBinding="DecodeTextBox_DataBinding"
C# code:
((TextBox)sender).Text = HttpUtility.HtmlDecode(((TextBox)sender).Text);
Upon doing this the EDITTEMPLATE textbox shows exactly what user has saved, which is:
<script> alert('Hello, World!') </script>
The issue is that on and off we have to add additional text boxes per client's needs- there are too many such requests. Sometimes we forget to attach the databinding HtmlDecode to the new textboxes.
Is there a way to run a loop on EDITTEMPLATE using HtmlDecode?
Although, I would look into doing a cleaner solution. The issue is that the textbox control encodes an already encoded HTML string. So, to decode them in bulk, you may try something like this.
Having a formview like this:
<asp:FormView ID="myFormView" OnDataBound="myFormView_DataBound" DefaultMode="Edit" runat="server">
<EditItemTemplate>
<asp:TextBox runat="server" ID="TextBox1" Text='<%# DataBinder.Eval(Container.DataItem,"ItemA") %>'></asp:TextBox>
<asp:TextBox runat="server" ID="TextBox2" Text='<%# DataBinder.Eval(Container.DataItem,"ItemB") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Literal runat="server" ID="TextBox1" Text='<%# DataBinder.Eval(Container.DataItem,"ItemA") %>'></asp:Literal>
<asp:Literal runat="server" ID="TextBox2" Text='<%# DataBinder.Eval(Container.DataItem,"ItemB") %>'></asp:Literal>
</ItemTemplate>
</asp:FormView>
You could recursively HtmlDecode all textboxes inside of it, after databinding has been executed:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var data = new[]{
new DataObject(){
ItemA = "<div>Item 1</div>",
ItemB = "<div>Item 2</div>"
}
};
myFormView.DataSource = data;
myFormView.DataBind();
}
protected void myFormView_DataBound(object sender, EventArgs e)
{
var form = (FormView)sender;
HtmlDecode(form);
}
private void HtmlDecode(Control control)
{
if (control == null)
return;
foreach(var child in control.Controls.Cast<Control>())
HtmlDecode(child);
var textBox = control as TextBox;
if (textBox == null)
return;
textBox.Text = HttpUtility.HtmlDecode(textBox.Text);
}
}
public class DataObject
{
public string ItemA { get; set; }
public string ItemB { get; set; }
}

Passing variables to usercontrol inside the listview (asp.net, c#)

I want to pass some dynamic information from the listview to UserControl, but I guess I'm missing something.
.aspx page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource"
DataKeyNames="id_Image">
<ItemTemplate>
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description")%>' ID="info1" runat="server" />
</ItemTemplate>
</asp:ListView>
.ascx file:
Name:
<asp:Label ID="NameLabel" runat="server" />
Description:
<asp:Label ID="DescriptionLabel" runat="server" />
.ascx codebehind file:
public string Name_Lbl { get; set; }
public string Description_Lbl { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Text = Name_Lbl;
DescriptionLabel.Text = Description_Lbl;
}
Everything is working fine if Im trying to get value from string text like:
<uc1:Info Name_Lbl="Name" Description_Lbl="Description" ID="info1" runat="server" />
But when I'm trying to get value from Datasource, the string values in usercontrol are "null"
Can anyone see what I'm doing wrong? Thanks, Jim Oak
DataBinding occurs a lot later in the Control Life cycle than Load.
You assign your text on Load, but your control only receives the text on DataBind
To fix this you can set your text OnPreRender. This occurs after DataBind
protected override void OnPreRender(EventArgs e)
{
NameLabel.Text = Name_Lbl;
DescriptionLabel.Text = Description_Lbl;
}
Everything looks fine in your code just check the code line:
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description"%>' ID="info1" runat="server" />
You are missing the closing bracket ")" against Description_Lbl. It should be:
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description")%>' ID="info1" runat="server" />

Set innerHTML in javascript and get from C#

I have two labels:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
and I set innerHTML by javascript:
document.getElementById('Label1').innerHTML = position.lat();
document.getElementById('Label2').innerHTML = position.lng();
How I can get those labels values in codebehind? I try:
TextBox2.Text = Label1.Text;
UPDATE:I need to get pushpin location:
<artem:GoogleMap ID="GoogleMap1" runat="server"
EnableMapTypeControl="False" MapType="Roadmap" >
</artem:GoogleMap>
<artem:GoogleMarkers ID="GoogleMarkers1" runat="server"
TargetControlID="GoogleMap1" onclientpositionchanged="handlePositionChanged">
</artem:GoogleMarkers>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<script type="text/javascript">
var list = document.getElementById("Label1");
function handlePositionChanged(sender, e) {
printEvent("Position Changed", sender, e);
}
function printEvent(name, sender, e) {
var position = e.latLng || sender.markers[e.index].getPosition();
document.getElementById('Label1').innerHTML = position.lat();
document.getElementById('Label2').innerHTML = position.lng();
}
</script>
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = Label1.Text;// return value: Label
}
You cannot access the value on server side. You will have to use a hidden field for that:
<asp:HiddenField ID="Hidden1" runat="server" />
The set the innerHtml value in the Hidden field by doing:
document.getElementById('<%= Hidden1.ClientID %>').value = position.lat();
You can then access it from server side by doing:
TextBox1.Text = Hidden1.Value;
You are not able to do that with the Label control as when the page is posted back the content of labels are not posted to the server. You would need to make use of an input control of sorts. Probably a hidden input would be your best bet.
Take a hidden field like below
<asp:HiddenField ID="hdnBody" ClientIDMode="Static" runat="server" />
Then set its value in Jquery like below
<script>
function GetEmailID() {
var bodyHtml = $("#editor").html();
$("#hdnBody").val(bodyHtml);
}
</script>
And in the code behind do this to get it
string body = hdnBody.Value;

If statement in repeater

I have this repeater on my page..Under the default column what I want is that there
should be an IF condition that checks my table's "IsDEfault" field value.
If IsDefault=True then the lable below "label1" ie "Yes" should be
displayed inside the repeater else "Make DEfault" link should be displayed..
Now how do I include this IF statement as inline code in my repeater to accomplish what I want ?
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
I have an idea :-
<%# If DataBinder.Eval(Container.DataItem,"IsDefault") = "True"
Then%>
<%End If%>
How should I form the "Then" statement now?
Please help me with the proper syntax..thnx
Do I need to like make a method that checks if "IsDefault" is true or not and then call it inside of inline code in my repeater ? How do I go about it ?
[EDIT]
I tried as follows:-
<% If (Eval("Container.DataItem,"IsDefault"")="True"?
("<asp:LinkButton ID="lnk1" Text="Set as Default" CommandName="SetDefault1" runat="server" CommandArgument='<%#Eval("User1ID") %>'
CausesValidation="false" Visible=true></asp:LinkButton>") : ("<asp:Label ID="label1" Text="Yes" runat="server" Visible=true></asp:Label>")
)%>
didnt work :( Help!!
If you want some control to be visible only on some condition, set the Visible property according to that condition:
<asp:Label ID="label1" Text="Yes" runat="server"
Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault") %>" />
EDIT
If you want the control INvisible for the "IsDefault" situation, reverse the test with something like Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault")==False %>".
I'm not quite sure about the exact syntax, but you should get the idea.
Here's your repeater markup. Notice both controls are hidden at the start:
<asp:Repeater runat="server" ID="rpt1" OnItemDataBound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand">
<ItemTemplate>
<p>
ID: <%# Eval("Id") %>
IsDefault: <%# Eval("IsDefault") %>
Name: <%# Eval("Name") %>
<asp:Label BackColor="Blue" ForeColor="White" runat="server" ID="lDefault" Text="DEFAULT" Visible="false" />
<asp:Button runat="server" ID="btnMakeDefault" Text="Make Default" Visible="false" CommandArgument='<%# Eval("Id") %>' />
</p>
</ItemTemplate>
</asp:Repeater>
And some code to go with it. Note I've simulated the retrieval of your collection of blluser objects, so there is some additional code there relating to this which you won't require as, presumably the bllusers collection that you bind to is coming from a db or something?
Anyway I think this is what you're looking for, but let me know if its not ;-)
//Dummy object for illustrative purposes only.
[Serializable]
public class bllUsers
{
public int Id { get; set; }
public bool isDefault { get; set; }
public string Name { get; set; }
public bllUsers(int _id, bool _isDefault, string _name)
{
this.Id = _id;
this.isDefault = _isDefault;
this.Name = _name;
}
}
protected List<bllUsers> lstUsers{
get
{
if (ViewState["lstUsers"] == null){
ViewState["lstUsers"] = buildUserList();
}
return (List<bllUsers>)ViewState["lstUsers"];
}
set{
ViewState["lstUsers"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
buildGui();
}
}
private List<bllUsers> buildUserList(){
lstUsers = new List<bllUsers>();
lstUsers.Add(new bllUsers(1, false, "Joe Bloggs"));
lstUsers.Add(new bllUsers(2, true, "Charlie Brown"));
lstUsers.Add(new bllUsers(3, true, "Barack Obama"));
return lstUsers;
}
private void buildGui()
{
rpt1.DataSource = lstUsers;
rpt1.DataBind();
}
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
bllUsers obj = (bllUsers)e.Item.DataItem;//this is the actual bllUser the row is being bound to.
//Set the labels
((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault;
((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault;
//Or use a more readable if/else if you want:
if (obj.isDefault)
{
//show/hide
}
else
{
//set visible/invisible
}
}
}
Hope this helps :-)
Sorry to say you to be honest i was unable to understand what actually you wanted to do
If your looking to use the condition in the Item Templet then i think
the following systax will help you
<asp:LinkButton ID="Label1" runat="server"
Text='<%# ((Eval("Cond"))="True" ? Eval("Result for True") : Eval("Result for False") )%>'></asp:LinkButton>

Categories

Resources