Solved

VB.Net DetailsView - when to add an attribute to a control

Posted on 2007-11-19
5
1,050 Views
Last Modified: 2013-11-07
I have a page with a DetailsView and I'm having a hard time adding a javascript function to it's child controls.  Originally I was using a GridView for this, and just added the script as an attribute on the rowDataBound event.  That's not working on the DataBound event for the DetailsView so I'm not sure what's the next most logical event.  

Thanks in advance,

N
0
Comment
Question by:stretch73
  • 3
5 Comments
 

Author Comment

by:stretch73
ID: 20312343
Okay, I've got it in the ItemCommand event as follows:

    Protected Sub dvProjectForecastDetails_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) Handles dvProjectForecastDetails.ItemCommand
        If e.CommandName = "Edit" Then

Dim txtM1Amt As TextBox = dvProjectForecastDetails.FindControl("txtM1Amt")
txtM1Amt.Attributes.Add("onkeypress", "return isNumberKey(event);")
txtM1Amt.Attributes.Add("onKeyUp", "calcTotal('" & sM1Prefix & "','" & sTotalPrefix & "','" & bAddRow & "');")

But it's not finding the textbox control.
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 250 total points
ID: 20312394
try using the load event, and then also checking the mode of the detailsview
    Protected Sub dvProjectForecastDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvProjectForecastDetails.Load

        If dvProjectForecastDetails.CurrentMode = DetailsViewMode.Edit Then

            Dim txtM1Amt As TextBox = dvProjectForecastDetails.FindControl("txtM1Amt")

            txtM1Amt.Attributes.Add("onkeypress", "return isNumberKey(event);")

            txtM1Amt.Attributes.Add("onKeyUp", "calcTotal('" & sM1Prefix & "','" & sTotalPrefix & "','" & bAddRow & "');")

        End If
 

    End Sub

Open in new window

0
 
LVL 6

Assisted Solution

by:ventaur
ventaur earned 250 total points
ID: 20312419
It depends on which template you are trying to work with. That DataBound event should work for controls when CurrentMode is Edit or ReadOnly. However, you can use ItemCreated for any mode to get at controls (so long as their existence does not depend on data being bound; i.e., you show a control based on the value of the data item when bound).

Just be sure to check the CurrentMode in ItemCreated so you know which controls to find before adding attributes.

If you were trying to use DataBound during Edit or ReadOnly, there was probably a problem with the hierarchy during your FindControl call. If that's the case, post your code, so we can have a look.
0
 

Author Comment

by:stretch73
ID: 20312470
Samtran,

I implemented your code but when I click on the edit button it goes into 'readonly' mode which I don't understand.  If I click the cancel button, the flow falls into that event in edit mode but as that closes the templates with the textboxes it doesn't help much.  

Ventaur,

This appears to apply to your suggestion as well.  

Here's the code:

Protected Sub dvProjectForecastDetails_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvProjectForecastDetails.ItemCreated

If dvProjectForecastDetails.CurrentMode = DetailsViewMode.Edit Then
Dim bAddRow As Boolean = False
Dim txtM1Amt As TextBox = dvProjectForecastDetails.FindControl("txtM1Amt")
txtM1Amt.Attributes.Add("onkeypress", "return isNumberKey(event);")
Dim sM1Prefix As String = txtM1Amt.ClientID.ToString
txtM1Amt.Attributes.Add("onKeyUp", "calcTotal('" & sM1Prefix & "','" & sTotalPrefix & "','" & bAddRow & "');")
End if
End Sub
0
 

Author Comment

by:stretch73
ID: 20312525
I take it back, that worked.  Must have had a cached version in front of me.  

Splitting points because I used both of your answers, thanks for the help.

N
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 117
Get String split 5 33
Protecting vb6 & .Net code Obfuscation 18 51
Different Delete Messages 7 17
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

21 Experts available now in Live!

Get 1:1 Help Now