I want to change css from code-behind
If I have: <asp:TextBox ID="txt" CssClass="MyClass" runat="Server" />
I can do: txt.Visible = false; to hide the textbox.
But this will free up the space that the text box txt had.
Instead I want to do something like:
txt.css("display", "none");
How can I achieve this in asp.net code behind?
Thanks
Controls have a Style property which you can use to set specific CSS rules:
txt.Style["display"] = "none";
However specifying style in the html element directly is not recommended. Instead you might want to have a class, say hide, and append it to the control's css classes:
txt.CssClass += " hide";
Related
I have button in my user control like
<button class="show-form-button" runat="server" ID="btnShow">Please submit<span class="icon-down"></span></button>
In my code behind I am setting text of button but it is somehow removing span class when page is rendered
btnShow.InnerText = "Submitted";
How do I retain span class?
Innertext will replace the entire contents of the tag with the supplied text. To retain the icon, replace the inner HTML with "Submitted<span class='icon-down'></span>"
I have been having trouble updating the value for my input type=text or the texbox control text value with the jquery $(window).resize(function(){}); I know that the event fires because when i resize the browser an alert will appear. I also am using the functionality for something else.
It currently looks like this:
$(window).resize(function(){
if($(window).width()>1080){
var innerwidth = $(window).width()-170;
$("#div1").width(innerwidth);
}
I want to add this:
$(window).resize(function(){
if($(window).height()>500){
var innerheight = $(window).height();
$('input.hiddentest').val(innerheight);
}
I know that the issue lies with:
$('input.hiddentest').val(innerheight);
I have also tried this:
$('#texttest.ClientID').text(innerheight);
This is the input and the textbox below that I am using(note that the type used to be hidden, but i dont think that makes an issue and I wanted it to be visible for debugging purposes)
<input id="hiddentest" type="text" visible="true" name="hiddentest" onclick="test();" runat="server" value="1000" />
<asp:TextBox id="texttest" Visible="true" runat="server" Text="1000" />
Overall I have been looking for a way to dynamically update the values as the page resizes with the size of the page. My geuss is that i am not using the right thing to identify the id's. Thanks for taking the time to look at this and for any replies.
P.S. I am also open to the idea of using a javascript function instead but i can't even seem to get the function to fire for the resize event so it would require more help.
This is what i have so far:
window.onresize=Test();
function Test(){
var hdnfld= document.getElementById("texttest");
var testing = window.innerWidth;
alert(testing);
hdnfld.text= testing;
}
Use just ID of elements without dots (that actually represent the classes you don't have).
So use
$('#hiddentest').val(innerheight)
and
$('#texttest').val(innerheight)
Note that asp:TextBox renders as inptut type="text" so you still have to use .val() on it, not .text()
Hidden text box id is "hiddentest" so the code will be
$('#hiddentest').val(innerheight);
hiddentest is an id not a class in your case
Try,
$(window).resize(function(){
if($(window).height()>500){
var innerheight = $(window).height();
$('#hiddentest').val(innerheight);
}
});
<asp:TextBox id="texttest" Visible="true" runat="server" Text="1000" />
For the above asp.net textbox control, the ID changes dynamically when rendered (prepended with master and page information), id looks similar to main_ctrl100_texttest
var hdnfld= document.getElementById("texttest");, so this no longer holds good. Use a class instead.
<asp:TextBox id="texttest" Visible="true" runat="server" CssClass="texttest" Text="1000" />
var hdnfld = document.getElementsByClassName("texttest");
If you need more info on how to access .net controls using jquery, see here.
I have a Web Form project I am developing C# ASP.Net 4.5. I have a class that calls a response.write to display a message for user input validation purposes. The call to response.write is made inside the class in a method from creating a new instance of the class, thus the class method, by pressing a button on the form. But using the response.write causes the textboxes on my page to shrink considerably. Then when I press a different button the textboxes go back to normal. It only happens when I use response.write. Any help would be appreciated. Code call in class method:
HttpContext.Current.Response.Write("File not found");
By using that you're simply dumping text to the top of the page, typically outside of the <html> tags. This can have a knock-on effect to the rest of the pages style; i see the same when i am spitting out test responses.
Instead, put yourself a label control on your page and populate that instead. you can put it exactly where you want and simply call:
So put this: <asp:Label runat="Server" id="myLabel" /> where you want the message to appear.
Then in your code-behind, write this. It will populate the label with the given text.
myLabel.Text = "File not found";
The Label control will be rendered as a <span></span> - so styling it is nice and easy.
If you fancied using a <div> then use the Panel control.
If you're not fussed about any sort of style, go for a Literal control, which renders no html elements.
When you use the HttpContext.Current.Response.Write on code behind is direct send to the page your text, at any random point of page render.
Maybe on top, maybe on bottom, on some point that you can not control if you use the code behind to call it.
Change the way you show your message, at a minimum you can use a literal control to render there your output and show it.
You may want to use a control to display your error. For example:
In the aspx/ascx
<asp:Label id="ErrorMessage" runat="server" />
in the page/control code behind
//call TheClass
TheClass c = new TheClass();
string error = c.TheMethod();
if (!string.IsNullOrEmpty(error))
{
ErrorMessage.Text = error;
}
in TheClass
public class TheClass
{
public string TheMethod()
{
string result = "";
...
//When file is not found
result = "File not found";
...
return result;
}
}
If I did not have a rendering template I could add the following to my aspx page:
<asp:label ID="myLabel" runat="server" />
Then this in my code behind:
myLabel.Text = "Hello World";
But since my label is inside of a <SharePoint:RenderingTemplate> I am not able to access it the normal way.
Is there another way?
Edit: Ive found a number of articles like this one talking about creating a .dll for every rendering template. Is it really that complicated to output a string to a page? I should clarify that I am open to all ideas. I do not need code behind. I simply need to output a dynamic string to the template.
You can just create your own label control and work with it in any way you like.
There is an example here: Extending the Label Control, but it's basically like you would extend any other control.
What I would do then: Either set the .Text in that custom control directly, or what would be better is to use resource files. Then you could create your won property on the label like so:
<Custom:label ID="myLabel" specialresource="myLabel" runat="server" />
And in your class you could just handle the specialresource property and e.g. set the .Text to whatever you get from the resource.
I am using <asp:TextBox> not HTML textbox and I want to display hint text.
Is there any way to achieve that?
I have tried for making static text and color it gray but not getting how to make it empty when cursor get focus to that textbox.
<asp:TextBox ID="TextBox1" runat="server" placeholder="Hint Text"></asp:TextBox>
Use a ASP.NET WaterMark TextBox
It is in Ajax Toolkit
You, can give in Code behind like follows
textBox.Attributes.Add(“onfocus”, “clearText(this,’” + defaultText + “‘)”);
And also refer to this to know more.
You can using "placeholder" properties for textbox.
Example
<asp:TextBox ID="TextBox1" runat="server" Width="233px" placeholder="Search"></asp:TextBox>
You could use the HTML5 placeholder attribute for that. The downside is that it's only supported by some browsers. Fortunately you have JQuery Placeholder to the rescue. This plugin makes the placeholder behavior available for unsupported browsers.
Check it out here JQuery Placeholder
text input watermarks using javascript.
refer this it might help u
http://naspinski.net/post/Text-Input-Watermarks-using-Javascript-%28IE-Compatible%29.aspx
just add property placeholder="HintText" in your asp textbox