We help IT Professionals succeed at work.

GridView with ItemTemplate TextBox Right Align

Hi,

I'm trying to right align the textbox which is inside the GridView control. Here is the code:
         <asp:TemplateField HeaderText="Remaining Qty" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="10">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="TextBoxInvRemQty" Text='<%# String.Format("{0:###,##0}", Convert.ToDouble(Eval("InvQty"))) %>' 
                    BorderStyle="none" BackColor="transparent" Width="80px" ReadOnly="True" TabIndex="-1"></asp:TextBox>
            </ItemTemplate>

Open in new window


Can anyone help me on how to get the textbox right-aligned when there is lengthy number displayed for e.g: 2220 or 22220 or 22000000.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
You mean the text inside textbox?

Set css property

style="text-align: right"
Obadiah ChristopherDeveloper User Interface
CERTIFIED EXPERT

Commented:
Please try this.
<asp:TextBox onkeydown="javascript:setAlignment();" ID="rightAlignedTextBox" runat="server"></asp:TextBox>

<script type="text/javascript" language="javascript">
        function setAlignment(txtBox) {
            if (txtBox.value.length > 5) {
                txtBox.style.textAlign = "right";
            }
            else{
                txtBox.style.textAlign = "left";
            }
        }
    </script>

Open in new window

Hi All,

Thanks for your suggestions. I have modified my code by adding style, please see the code below:

         <asp:TemplateField HeaderText="Remaining Qty" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="10">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="TextBoxInvRemQty" Text='<%# String.Format("{0:###,##0}", Convert.ToDouble(Eval("InvQty"))) %>' 
                    BorderStyle="none" BackColor="transparent" Width="80px" ReadOnly="True" TabIndex="-1" [b]style="text-align:right;[/b]"></asp:TextBox>
            </ItemTemplate>
           
        </asp:TemplateField>

Open in new window

Author

Commented:
I have tried in multiple ways and added style inside the item template textbox.