 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

March 31st, 2005, 01:29 AM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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)
|

April 1st, 2005, 03:52 AM
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
<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
|

April 13th, 2005, 11:00 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|

April 22nd, 2005, 04:03 PM
|
Registered User
|
|
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 23rd, 2005, 02:29 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|

April 25th, 2005, 09:12 AM
|
Registered User
|
|
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 27th, 2005, 08:53 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks
|

April 4th, 2006, 04:55 PM
|
Registered User
|
|
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why not just use CSS?
(Many details omitted)
.wideInput input
{
width: 100px;
}
...
<asp:BoundColumn ... >
<ItemStyle CssClass="wideInput">
</asp:BoundColumn>
...
|
|
 |