I am trying to set the style of an asp:TextBox in codebehind, the textbox is style is set initially to
style="display:none"
when I set the dispaly to block in codebehind the textbox appears for a moment and then it's gone. I don't know what this problem is, when it's done in javascript it works fine
Here is the code:
asp.net code:
<asp:TextBox ID="txtError" style="display:none" runat="server" ReadOnly="True" Width="95%"></asp:TextBox>
codebehind:
txtError.Style["display"] = "block";
Am I doing anything wrong? Thanks in advance.
If you use
txtError.Visible = false;
You can not access the text box using JavaScript.
If you want to just change visibility you can use
txtError.Style.Add("display", "none");
Any .NET control has Visible property - you should use it in case you don't need control to be shown later (if Visible is set to false control won't be rendered at all).
Regarding your issue - I think there is some client (javascript) code that changes style of textbox back to display:none;
What about setting the Visible property?
txtError.Visible = false;
If this also doesn't work then somewhere else you will be re setting the value to none. Also check whether any of the parent elements of the textbox is not hidden.
Also no need to set the display of a textbox to block(if not intended so), use inline instead.
Related
I am creating a program with asp.net c#, i am using multiple gridviews to show different criteria of data on 1 page, each of these criteria has a title,
Now, if one of the gridviews happens to have no data it will not show, however the title of the gridview will still be there as it is purely html fieldset and legend.
Are there any functions i could use to trigger the visibility of the title from the c# code?
Thanks
(screenshot below)
You can add runat="server" to fieldset and set Visible = true or false as usual (of course you need to set an ID, too).
You can surround each grid in an <asp:Panel... with a visibility on the panel set to a server based property (e.g. Visibility=<%# RecordsReturnedGreaterThanZero %>
so that the panel will only render the contents if there are some records returned in your query. Simply provide a property in your codebehind that acts as the boolean operator
Im adding dynamically some aspxcombobox controls to my page. Each time I add one I add specified cssclass to it. Is it then possible to get that controls cssclass from javascript ?
Thanks for any hints
the CssClass is the class field on an html element. So to get it from javascript easily
document.getElementById("MyElement").className;
If the editor is inside a NamingContainer, for example, WebUserControl, or TabControl or any template, I would suggest that you set the editor's ClientInstanceName property. This property was specially designed to allow our users access the corresponding client side java script objects. For example, if the ClientInstanceName is set to the combo value, the code would be:
var class = combo.GetMainElement().className;
I have a label on a page and I'm updating the text property of the label (with a calculated value) when text is changed in a textbox.
I'm updating the label like so:
$myLabel.text("123");
The text is being displayed correctly on the screen butwhen I try to save the text value to an object in the code behind (When I press a button) the text property of the label is "" and not "123".
Code behind:
var myLabel = myLabel.Text;
//the var myLabel is "" when it should be "123"
Any ideas as to why this would be?
Thanks in advance,
Zaps
Not sure if this is the correct way to do it but I got around the problem by using a hidden field.
I updated the label text as above:
$myLabel.text("hello");
but then I updated the hidden field value:
$('#<%= hiddenField.ClientID %>').val("hello");
I was then able to use the hidden field in the code behind:
var myLabel = hiddenField.Value.ToString();
This seems to work fine.
Why don't you check the value that was entered in the textbox. based on your description, that should be the same and it will be available. Otherwise, I think you need to post some more code to clarify what you are doing.
The value of the label text needs to be stored in ViewState, otherwise it will be overwritten on the postback triggered by the button click.
One option would be to also change the value of a hidden control. Any changes to this value will be available in the code behind on postback.
<asp:Hidden id="hiddenLabel" runat="server" />
Html controls like labels, spans, divs don't post their values to the server - while inputs do. ASP.NET maintains changes in controls by using ViewState.
When you change the value of a server control, it's state is often persisted there. If you change the value on the client side via JavaScript, the ViewState isn't changed, and this is why on PostBack you get the original Empty value.
In what function are you putting var myLabel = myLabel.Text;?
It won't work in the init function -- you need to give the page time to load from the viewstate. Best in the button push event handler.
Update:
You need to use an form input control (eg TextBox) not label. Labels are readonly.
I have a select checkbox that has invisible set to false. ASPX looks like this:
<input name="selGR" id="selGR" type="checkbox" checked="checked" visible="false" runat="server" fieldname="GR"/>
The select box is not even rendered in the HTML which explains why JQuery is not finding it. Is there a way around this?
EDIT:
Setting style works! Thanks. I am curious to know if there are any rules the .NET framework follows when rendering controls with visible="false"? For all controls irrespective of whether they are server, custom or html controls, it follows this rule?
Also, any side effects of setting the style instead of setting the property of visible?
you can always set the display property to none. jQuery will be able to find it at that point.
<input type="checkbox" id="selGr" style="display:none" />
For future reference as well. $("#selGr").hide() & $("#selGr").show() basically uses the display property of the element.
I do not believe an 'visible' is an attribute of an element. There is visibility and display that can be set within the style attribute though. For a better understand of the difference between visibility and hidden check this out: http://www.devx.com/tips/Tip/13638
Visible is a asp.net attribute,by setting it to false is to ask sever not to render the control. That is why your check box is not in the html source code.
Chaning html display style property to none is telling the explorer not to display the checkbox, however, it is stilled included in the source code.
Use "style='display: none'" instead of "visible='false'". That way it will still be in the rendered HTML for JQuery to do something with.
it is the option that u can use "Display:None",
but if u use this one,
than ur control will render every time in page and if u see it in Firebug,
u will find ur control there and that thing will not occurs in Visible=false
I have a single webform that has a listbox and a button. When the onmousover event fires the listbox will appear; however on page load, the listbox should not be visible. I have the following code (please excuse the background color changes):
Button2.Attributes.Add("onmouseout",
"this.style.backgroundColor='Blue',
ListBox3.style.display ='none'");
Button2.Attributes.Add("onmouseover",
"this.style.backgroundColor='Red',
ListBox3.style.display='block'");
This code works when the listbox.visible is set to true. Unfortunately, when the page loads, the listbox is always visible, which is what I want to avoid. When I set the listbox to visible = false, the above code doesn't work. I've messed around with postback and used if statements, such as if (button = red), display=block; however, to no avail. I am stuck at this point. Does anyone know what additional things I need to do to get the above code to work? I am new with ASP.NET, so I don't know if I also have to do something with the html. Also, one interesting point, the backgroundcolor portion of the code works flawlessly.
I really appreciate everyone's help.
When you set Listbox visible="false" in the server-side code, the HTML for the listbox is not rendered and sent to the client. Therefore, you need to ensure that the listbox gets rendered, but is set to not be visible upon page load, either by
having an initial CSS style that makes the listbox not visible
or
setting it as not visible when the DOM has loaded, using JavaScript on the client.
Also have a look at this Display vs. Visibility article
You could use the jQuery javascript library for doing this pretty easily.
Instead of your code you would have something like:
$('#buttonid').hover(function(){
$(this).css('background','blue');
$('#listboxid').hide('fast');
}, function(){
$(this).css('background','red');
$('#listboxid').show('fast');
});
The first function of hover is a mouseover and second function is mouseout. You can also animate it showing and hiding which makes it look slicker.