如何获取文本框的ClientID使用ListView和InsertTamplate或EditTemplate javascript函数 [英] How to get TextBox ClientID in a javascript function using ListView and InsertTamplate or EditTemplate
问题描述
我有ItemTemplate中,EditTemplate和InsertTemplate一个ListView。
有编辑和InsertTemplate几个文本框。我需要计算一些JavaScript中的价值,但我不知道,怎样在我的JS函数得到一个clientId或将其作为一个参数传递。
I have a ListView with ItemTemplate, EditTemplate and InsertTemplate. There are several TextBoxes in Edit and InsertTemplate. I need to calculate some of the values in Javascript, but I don't know, how to get a ClientID in my JS function or pass it as a parameter.
JavaScript函数:
Javascript function:
<script type="text/javascript">
function calculate() {
var length = document.getElementById('???').value;
var quantity = document.getElementById('???').value;
var fullLength = document.getElementById('???');
fullLength.value = length*quantity;
}
</script>
ASP.NET一块我的ListView:
ASP.NET piece of my ListView:
<InsertItemTemplate>
<asp:TextBox ID="Weight" runat="server" Text='<%#Bind("Weight") %>' />
<asp:TextBox ID="Quantity" runat="server" Text='<%#Bind("Quantity") %>' />
<asp:TextBox ID="FullLength" runat="server" Text='<%#Bind("FullLength") %>' />
<asp:Button runat="server" ID="Insert" Text="AddNewEntry" CommandName="Insert" OnClientClick="calculate()" />
</InsertItemTemplate>
是前人的精力,而不是???什么?
或者,我可以以某种方式通过它在的OnClientClick =的参数计算(???,???,???)?
What sould be instead of '???'? Or, can I pass it somehow in the parameters in the OnClientClick="calculate(???,???,???)"?
由于提前,JiKra
Thanks in advance, JiKra
推荐答案
现在的asp.net支持的ClientIDModepredictable您可以通过添加在EditItemTemplate中的更新按钮上的OnClientClick处理做到这一点。
Now that asp.net supports ClientIDMode "predictable" you can do this by adding an OnClientClick handler for the update button in the EditItemTemplate.
- 设置ListView的的ClientIDMode为predictable。
- 设置ListView的ClientIDRowSuffix为行的主键(例如,产品ID为罗斯文的Products表)
- 产品ID传递到处理程序的OnClientClick
- 使用的document.getElementById获取字段的当前值。
例如,假设你已经ListView1的必然罗斯文的Products表。对产品的主键是产品ID。为ListView的声明语法是:
For example, let's say you have ListView1 bound to Northwind's Products table. The primary key for Products is ProductID. The declarative syntax for the ListView is:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1" ClientIDMode="Predictable"
ClientIDRowSuffix="ProductID">
在EditItemTemplate中的更新按钮的声明语法是:
The EditItemTemplate's Update button's declarative syntax is:
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" OnClientClick=<%# string.Format("ShowProductName('{0}');", Eval("ProductID")) %> />
而处理的OnClientClick JavaScript是:
And the OnClientClick handler javascript is:
<script type="text/javascript">
function ShowProductName(prodId) {
var prodNameTextBox = document.getElementById('ListView1_ProductNameTextBox_' + prodId);
alert('product name is ' + prodNameTextBox.value);
return false;
}
</script>
请注意:假设产品名称都存储在一个名为ProductNameTextBox文本框。改变这种状况,以配合所需的字段文本框中的名称。 predictable模式将导致沿ListView1_YOURFIELDNAMEHERE_PRODUCTIDHERE的线路的ID。
Note: Assumes that the product name is stored in a textbox named ProductNameTextBox. Change that to match the name of the textbox with the field you want. Predictable mode will result in an ID along the lines of ListView1_YOURFIELDNAMEHERE_PRODUCTIDHERE.
有关详细信息,请参阅的http:// MSDN .microsoft.com / EN-US /库/ dd410598(v = VS.100)的.aspx
For details, see http://msdn.microsoft.com/en-us/library/dd410598(v=vs.100).aspx
这篇关于如何获取文本框的ClientID使用ListView和InsertTamplate或EditTemplate javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!