?
Solved

Set Gridview Template Column Controls Attributes

Posted on 2007-04-09
10
Medium Priority
?
664 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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