Solved

Set Gridview Template Column Controls Attributes

Posted on 2007-04-09
10
637 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

11 Experts available now in Live!

Get 1:1 Help Now