Solved

Attach Javascript Events to BoundFields in an asp:DetailsView

Posted on 2007-11-29
3
1,335 Views
Last Modified: 2013-11-05
I'm not sure if this is possible, but if I have an <asp:DetailsView />, can I attach a javascript function to the onkeyup event of the text boxes (visible while your in Insert or Edit mode)?
<html>
<head>
<script type="text/javascript">
<!--
function formatPhone( obj ) {
	var str = obj.value.replace(/[^0-9]/g, '');
 
	if( str.length < 10 ) return;
 
	var str1 = str.substring(0,3);
	var str2 = str.substring(3,6);
	var str3 = str.substring(6,10);
 
	obj.value = '(' + str1 + ') ' + str2 + '-' + str3;
}
//-->
</script>
</head>
<body>
<asp:DetailsView ID="details" runat="server" AutoGenerateRows="False" CellPadding="4"
            DataSourceID="detailisSource" DataKeyNames="ID">
<Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" />
                <asp:BoundField DataField="First" HeaderText="First" SortExpression="First" />
                <asp:BoundField DataField="Last" HeaderText="Last" SortExpression="Last" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" /><!-- I want to attach to the onkeyup of this BoundField -->
                <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
                <asp:BoundField DataField="Street" HeaderText="Street" SortExpression="Street" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
                <asp:HyperLinkField InsertVisible="False" NavigateUrl="Customers.aspx" ShowHeader="False"
                    Text="Return to Customer List" />
            </Fields>
</asp:DetailsView>
 
<!-- Other Page Code -->
 
</body>
</html>

Open in new window

0
Comment
Question by:netsmithcentral
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Infinite_Recursion earned 500 total points
ID: 20377377
To achieve that, it is easier to use a TemplateField instead of a BoundField where you need to do something custom like that.
0
 
LVL 12

Author Comment

by:netsmithcentral
ID: 20377387
Can you give some code example?  I'm very new to ASP.NET and don't know about TemplateField's
0
 
LVL 7

Expert Comment

by:Infinite_Recursion
ID: 20377546
check this link for explanation on how to use them:
http://www.asp.net/learn/data-access/tutorial-13-vb.aspx
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ProductID" DataSourceID="ObjectDataSource1" AllowPaging="True" EnableViewState="False">  <Fields>  <asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />  <asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />  <asp:BoundField DataField="SupplierName" HeaderText="Supplier" ReadOnly="True" SortExpression="SupplierName" />  <asp:BoundField DataField="QuantityPerUnit" HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" />  <asp:TemplateField HeaderText="Price and Inventory">  <ItemTemplate>  <asp:Label ID="Label1" runat="server" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label>  <br />  <strong>  (In Stock / On Order: </strong>  <asp:Label ID="Label2" runat="server" Text='<%# Eval("UnitsInStock") %>'></asp:Label>  <strong>/</strong>  <asp:Label ID="Label3" runat="server" Text='<%# Eval("UnitsOnOrder") %>'>  </asp:Label><strong>)</strong>  </ItemTemplate>  </asp:TemplateField>  <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />  </Fields> </asp:DetailsView>

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert html to string 6 34
Entity Framework 7 41
daypilot scheduler 3 26
Pagebreak issue while printing the aspx page 3 24
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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