Text Alignment in DataGrid Edit TextBox

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Nov 28, 2006.

  1. Guest

    When the EditCommandColumn in a DataGrid is clicked, all the
    BoundColumns get replaced by TextBoxes so that users can alter the
    data. By default, the Text in the TextBoxes are left-aligned. Is there
    any way by which the Text in some of the TextBoxes, not all, be
    center-aligned or right-aligned?

    Please note that I am referring to the alignment of the Text in the
    TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    DataGrid.
     
    , Nov 28, 2006
    #1
    1. Advertising

  2. Gozirra Guest

    Yes there is. Use a template column instead. Heres a short little
    example.

    <asp:TemplateColumn HeaderText="UserName">
    <ItemTemplate>
    <asp:Label ID="TheLabel" runat="server"><%#
    DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="TheTextBox" runat="server"
    style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
    "Data") %> />
    </EditItemTemplate>
    </asp:TemplateColumn>

    The style can be set to left, right or center. Of course you can also
    set the cssclass as well. This is probably a better option but it all
    works the same.

    You can set the itemstyle-cssclass on a bound column but this only
    applies to the display of the data within a label. When you switch to
    edit, the style class is not applied to the textbox.


    http://localhost/TestService1/WebForm2.aspx
    wrote:
    > When the EditCommandColumn in a DataGrid is clicked, all the
    > BoundColumns get replaced by TextBoxes so that users can alter the
    > data. By default, the Text in the TextBoxes are left-aligned. Is there
    > any way by which the Text in some of the TextBoxes, not all, be
    > center-aligned or right-aligned?
    >
    > Please note that I am referring to the alignment of the Text in the
    > TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    > DataGrid.
     
    Gozirra, Nov 29, 2006
    #2
    1. Advertising

  3. Guest

    Again it's my dear Gozirra & as expected, you have once again provided
    the solution I was looking out for.

    Gozirra, a DataGrid can be set in the editable mode using BoundColumns
    as well as EditItemTemplate. Which one would you suggest & why? Are
    there any performance issues involved using one over the other?


    Gozirra wrote:
    > Yes there is. Use a template column instead. Heres a short little
    > example.
    >
    > <asp:TemplateColumn HeaderText="UserName">
    > <ItemTemplate>
    > <asp:Label ID="TheLabel" runat="server"><%#
    > DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox id="TheTextBox" runat="server"
    > style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
    > "Data") %> />
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    >
    > The style can be set to left, right or center. Of course you can also
    > set the cssclass as well. This is probably a better option but it all
    > works the same.
    >
    > You can set the itemstyle-cssclass on a bound column but this only
    > applies to the display of the data within a label. When you switch to
    > edit, the style class is not applied to the textbox.
    >
    >
    > http://localhost/TestService1/WebForm2.aspx
    > wrote:
    > > When the EditCommandColumn in a DataGrid is clicked, all the
    > > BoundColumns get replaced by TextBoxes so that users can alter the
    > > data. By default, the Text in the TextBoxes are left-aligned. Is there
    > > any way by which the Text in some of the TextBoxes, not all, be
    > > center-aligned or right-aligned?
    > >
    > > Please note that I am referring to the alignment of the Text in the
    > > TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    > > DataGrid.
     
    , Nov 29, 2006
    #3
  4. Rocky Guest

    Hi,
    I'll suggest Edit Template mode because this is designed specially for
    Text box.

    Anshu
    wrote:
    > Again it's my dear Gozirra & as expected, you have once again provided
    > the solution I was looking out for.
    >
    > Gozirra, a DataGrid can be set in the editable mode using BoundColumns
    > as well as EditItemTemplate. Which one would you suggest & why? Are
    > there any performance issues involved using one over the other?
    >
    >
    > Gozirra wrote:
    > > Yes there is. Use a template column instead. Heres a short little
    > > example.
    > >
    > > <asp:TemplateColumn HeaderText="UserName">
    > > <ItemTemplate>
    > > <asp:Label ID="TheLabel" runat="server"><%#
    > > DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
    > > </ItemTemplate>
    > > <EditItemTemplate>
    > > <asp:TextBox id="TheTextBox" runat="server"
    > > style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
    > > "Data") %> />
    > > </EditItemTemplate>
    > > </asp:TemplateColumn>
    > >
    > > The style can be set to left, right or center. Of course you can also
    > > set the cssclass as well. This is probably a better option but it all
    > > works the same.
    > >
    > > You can set the itemstyle-cssclass on a bound column but this only
    > > applies to the display of the data within a label. When you switch to
    > > edit, the style class is not applied to the textbox.
    > >
    > >
    > > http://localhost/TestService1/WebForm2.aspx
    > > wrote:
    > > > When the EditCommandColumn in a DataGrid is clicked, all the
    > > > BoundColumns get replaced by TextBoxes so that users can alter the
    > > > data. By default, the Text in the TextBoxes are left-aligned. Is there
    > > > any way by which the Text in some of the TextBoxes, not all, be
    > > > center-aligned or right-aligned?
    > > >
    > > > Please note that I am referring to the alignment of the Text in the
    > > > TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    > > > DataGrid.
     
    Rocky, Nov 29, 2006
    #4
  5. Guest

    Rocky how do you say that the EditItemTemplate is designed specially
    for TextBox? In fact, instead of having a TextBox within the
    EditItemTemplate, I can even have a CheckBox within the
    EditItemTemplate. So I couldn't follow what makes you say that the
    EditItemTemplate is designed specially for TextBox.

    In fact, on the other hand, BoundColumns will always get replaced by
    TextBoxes when the DataGrid is in the editable mode. So I believe, on
    the contrary, BoundColumn is specially designed for TextBox, isn't it?

    Correct me if I am wrong.


    Rocky wrote:
    > Hi,
    > I'll suggest Edit Template mode because this is designed specially for
    > Text box.
    >
    > Anshu
    > wrote:
    > > Again it's my dear Gozirra & as expected, you have once again provided
    > > the solution I was looking out for.
    > >
    > > Gozirra, a DataGrid can be set in the editable mode using BoundColumns
    > > as well as EditItemTemplate. Which one would you suggest & why? Are
    > > there any performance issues involved using one over the other?
    > >
    > >
    > > Gozirra wrote:
    > > > Yes there is. Use a template column instead. Heres a short little
    > > > example.
    > > >
    > > > <asp:TemplateColumn HeaderText="UserName">
    > > > <ItemTemplate>
    > > > <asp:Label ID="TheLabel" runat="server"><%#
    > > > DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
    > > > </ItemTemplate>
    > > > <EditItemTemplate>
    > > > <asp:TextBox id="TheTextBox" runat="server"
    > > > style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
    > > > "Data") %> />
    > > > </EditItemTemplate>
    > > > </asp:TemplateColumn>
    > > >
    > > > The style can be set to left, right or center. Of course you can also
    > > > set the cssclass as well. This is probably a better option but it all
    > > > works the same.
    > > >
    > > > You can set the itemstyle-cssclass on a bound column but this only
    > > > applies to the display of the data within a label. When you switch to
    > > > edit, the style class is not applied to the textbox.
    > > >
    > > >
    > > > http://localhost/TestService1/WebForm2.aspx
    > > > wrote:
    > > > > When the EditCommandColumn in a DataGrid is clicked, all the
    > > > > BoundColumns get replaced by TextBoxes so that users can alter the
    > > > > data. By default, the Text in the TextBoxes are left-aligned. Is there
    > > > > any way by which the Text in some of the TextBoxes, not all, be
    > > > > center-aligned or right-aligned?
    > > > >
    > > > > Please note that I am referring to the alignment of the Text in the
    > > > > TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    > > > > DataGrid.
     
    , Nov 29, 2006
    #5
  6. Guest

    Gozirra, one thing I forgot to mention - the Text in the TextBoxes in
    the BoundColumns when the DataGrid is in the editable mode can also be
    center or right-aligned using this style

    <style>
    input{
    text-align:center;
    }
    </style>

    But the drawback of this is that all the TextBoxes in the page (be it
    inside or outside the DataGrid) will have their Text aligned at the
    center.


    Gozirra wrote:
    > Yes there is. Use a template column instead. Heres a short little
    > example.
    >
    > <asp:TemplateColumn HeaderText="UserName">
    > <ItemTemplate>
    > <asp:Label ID="TheLabel" runat="server"><%#
    > DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox id="TheTextBox" runat="server"
    > style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
    > "Data") %> />
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    >
    > The style can be set to left, right or center. Of course you can also
    > set the cssclass as well. This is probably a better option but it all
    > works the same.
    >
    > You can set the itemstyle-cssclass on a bound column but this only
    > applies to the display of the data within a label. When you switch to
    > edit, the style class is not applied to the textbox.
    >
    >
    > http://localhost/TestService1/WebForm2.aspx
    > wrote:
    > > When the EditCommandColumn in a DataGrid is clicked, all the
    > > BoundColumns get replaced by TextBoxes so that users can alter the
    > > data. By default, the Text in the TextBoxes are left-aligned. Is there
    > > any way by which the Text in some of the TextBoxes, not all, be
    > > center-aligned or right-aligned?
    > >
    > > Please note that I am referring to the alignment of the Text in the
    > > TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    > > DataGrid.
     
    , Nov 29, 2006
    #6
  7. Gozirra Guest

    >From what I've seen there are no performance hits using the
    edititemtemplate or the bound column. I usually use boundcolumns when
    I have no need for anything out of the ordinary. If all you require is
    simple text input, then the bound column fills the need. I use
    edititemtemplate for cases where you need greater control over the
    output. As you pointed out, you get greater flexibility with the
    ability to use a number of different controls within the
    edititemtemplate and greater control over the styles applied to those
    controls. So thats my 2 cents for what its worth.

    wrote:
    > Again it's my dear Gozirra & as expected, you have once again provided
    > the solution I was looking out for.
    >
    > Gozirra, a DataGrid can be set in the editable mode using BoundColumns
    > as well as EditItemTemplate. Which one would you suggest & why? Are
    > there any performance issues involved using one over the other?
    >
    >
    > Gozirra wrote:
    > > Yes there is. Use a template column instead. Heres a short little
    > > example.
    > >
    > > <asp:TemplateColumn HeaderText="UserName">
    > > <ItemTemplate>
    > > <asp:Label ID="TheLabel" runat="server"><%#
    > > DataBinder.Eval(Container.DataItem, "Data") %></asp:Label>
    > > </ItemTemplate>
    > > <EditItemTemplate>
    > > <asp:TextBox id="TheTextBox" runat="server"
    > > style="text-align:center" Text=<%# DataBinder.Eval(Container.DataItem,
    > > "Data") %> />
    > > </EditItemTemplate>
    > > </asp:TemplateColumn>
    > >
    > > The style can be set to left, right or center. Of course you can also
    > > set the cssclass as well. This is probably a better option but it all
    > > works the same.
    > >
    > > You can set the itemstyle-cssclass on a bound column but this only
    > > applies to the display of the data within a label. When you switch to
    > > edit, the style class is not applied to the textbox.
    > >
    > >
    > > http://localhost/TestService1/WebForm2.aspx
    > > wrote:
    > > > When the EditCommandColumn in a DataGrid is clicked, all the
    > > > BoundColumns get replaced by TextBoxes so that users can alter the
    > > > data. By default, the Text in the TextBoxes are left-aligned. Is there
    > > > any way by which the Text in some of the TextBoxes, not all, be
    > > > center-aligned or right-aligned?
    > > >
    > > > Please note that I am referring to the alignment of the Text in the
    > > > TextBoxes & NOT the alignment of the TextBoxes within the cells in the
    > > > DataGrid.
     
    Gozirra, Nov 30, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. San Diego Guy
    Replies:
    0
    Views:
    551
    San Diego Guy
    Aug 7, 2003
  2. Schultz
    Replies:
    3
    Views:
    568
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. Andreas Klemt
    Replies:
    0
    Views:
    310
    Andreas Klemt
    Oct 8, 2003
  4. Sharon McCarty
    Replies:
    0
    Views:
    253
    Sharon McCarty
    Nov 24, 2004
  5. Frank Kurka
    Replies:
    8
    Views:
    312
Loading...

Share This Page