Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 31st, 2005, 01:29 AM
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Change the properties of a textbox control?

I'm using a datagrid to update an Access database. The default width and rows appear in the datagrid. How can you modify it to a longer width and multiline?

Dim AnswerTextBox As TextBox = CType(E.Item.Cells(2).Controls(0), TextBox)
Dim Answer As String = Convert.ToString(AnswerTextBox.Text)
  #2 (permalink)  
Old April 1st, 2005, 03:52 AM
Authorized User
 
Join Date: Nov 2004
Location: , , Hong Kong.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<asp:DataGrid id="MyGrid" runat="server" AutoGenerateColumns="False" CellPadding="2" BorderColor="Black" AlternatingItemStyle-BackColor="#FFFFC0" HeaderStyle-BackColor="#AAAADD" PagerStyle-PrevPageText="W@" PagerStyle-NextPageText="U@" PagerStyle-HorizontalAlign="Right" OnPageIndexChanged="ChangePage" AllowPaging="True">
                <HeaderStyle backcolor="#AAAADD"></HeaderStyle>
                <PagerStyle nextpagetext="U@" prevpagetext="W@" horizontalalign="Right"></PagerStyle>
                <AlternatingItemStyle backcolor="#FFFFC0"></AlternatingItemStyle>
                <Columns>
                    <asp:BoundColumn DataField="?" HeaderText="?">
                        <HeaderStyle width="100px"></HeaderStyle>
                    </asp:BoundColumn>
                    <asp:BoundColumn DataField="mW" HeaderText="mW"></asp:BoundColumn>
                    <asp:BoundColumn HeaderText="y">
                        <HeaderStyle width="100px"></HeaderStyle>
                        <ItemStyle horizontalalign="Right"></ItemStyle>
                    </asp:BoundColumn>
                    <asp:BoundColumn DataField="^" HeaderText="^y">
                        <ItemStyle horizontalalign="Right"></ItemStyle>
                    </asp:BoundColumn>
                    <asp:BoundColumn DataField="?" HeaderText="?">
                        <ItemStyle horizontalalign="Right"></ItemStyle>
                    </asp:BoundColumn>
                </Columns>
            </asp:DataGrid>

look for HeaderStyle
default is multine line automaticalluy
and ignore text quoted

  #3 (permalink)  
Old April 13th, 2005, 11:00 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If your wish is to have a multiline textbox in edit mode, you'll need to change the column from a regular boundcolumn to a template column and put in an asp:textbox set to multiline textmode.

-Peter
  #4 (permalink)  
Old April 22nd, 2005, 04:03 PM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've just found what you might feel is a better solution; I certainly feel so. It's not perfect: I'd rather see an attribute or EditItemStyle template on the BoundColumn, but it works pretty nicely.

If you add an event handler to the grid's PreRender event, the TextBox will have already been created and you can modify its properties before it is rendered for the client. I am only latching the PreRender event from the Edit command in this sample, so the check on the editing flag is unnecessary here. However, if you were doing something else in that event, it would be required, so I included it. Also note that I am working with the second column.

I'm slightly dissatisfied that we have to specify the width and mode in the page class but this is the cleanest solution I've found. Scratch that -- while typing this up, I got enough of an itch to try deriving BoundColumn and adding a couple of attributes. It seems to work nicely. Using EditMode as the TextBoxMode enum type "just works", apparently using reflection somewhere internally to find the right type.

I'll leave both solutions in this post for academic purposes. If anyone knows of a situation where a BoundColumn will not have the TextBox as the first control, please chime in, as that's an assumption in the code below.

First solution: modify the cell programmatically
--
private bool editing = false;
private int editingIndex;
private Unit editingWidth = new Unit(20, UnitType.Em);

private void someDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  someDataGrid.EditItemIndex = e.Item.ItemIndex;
  BindGrid();
  editing = true;
  editingIndex = e.Item.ItemIndex;
  someDataGrid.PreRender += new EventHandler(someDataGrid_PreRender);
}

private void someDataGrid_PreRender(object sender, EventArgs e)
{
  if (editing)
  {
    ((TextBox)someDataGrid.Items[editingIndex].Cells[1].Controls[0]).Width =
      editingWidth;
    ((TextBox)someDataGrid.Items[editingIndex].Cells[1].Controls[0]).TextMode =
      TextBoxMode.MultiLine;
  }
}
--


Second, better solution: derive BoundColumn and leave it mostly untouched, adding a couple of attributes
--
namespace CustomWebControls
{
  [DefaultProperty("Text"),
    ToolboxData("<{0}:CustomBoundColumn runat=server></{0}:CustomBoundColumn>")]
  public class CustomBoundColumn : System.Web.UI.WebControls.BoundColumn
  {
    private string editWidth = "";
    public string EditWidth
    {
      get { return editWidth; }
      set { editWidth = value; }
    }

    private TextBoxMode editMode = TextBoxMode.SingleLine;
    public TextBoxMode EditMode
    {
      get { return editMode; }
      set { editMode = value; }
    }

    public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
    {
      base.InitializeCell (cell, columnIndex, itemType);
      if (itemType == ListItemType.EditItem)
      {
        if (editWidth != "")
          ((TextBox)cell.Controls[0]).Width = new Unit(editWidth);
        ((TextBox)cell.Controls[0]).TextMode = editMode;
      }
    }
  }
}
--
<cwc:CustomBoundColumn DataField="someField" SortExpression="sort_order" HeaderText="Some Header" EditWidth="20em" EditMode="MultiLine">
  <HeaderStyle Width="20em"></HeaderStyle>
  <ItemStyle Width="20em"></ItemStyle>
</cwc:CustomBoundColumn>
--

I hope it helps!

Thanks,
-Noah

  #5 (permalink)  
Old April 23rd, 2005, 02:29 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Noah,

That's a great solution!

How about making a small modification to spin thru the controls in the column controls collection and look for the textbox control. That way your column template could contain something before the textbox and not break your derived column.

-Peter
  #6 (permalink)  
Old April 25th, 2005, 09:12 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,

I'm sure that would be possible and, in fact, preferred as an explicit sanity check. I'm not sure of a case where the TextBox would not be the first control in the column but I do agree that a check should be performed.

As another note, it should be relatively simple to remove the attribute and add another template to the derived column type. Dino Esposito's article at http://msdn.microsoft.com/msdnmag/is...e/default.aspx should give enough reference for even the uninitiated to make that modification. It seems that, within the constructs of the control, something like a TextBoxStyle template would feel a bit more natural.

Thanks,
-Noah

  #7 (permalink)  
Old April 27th, 2005, 08:53 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks

  #8 (permalink)  
Old April 4th, 2006, 04:55 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why not just use CSS?
(Many details omitted)


.wideInput input
{
    width: 100px;
}

...


<asp:BoundColumn ... >
    <ItemStyle CssClass="wideInput">
</asp:BoundColumn>

...



 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change An Objects Properties Via Sub Routine Rood67 Access 6 October 12th, 2007 12:14 PM
function - change forms txtbox properties feets Access VBA 3 May 11th, 2007 10:05 AM
Change TextBox control location on aspx page minhpx General .NET 2 March 4th, 2005 08:29 AM



All times are GMT -4. The time now is 08:19 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.