ASP.NET DataGrid - how change edit textbox widths?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

Hi all,

I have an ASP.NET DataGrid wherein there is an edit link for each row. Upon
clicking the link, certan fields in that row display in text boxes so that
they may be edited. I would like some textboxes to be wider, some narrower.
By default, they are all the same pre-defined width.

Does anyone know how I can do this?

Thanks very much,
Ron
 
Hi Ronald,

I think that it depends of how you declare your grid, below you will find an
example of how I do it, you will see that I declare my controls in the way I
want.

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="550"
ItemStyle-HorizontalAlign="left" ItemStyle-CssClass="comunrow">
<itemtemplate>
<asp:Label Runat="server" Text='<%#
((CtpSentVia)Container.DataItem).Name%>' ID="Label23">
</asp:Label>
</itemtemplate>
<EditItemTemplate>
<asp:TextBox Width=100 CssClass="text" Runat="server" ID="SentViaName"
Text='<%# ((CtpSentVia)Container.DataItem).Name%>'></asp:TextBox>
<asp:CustomValidator id="Customvalidator2"
ControlToValidate="SentViaName"
OnServerValidate="ServerValidation"
ClientValidationFunction="IMValidator"
Display="Static"
ValidateIfBlank="yes"
ErrorMessage="*"
PopUPMessage="The Name can not be null"
runat="server"/>
</EditItemTemplate>
</asp:templatecolumn>

If you don;t use the TemplateColumn construction then you could set the
different parameters of the TextBox, the pseudocode below will give you an
idea:

protected void EditHandler( sender o, ... )
{
//get the selected row
int rowselected = grid.SelectedIndex;
TextBox textbox = (TextBox) grid[ rowselected].FindControl("TextBox
Name");
textbox.Width = 40;
}

Hope this help,
 
Thanks. If I use the template column instead, what to I change in my update
code? Currently, I reference the value in the edit textbox as:

string strFirstName = ((TextBox)e.Item.Cells[1].Controls[0]).Text

Now that it's a template column, how do I reference what the user has
entered (or may have changed)?

Thanks,
Ron



Ignacio Machin ( .NET/ C# MVP ) said:
Hi Ronald,

I think that it depends of how you declare your grid, below you will find an
example of how I do it, you will see that I declare my controls in the way I
want.

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="550"
ItemStyle-HorizontalAlign="left" ItemStyle-CssClass="comunrow">
<itemtemplate>
<asp:Label Runat="server" Text='<%#
((CtpSentVia)Container.DataItem).Name%>' ID="Label23">
</asp:Label>
</itemtemplate>
<EditItemTemplate>
<asp:TextBox Width=100 CssClass="text" Runat="server" ID="SentViaName"
Text='<%# ((CtpSentVia)Container.DataItem).Name%>'></asp:TextBox>
<asp:CustomValidator id="Customvalidator2"
ControlToValidate="SentViaName"
OnServerValidate="ServerValidation"
ClientValidationFunction="IMValidator"
Display="Static"
ValidateIfBlank="yes"
ErrorMessage="*"
PopUPMessage="The Name can not be null"
runat="server"/>
</EditItemTemplate>
</asp:templatecolumn>

If you don;t use the TemplateColumn construction then you could set the
different parameters of the TextBox, the pseudocode below will give you an
idea:

protected void EditHandler( sender o, ... )
{
//get the selected row
int rowselected = grid.SelectedIndex;
TextBox textbox = (TextBox) grid[ rowselected].FindControl("TextBox
Name");
textbox.Width = 40;
}

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Ronald S. Cook said:
Hi all,

I have an ASP.NET DataGrid wherein there is an edit link for each row. Upon
clicking the link, certan fields in that row display in text boxes so that
they may be edited. I would like some textboxes to be wider, some narrower.
By default, they are all the same pre-defined width.

Does anyone know how I can do this?

Thanks very much,
Ron
 
Hi,

Well you should not have to change anything, this is a line that I use in
the update handler:
item.Name = ((TextBox)e.Item.FindControl("SentViaName")).Text;


As you see I'm using almost the same construction than you , only that
instead of knowing the exact location of the control I use FindControl()

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Ronald S. Cook said:
Thanks. If I use the template column instead, what to I change in my update
code? Currently, I reference the value in the edit textbox as:

string strFirstName = ((TextBox)e.Item.Cells[1].Controls[0]).Text

Now that it's a template column, how do I reference what the user has
entered (or may have changed)?

Thanks,
Ron



"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:[email protected]...
Hi Ronald,

I think that it depends of how you declare your grid, below you will
find
an
example of how I do it, you will see that I declare my controls in the
way
I
want.

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="550"
ItemStyle-HorizontalAlign="left" ItemStyle-CssClass="comunrow">
<itemtemplate>
<asp:Label Runat="server" Text='<%#
((CtpSentVia)Container.DataItem).Name%>' ID="Label23">
</asp:Label>
</itemtemplate>
<EditItemTemplate>
<asp:TextBox Width=100 CssClass="text" Runat="server" ID="SentViaName"
Text='<%# ((CtpSentVia)Container.DataItem).Name%>'></asp:TextBox>
<asp:CustomValidator id="Customvalidator2"
ControlToValidate="SentViaName"
OnServerValidate="ServerValidation"
ClientValidationFunction="IMValidator"
Display="Static"
ValidateIfBlank="yes"
ErrorMessage="*"
PopUPMessage="The Name can not be null"
runat="server"/>
</EditItemTemplate>
</asp:templatecolumn>

If you don;t use the TemplateColumn construction then you could set the
different parameters of the TextBox, the pseudocode below will give you an
idea:

protected void EditHandler( sender o, ... )
{
//get the selected row
int rowselected = grid.SelectedIndex;
TextBox textbox = (TextBox) grid[ rowselected].FindControl("TextBox
Name");
textbox.Width = 40;
}

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Ronald S. Cook said:
Hi all,

I have an ASP.NET DataGrid wherein there is an edit link for each row. Upon
clicking the link, certan fields in that row display in text boxes so that
they may be edited. I would like some textboxes to be wider, some narrower.
By default, they are all the same pre-defined width.

Does anyone know how I can do this?

Thanks very much,
Ron
 
Back
Top