Solved

Attach Javascript Events to BoundFields in an asp:DetailsView

Posted on 2007-11-29
3
1,333 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to page 4 70
Resolve Dependency Issues 4 42
Why is some text in blue in Visual Studio? 6 22
ASP.NET 5 Templates 2 65
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

22 Experts available now in Live!

Get 1:1 Help Now