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

x
?
Solved

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

Posted on 2007-11-19
5
Medium Priority
?
1,067 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 1000 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 1000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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