Solved

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Looping through datagridview and dataset ? 6 24
How to read text with RegEx... 2 25
VS 2017 18 39
Easy filter aspnet 2 24
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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