Solved

Set Gridview Template Column Controls Attributes

Posted on 2007-04-09
10
641 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now