What I want to achieve is once a user have entered wrong data, an image label will be displayed.
Visible method is not recommended due to my lblMessage serve for other purposes as well.
My big big problem now is once users corrected their input field, the label message is disabled but the image still visible, just because i set my lblMessage to null.
Is there any method I can use like when there are something is lblMessage invoke image CSS but whenever there is nothing in lblMessage, no css is invoked?
if (!Utils.mtdIsBlank(Session["Message"]))
{
lblMessage.Text = Session["Message"].ToString();
Session["Message"] = null;
}
else
{
lblMessage.Text = "";
}
Seems like your problem is that you don't know how to add/remove styles to your asp.net controls: You can use CssClass for asp.net controls (label, panel,...) like this:
lbl.CssClass = "new-class";
For your example, something like this should work for you:
if (!Utils.mtdIsBlank(Session["Message"]))
{
//If is not blank no image
lblMessage.Text = Session["Message"].ToString();
Session["Message"] = null;
lblMessage.CssClass = "no-img";
}
else
{
//Show alert image
lblMessage.Text = "";
//Replace with-img with the css class you are using
lblMessage.CssClass = "with-img";
}
Then you need to add a css property:
.no-img{
background: none;
//Anything else
}
Related
I want to implement simple toggle functionality on a ToolStripButton.
View Mode --> Edit Mode
Edit Mode --> View Mode
This piece of code is working fine except that ToolTip doesn't refresh. I tried AutoToolTip property. I also tried setting ToolTipText to string.Empty. But my toolstrip continues to show old tooltip text. ToolTipText should also change when I toggle.
private void btnTrackingMode_Click(object sender, EventArgs e)
{
if (_currentSheetTrackingMode == SheetTrackingMode.ViewMode)
{
_currentTrackingMode = TrackingMode.EditMode;
btnTrackingMode.AutoToolTip = true;
btnTrackingMode.ToolTipText = string.Empty;
btnTrackingMode.ToolTipText = "You are currently in Edit Mode. Click here to enter into View Mode";
btnTrackingMode.Image = ((System.Drawing.Image) ((Image) new ComponentResourceManager(typeof(MyForm)).GetObject("btnTrackingEditMode.Image")));
btnTrackingMode.ImageTransparentColor = System.Drawing.Color.Magenta;
}
else
{
_currentTrackingMode = TrackingMode.ViewMode;
btnTrackingMode.AutoToolTip = true;
btnTrackingMode.ToolTipText = string.Empty;
btnTrackingMode.ToolTipText = "You are currently in View Mode. Click here to enter into Edit Mode";
btnTrackingMode.Image = ((System.Drawing.Image) ((Image) new ComponentResourceManager(typeof(MyForm)).GetObject("btnTrackingViewMode.Image")));
btnTrackingMode.ImageTransparentColor = System.Drawing.Color.Magenta;
}
}
You can try to refresh the button's parent ToolStrip
btnTrackingMode.ToolTipText = "something";
//put your toolstrip's name here
buttonsToolStrip.Refresh();
You should be able to access the parent tool strip through btnTrackingMode.Owner if it's not directly available for some reason.
I have a gridview that is bound to a sql database. As the user enters data they must indicate whether that cell's information is complete or not. To do this they enter /end/ at the end of their statement and it will automatically change the cell color. If nothing is entered then nothing happens.
Here is the code:
if (dataItem != null)
{
var label = dataItem["Status"].FindControl("Statuslbl") as Label;
if (label != null)
{
var item = dataItem;
var text = label.Text;
if (text.Contains("/end/"))
{
item["Status"].BackColor = Color.Lime;
item["Status"].Text = item["Status"].Text.Replace(#"/end/", #"");
}
else
{
item["Status"].BackColor = Color.Salmon;
}
}
}
Instead of just hiding the '/end/' like I need it to, it hides the entire cells contents.
How can I go about fixing this?
Discovered all I would need to do would be the following to achieve my result:
if (text.Contains("/end/"))
{
item["Test"].BackColor = Color.Lime;
item["Test"].Text = label.Text.Replace("/end/", " ");
}
Really simple, I just needed to use label.Text.
I am trying to change the color of an empty textbox, I have more than one textbox on this form and i wish to highlight the empty one when a user clicks submit. I have written the loop below which is in my btnSubmit function after checking if all the textbox have a value. Can anyone help in completing this loop for me??
foreach (Control txtbxs in this.Controls)
{
if (txtbxs is TextBox)
{
var TBox = (TextBox)txtbxs;
if (TBox.Text == string.Empty)
{
TBox.ForeColor = Color.Red;
}
}
}
lblTopError.Text = "Please fill in the missing billing information";
pnlTopError.Visible = true;
When your string is empty, changing the ForeColor will do nothing since you don't have Text to display in red. Consider using BackColor and remember to have an event when text is entered to switch it back to the appropriate BackColor.
If this is what you are trying to do, have you considered using the error provider? this would help you to signal the user and prompt them to put in the information.
errorProvider= new System.Windows.Forms.ErrorProvider();
errorProvider.BlinkRate = 1000;
errorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
private void TextValidated(object sender, System.EventArgs e)
{
var txtbox = Sender as TextBox;
if(IsTextValid(txt))
{
// Clear the error, if any, in the error provider.
errorProvider.SetError(txtbox, String.Empty);
}
else
{
// Set the error if the name is not valid.
errorProvider.SetError(txtbox, "Please fill in the missing billing information.");
}
}
You can apply any CSS you want like this:
TBox.Attributes.Add("style", "color: red; border: solid 1px #FC3000")
I would use this instead of:
TBox.ForeColor = Color.Red;
Well since there aren't much textboxes in this form, i went the easy route and it worked, code bellow:
List<TextBox> boxes = new List<TextBox>();
if (string.IsNullOrWhiteSpace(txtFname.Text))
{
//highlightTextBox= txtFname;
boxes.Add(txtFname);
}
if (string.IsNullOrWhiteSpace(txtLname.Text))
{
//highlightTextBox = txtLname;
boxes.Add(txtLname);
}
if (string.IsNullOrWhiteSpace(txtAddOne.Text))
{
//highlightTextBox = txtAddOne;
boxes.Add(txtAddOne);
}
if (string.IsNullOrWhiteSpace(txtTown.Text))
{
//highlightTextBox = txtTown;
boxes.Add(txtTown);
}
if (string.IsNullOrWhiteSpace(txtPostCode.Text))
{
//highlightTextBox = txtPostCode;
boxes.Add(txtPostCode);
}
foreach (var item in boxes)
{
if (string.IsNullOrWhiteSpace(item.Text))
{
item.BackColor = Color.Azure;
}
}
lblTopError.Text = "Please fill in the missing billing information highlighted below";
pnlTopError.Visible = true;
I'm having a DropdownList and when its Selected Value is changed (for ex: 0 ) I need to set the visible property of a Panel to True and the visible property of another Panel to False.
and when another Value is selected I need to do Vice Versa Using JAVASCRIPT.
I'm currently achieving this but the space remains as it is. How can i remove the spaces also.
can anyone help me??
I'm attaching the code also.
function visible(val) {
var ddl = document.getElementById("ddl_IDProof");
var selectedFilterType = drpFilterType.options[ddl.selectedIndex].value;
if (selectedFilterType == "0") {
document.getElementById("pnl1").style.visibility = "visible";
document.getElementById("pnl2").style.visibility = "hidden";
}
else {
document.getElementById("pnl1").style.visibility = "hidden";
document.getElementById("pnl2").style.visibility = "visible";
}
}
Use display instead of visibility.
This will hide the entire element:
// Show pnl1 (maybe you have to use inline or inline-block insdead of block)
document.getElementById("pnl1").style.display = "block";
// Hide pnl2
document.getElementById("pnl2").style.display = "none";
I have some link buttons in which I am dynamically adding a style to it. I am doing the following in a method:
LinkButton lb = new LinkButton();
lb.Style["font-weight"] = "bold";
When the another link is clicked, it should unbold the link button that is bold and bold the currently clicked one, so in the method that is doing this, I have tried:
lb.Style["font-weight"] = "none";
The above does not work though, the previously selected link stays bold.
I just realized the possible problem. I am creating multiple links and what it looks like is that since all the links are named lb, it never removes the bold. I am trying to think of a way for it to remember the previously selected link and to only unbold that one.
Can I suggest an alternative approach?
Set a CSS Style:
.selected { font-style: bold; }
When a link is clicked set that link's CSS class to "selected" and the others to "";
EDIT: To accommodate for existing Css Class
const string MY_CLASS = "links";
lb1.CssClass = MY_CLASS + " selected"; // selected
lb.CssClass = MY_CLASS; // not selected
You can quickly get into trouble when defining inline styles, in that they're difficult to overwrite.
EDIT 2:
Something like this code should work. You may have to loop through all the LinkButtons in the list, but I don't think so. I'd just turn off ViewState on the LinkButtons.
// container for links. so you can reference them
// outside of the creation method if you wish. I'd probably call this method in the
// Page_Init Event.
List<LinkButton> listOfLinks = new List<LinkButton>();
const string MY_LB_CLASS = "linkButton"; // generic lb class
private void createSomeLinks() {
for (int i = 0; i < 10; i++) {
// create 10 links.
LinkButton lb = new LinkButton()
{
ID = "lb" + i,
CssClass = MY_LB_CLASS
};
lb.Click += new EventHandler(lb_Click); // Add the click event
}
// You can bind the List of LinkButtons here, or do something with them.
}
void lb_Click(Object sender, EventArgs e) {
LinkButton lb = sender as LinkButton; // cast the sender as LinkButton
if (lb != null) {
// Make the link you clicked selected.
lb.CssClass = MY_LB_CLASS + " selected";
}
}
Try lb.Style.Remove("font-weight"). I didn't test it, but you can try it out.
Alternatively, have you tried settings the Font.Bold property?
lb.Font.Bold = true;
Try ListBox1.Attributes.Add("style","font-weight:bold");
and ListBox1.Attributes.Add("style","font-weight:normal");
or even better is
// css
.active {font-weight:bold}
.notactive {font-weight:normal}
//c#
ListBox1.CssClass = "active";
ListBox1.CssClass = "notactive ";
you could try lb.Style.Remove("font-weight");
set the font bold in the click event of the link button and set the enable view state property to false in the click event itself which will reset the link to the normal foam in the other click