Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Set Gridview Template Column Controls Attributes

Posted on 2007-04-09
10
Medium Priority
?
670 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

609 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