Solved

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

Posted on 2007-11-19
5
1,054 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - TypeInitializer Error 25 25
vb.net help 4 28
Deploying to Azure 3 27
HTML - Color not displaying correctly in EMAIL. 6 32
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

19 Experts available now in Live!

Get 1:1 Help Now