how to align some text left and some text right in textbox using asp.net c#

A Kumar 1 Reputation point
2022-08-09T11:41:11.173+00:00

Dear Sir,

I would like to display the some text left and some text in right side in textbox mode = MultiLine(in ASP.NET C#). I would be very grateful if you could provide me some pointers as how I could achieve the same.

I gone throw some links (https://stackoverflow.com/questions/27054673/right-and-left-alignment-richtextbox-c-sharp) and downloaded the dll "System.Windows.Forms" also but it is not worked well to me

ex code:-

If (...)
{
textBox1.TextAlign = HorizontalAlignment.Left;
textBox1.Text = " Blah Blah ";
}
else
{
textBox1.TextAlign = HorizontalAlignment.Right;
textBox1.Text = " Blah Blah Right";
}

Suggest me where I did the mistake and how to achieve this.

Thanks in advance.

Regards,

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,309 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Petrus 【KIM】 456 Reputation points
    2022-08-10T08:55:59.753+00:00

    In ASP.NET, try like follow

    Default.aspx

        <style>  
                .align-left {  
                    text-align: left;  
                }  
                .align-right {  
                    text-align: right;  
                }  
            </style>  
            <div class="jumbotron">  
                <h1>ASP.NET</h1>  
                <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>  
                <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="63px" CssClass=".align-left"></asp:TextBox>  
          
                <p><a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>  
            </div>  
    

    Default.aspx.cs

    protected void Page_Load(object sender, EventArgs e)  
    {  
    Style oStyle1 = this.TextBox1.ControlStyle;  
    oStyle1.CssClass = "align-right";  
      
    this.TextBox1.TextMode = TextBoxMode.MultiLine;  
    this.TextBox1.Text = "AAA\nBBB\nCCC";  
    this.TextBox1.ApplyStyle(oStyle1);  
    }  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 112.7K Reputation points
    2022-08-09T13:46:05.16+00:00

    To set the alignment, use

    textBox1.Style[HtmlTextWriterStyle.TextAlign] = "right";

    or

    textBox1.Style[HtmlTextWriterStyle.TextAlign] = "left";

    0 comments No comments

  3. CowieCowie 76 Reputation points
    2022-08-10T06:36:51.413+00:00

    Hi Akumar, Im a bit confused about your textbox mode but I believe this wont matter.
    After reviewing your code quote from StackOverFlow, I find that the code a little bit old for us to use.
    I create a WPF app in visual studio and add a window(WPF) to test my code, in my project I call this window WindowTextBox.
    Here`s my code in textbox
    WindowTextBox.xaml:

        <Grid>  
            <TextBox Name="textBox1" BorderThickness="5" Width="200" Height="150">  
            </TextBox>  
            <Button Width="60" Height="30" Click="Button_Click" VerticalAlignment="Bottom" Margin="0,0,0,50">  
                click me  
            </Button>  
        </Grid>  
    

    WindowTextBox.xmal.cs:

    public partial class WindowTextBox : Window  
        {  
            public WindowTextBox()  
            {  
                InitializeComponent();  
            }  
      
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                if (true) //notice!! you can change the boolean here to fit your own condition  
                {  
                    textBox1.TextAlignment = TextAlignment.Left;  
                    textBox1.Text = " Blah Blah Left";  
                }  
                else  
                {  
                    textBox1.TextAlignment = TextAlignment.Right;  
                    textBox1.Text = "Blah Blah Right";  
                }  
            }  
        }  
    

    You can see it in the .cs file that you need to replace "TextAlign" with "TextAlignment" and correspondingly, you need to replace "HorizontalAlignment" with "TextAlignment".
    Picture1 shows the origin status of the textbox, I use this button to trigger the move in textbox, in this case I set the text in textbox to left while the if condition is true (see picture2). If I set the condition to false, we can see that the text is in right position (see picture3).
    229748-p1.png229815-p2.png229808-p3.png

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Hope my code to be useful and helpful. If you have any problem about it, please comment my answer to discuss with me. If my answer solves your problem, please accept my answer and up vote it! Good day.


  4. Qi You 1 Reputation point
    2022-08-10T07:03:13.793+00:00

    Hi @A Kumar ,
    You can try to use the button control instead of if judgment, which will make the code very simple.
    Default.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)  
            {  
                TextBox1.Attributes.Add("OnClick", "this.value = '';");  
            }  
            public void LeftButton_Controller(object sender, EventArgs e)  
            {  
                TextBox1.Text = "Blah Blah";  
                TextBox1.Attributes.Add("style", "text-align:left");  
      
            }  
            public void RightButton_Controller(object sender, EventArgs e)  
            {  
                TextBox1.Text = "Blah Blah Right";  
                TextBox1.Attributes.Add("style", "text-align:right");  
            }  
    

    Default.aspx

    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" ></asp:TextBox>  
    <div>  
    <asp:Button  ID="LeftButton" Text="Left" runat="server" On Click="LeftButton_Controller" />  
    </div>  
    <div>  
    <asp:Button  ID="RightButton" Text="Right" runat="server" On Click="RightButton_Controller" />  
    </div>  
    

    when the left button is clicked:
    229790-image.png

    when the right button is clicked:
    229750-2.png

    When the text box is clicked, the content is cleared:
    229810-3.png

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  5. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2022-08-16T09:19:33.167+00:00

    Hi @A Kumar ,
    According to your further requirements, I think that it is a bit difficult to realize the left and right alignment of the text box at the same time. First of all, there is no such usage under the style of TextBox, so I use the method of text replacement.

      public void LeftButton_Controller1(object sender, EventArgs e)   
        {   
            string left = Request.Form["TextBox2"].ToString();   
            TextBox1.Text = TextBox1.Text.Replace("name1", left);   
        }   
        public void RightButton_Controller1(object sender, EventArgs e)   
        {   
            string right = Request.Form["TextBox2"].ToString();   
            TextBox1.Text = TextBox1.Text.Replace("name2", right);   
        }   
        public void LeftButton_Controller2(object sender, EventArgs e)   
        {   
            string left = Request.Form["TextBox2"].ToString();   
            TextBox1.Text = TextBox1.Text.Replace("string1", left);   
        }   
        public void RightButton_Controller2(object sender, EventArgs e)   
        {   
            string right = Request.Form["TextBox2"].ToString();   
            TextBox1.Text = TextBox1.Text.Replace("string2", right);   
        }   
        public void ClearButton_Controller(object sender, EventArgs e)   
        {   
            string To = "";   
            string From = "";   
            string string1 = "";   
            string string2 = "";   
            TextBox1.Text = "To:name1                              From:name2" + Environment.NewLine +   
                            "To:string1                              From:string2" + Environment.NewLine;   
        }   
    

    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"  Width="100%" Height="200" ></asp:TextBox>   
    <div>   
    <input type="text" name="TextBox2" />   
    <asp:Button  ID="LeftButton1" Text="To(name)" runat="server" On Click="LeftButton_Controller1" />   
    <asp:Button  ID="RightButton1" Text="From(name)" runat="server" On Click="RightButton_Controller1" />   
    <asp:Button  ID="LeftButton2" Text="To(string)" runat="server" On Click="LeftButton_Controller2" />   
    <asp:Button  ID="RightButton2" Text="From(string)" runat="server" On Click="RightButton_Controller2" />   
    </div>   
    <div>   
    <asp:Button  ID="ClearButton" Text="Show/Clear" runat="server" On Click="ClearButton_Controller" />   
    </div>   
    

    when the Show/Clear button is clicked:
    231501-image.png
    When we enter content in the text box and press the button To(name) at the same time:
    231497-image.png
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments