Set the maxLength property for a textBox inside myDatagrid in edit mode

I am using the built in edit buttonColumn in my datagrid. When click edit in the datagrid, I textBox appears on to be editted. I couldn't set the maxLength for that textBox. Here is my code:

TextBox txtName = ((TextBox)e.Item.Cells[1].Controls[0]);
txtName.MaxLength = 20;

I can set the property of maxLength but it is not limiting the text length.

Looking forward to your response.

Tx in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elvio Lujan.Net Senior DeveloperCommented:
where is you'r code?

"TextBox txtName = ((TextBox)e.Item.Cells[1].Controls[0]);
txtName.MaxLength = 20;"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chaleastaleAuthor Commented:
my code is in the update event handler for the datagrid. When I try it in the edit event handler for the datagrid, an exception happens saying Invalid cast.
Elvio Lujan.Net Senior DeveloperCommented:
TextBox txtName = ((TextBox)e.Item.Cells[1].Controls[1]);
txtName.MaxLength = 20;
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

chaleastaleAuthor Commented:
When I put your code for update event handler it is all the same. It doesn't restrict the the lenght I set.
When I put it in the edit event handler the exception says 'Specified argument was out of the range of valid values.'

I don't know what is going wrong!

Elvio Lujan.Net Senior DeveloperCommented:
are you sure the cellIndex is the correct?
chaleastaleAuthor Commented:
As long as it represents the column, Yes.

The column that I want to edit is the second column in the datagrid. That means its index is 1

I am afraid the built in edit button column doesn't allow to set the max legth for the textbox

Tx again
Elvio Lujan.Net Senior DeveloperCommented:
ummm... and the textbox is in the second cell?
Elvio Lujan.Net Senior DeveloperCommented:
may be if you can post the html code from the datagrid...
chaleastaleAuthor Commented:
Here is my datagrid html script:

<asp:datagrid id="dataGridEquipMaster" style="Z-INDEX: 107; LEFT: 16px; POSITION: absolute; TOP: 160px"
      runat="server" Width="296px" Height="208px" Font-Names="Arial" Font-Size="XX-Small" CellSpacing="1" AllowSorting="True" DataKeyField="Id"        AllowPaging="True" AutoGenerateColumns="False" ForeColor="Black"
BackColor="WhiteSmoke" BorderWidth="1px" BorderStyle="Solid" BorderColor="Wheat" PageSize="8" ShowFooter="True">
<FooterStyle Height="2px"></FooterStyle>
<AlternatingItemStyle HorizontalAlign="Left" Height="2px" VerticalAlign="Bottom"></AlternatingItemStyle>
<ItemStyle Font-Size="X-Small" HorizontalAlign="Left" Height="2px" VerticalAlign="Bottom"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Center"
Height="3px" VerticalAlign="Bottom" BackColor="Silver"></HeaderStyle>
<asp:BoundColumn Visible="False" DataField="Id" SortExpression="Id"><HeaderStyle Wrap="False"></HeaderStyle><FooterStyle Font-Size="XX-Small" Wrap="False"></FooterStyle></asp:BoundColumn>
<asp:BoundColumn DataField="Name" SortExpression="Name">
<HeaderStyle Wrap="False" Width="200px"></HeaderStyle><FooterStyle Font-Size="XX-Small"></FooterStyle></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="last_Updated" SortExpression="last_Updated" HeaderText="Last Updated"><HeaderStyle Wrap="False" Width="100px"></HeaderStyle><FooterStyle Font-Size="XX-Small"></FooterStyle>
</asp:BoundColumn><asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<HeaderStyle Wrap="False" Width="30px"></HeaderStyle><FooterStyle Font-Size="XX-Small"></FooterStyle>
</asp:EditCommandColumn><asp:ButtonColumn Text="Delete" HeaderImageUrl="../Imgages/delete_icon.gif" CommandName="Delete">
<HeaderStyle Wrap="False" Width="30px"></HeaderStyle></asp:ButtonColumn></Columns><PagerStyle VerticalAlign="Bottom" NextPageText="&gt;&gt;" Font-Size="X-Small" BorderColor="White"PrevPageText="&lt;&lt;" BorderStyle="None" HorizontalAlign="Right" BackColor="White" PageButtonCount="5"></PagerStyle>

I am sorry I is kinda bulky
chaleastaleAuthor Commented:
Did you something to the html script? I didn't see any comment of yours.
chaleastaleAuthor Commented:
I am sorry. Did you add something to the html script? I didn't see any comment of yours.
Jason ScolaroCommented:
Hi chaleastale,

Make your life easier with the use of a TemplateColumn, this way you can access the TextBox properties all you like without code-behind.  Just change your BoundColumn to this TemplateColumn:

<asp:TemplateColumn SortExpression="Name">
    <HeaderStyle Wrap="False" Width="200px"></HeaderStyle>
    <FooterStyle Font-Size="XX-Small"></FooterStyle>
        <asp:Label Runat="server" id="lblName" Text='<%# Container.DataItem("Name") %>' />
        <asp:TextBox Runat="server" id="txtName" MaxLength="20" Text='<%# Container.DataItem("Name") %>' />

That should do the trick.  Good luck!

-- Jason
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.