Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Set Gridview Template Column Controls Attributes

Posted on 2007-04-09
10
645 Views
Last Modified: 2008-01-09
Hello Experts

I have a gridview that contains numerous template columns.  In the EditItemTemplate, I have a textbox with the id of Textbox1.  When working with the control in design mode, it is identified as GridView1.Column8Note.EditItemTemplate.TextBox1 however, when I use that same id in the code behind, it doesn't find it.  What I need to do is to dynamically change the maxlength attribute of this textbox control based on control data returned.  How can I do this in the code behind?

Thanks
0
Comment
Question by:ClaySeifert
  • 5
  • 5
10 Comments
 
LVL 18

Expert Comment

by:Priest04
ID: 18879667
you can access DataGridViewTextBox column buy its name directly

TextBox1.MaxInputLength=300

or buy its position/name in Columns collection

dgw.Columns(1).MaxInputLength=300
dgw.Columns("TextBox1").MaxInputLength=300

0
 

Author Comment

by:ClaySeifert
ID: 18890223
Sorry -- but this didn't correct my situation.  My textbox controls are in a template column in the edititem template.  Placeing the code that you suggested in the codebehind results in an compile error indicating that it cannot find the control TextBox1.
0
 

Author Comment

by:ClaySeifert
ID: 18921632
I've increased the points on this to 500.  I really need an answer to this ASAP.  
Thanks
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 18

Expert Comment

by:Priest04
ID: 18924871
I was not reposponding since, as you could see, I made a mistake thinking this was a windows application datagridview question, not ASP.NET gridview question.

I just installed web developer, and created a template for gridview and this is the code I found working:

For Each r As GridViewRow In GridView1.Rows
    CType(r.FindControl("TextBox3"), TextBox).MaxLength = 5
Next

I hope that this solution is what you were looking for.

Goran
0
 
LVL 18

Expert Comment

by:Priest04
ID: 18924899
Change TextBox3 to the name of your TextBox control.
0
 

Author Comment

by:ClaySeifert
ID: 18925193
Priest04

In which event did you place that code?

Thanks and please advise.
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 500 total points
ID: 18925732
I placed it in Load event, but no reason why you shouldnt put it in some other event.

Goran
0
 

Author Comment

by:ClaySeifert
ID: 18950229
Goran
Thank you for taking the time to try and help me.  As it turs out, the problem is not really a problem according to Microsoft.  By design, if a textbox is placed in a template column of a datagrid control, a number of attributes are not avialable INCLUDING the maxlength.  In order to accomplish what I desired, I had to use javascript as follows:

Code Behind:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim t As TextBox = CType(e.Row.FindControl("TextBox1"), TextBox)
If Not IsNothing(t) Then
t.Attributes.Add("onkeyup", "javascript:return ismaxlength(this,10)")
End If
End Sub


Javascript Code:

<script type="text/javascript">
function ismaxlength(obj,length){
var mlength = length
if ( obj.value.length > mlength)
obj.value = obj.value.substring(0, mlength)
}
</script>

The TextBox control code:

<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("LastName") %>'  TextMode="MultiLine"  ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>


0
 

Author Comment

by:ClaySeifert
ID: 18950233
Goran
I'm going to give you the points because you tried to help.

Thank you
0
 
LVL 18

Expert Comment

by:Priest04
ID: 18952585
Well, I dont know what have you read, but with the code I gave you, I have managed to change MaxLength. I tried it and worked. I am using Framework 2.0
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question