I would like to display the numbers in the Text Box as right aligned instead of left aligned as I find it to be the default. I would be very grateful if you could provide me some pointers as tohow I could acheive the same. I
am now used to creating Custom controls and so if I get some directions, I could make one for this purpose.
it's even better to put the needed CSS in an external file so it gets cached on the client. But this little sample, that makes use of the CssClass property of the TextBox control class so there's no need to use code behind:
This doesn't work for a label though. The style property specified for the label is just ignored and the default alignment of left is used. Any suggestions?
partham
Member
402 Points
118 Posts
Right Align text in Label and Text Box
May 15, 2006 07:19 AM|LINK
Dear Sir,
I would like to display the numbers in the Text Box as right aligned instead of left aligned as I find it to be the default. I would be very grateful if you could provide me some pointers as tohow I could acheive the same. I am now used to creating Custom controls and so if I get some directions, I could make one for this purpose.
Thanks in advance.
Regards,
orzeh
Contributor
4993 Points
897 Posts
Re: Right Align text in Label and Text Box
May 15, 2006 07:29 AM|LINK
all you have to do is use some CSS:
<input type="text" size="30" style="text-align: right" />
hope it helps
orzeh
partham
Member
402 Points
118 Posts
Re: Right Align text in Label and Text Box
May 15, 2006 10:17 AM|LINK
Dear Sir,
Thank you for the suggestion.
However, I need to use the System.Web.UI.WebControls.TextBox control.
Regards,
orzeh
Contributor
4993 Points
897 Posts
Re: Right Align text in Label and Text Box
May 15, 2006 10:28 AM|LINK
it doesn`t make any difference...
<asp:TextBox ID="MyText" runat="server" Style="text-align: right" />
orzeh
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Right Align text in Label and Text Box
May 15, 2006 05:44 PM|LINK
Or from the CodeBehind:
TextBox1.Style.Add("text-align", "right");
NC...
XIII
All-Star
182684 Points
23455 Posts
ASPInsiders
Moderator
MVP
Re: Right Align text in Label and Text Box
May 15, 2006 05:55 PM|LINK
Hi,
it's even better to put the needed CSS in an external file so it gets cached on the client. But this little sample, that makes use of the CssClass property of the TextBox control class so there's no need to use code behind:
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <style type="text/css"> .rightAlign { text-align:right; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox runat="server" ID="TextBox1" CssClass="rightAlign"></asp:TextBox> </div> </form> </body> </html>Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
partham
Member
402 Points
118 Posts
Re: Right Align text in Label and Text Box
May 17, 2006 03:39 AM|LINK
aditya_sen
Member
6 Points
3 Posts
Re: Right Align text in Label and Text Box
Oct 31, 2007 08:32 PM|LINK
This doesn't work for a label though. The style property specified for the label is just ignored and the default alignment of left is used. Any suggestions?
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Right Align text in Label and Text Box
Nov 01, 2007 11:14 AM|LINK
It still works. Your problem probably is that your Label is not wide enough to show the alignment. XIII's CSS solution also works.
///////////////////////////////
// aspx file
///////////////////////////////
<form id="Form1" method="post" runat="server">
<asp:label id="Label1" runat="server" Width="100%">Some text</asp:label>
</form>
///////////////////////////////
// aspx.cs file
///////////////////////////////
private void Page_Load(object sender, System.EventArgs e)
{
this.Label1.Style.Add("text-align", "right");
}
NC...
aditya_sen
Member
6 Points
3 Posts
Re: Right Align text in Label and Text Box
Nov 01, 2007 03:19 PM|LINK
You were right, NC. The width was the culprit. Thanks a ton - I spent a very frustrating half-a-day yesterday wrestling with this problem.