Solved

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

Posted on 2007-11-19
5
1,061 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
[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
  • 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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