![](/img/trans.png)
[英]How can I access runat="server" ASP element using javascript?
[英]How can I access to an HTML control runat server from javascript
我有以下代碼:
<telerik:GridTemplateColumn DataField="JOB_CODE"
<EditItemTemplate>
<input type="text" ID="JOB_CODETextBox" runat="server"
value='<%# Eval("JOB_CODE") %>' readonly="readonly"
onclick="$('#basic-modal-content').modal({
appendTo:'form', persist: true,
onClose: function (dialog)
{
/*
I want to assign here a value to the textbox control
like this: JOB_CODETextBox = 'something...'
I tried this:
$find('<%= JOB_CODETextBox.ClientID %>').value = 'something..'
but it didn't work!! the find function returns [null]
*/
$.modal.close();
}
} );" />
任何幫助!!
這應該有效:
$('#'+'<%= JOB_CODETextBox.ClientID %>').val('something');
或(僅限 C#):
$('<%= "#" + JOB_CODETextBox.ClientID %>').val('something');
或使用 JavaScript/ECMAScript:
document.getElementById('<%= JOB_CODETextBox.ClientID %>').value = 'something';
我不熟悉您正在使用的 Telerik 控件,因此我假設它與其他數據綁定控件類似。 考慮到這一點,這里是一個使用Repeater
控件的示例。
這是標記
<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
<input type="text" id="JOB_CODETextBox" runat="server" />
</ItemTemplate>
</asp:Repeater>
在這種情況下,我通常會生成 javascript 服務器端。
System.Text.StringBuilder js = new StringBuilder();
js.AppendLine(" <script>");
// we'll store all the control references in a list
// since there will be one for each item in the repeater
js.AppendLine(" var JOB_CODETextBox_list = [];");
for (int j = 0; j < this.rpt1.Items.Count; j++)
{
System.Web.UI.HtmlControls.HtmlGenericControl JOB_CODETextBox;
// try to locate the copy of the control local to each item
JOB_CODETextBox = (HtmlGenericControl)this.rpt1.Items[j].FindControl("JOB_CODETextBox");
if (JOB_CODETextBox != null) // make sure you found something
{
js.AppendFormat("JOB_CODETextBox_list.push(document.getElementById('{0}'));", JOB_CODETextBox.ClientID);
js.AppendLine();
}
}
js.AppendLine(" </script>");
this.Page.ClientScript.RegisterStartupScript(typeof(Page), "JOB_CODE", js.ToString(), false);
這應該生成一個腳本,該腳本獲取對中繼器中該輸入控件的所有實例的引用。 運行后,您可以訪問項目客戶端,例如
JOB_CODETextBox_list[n].value = 'something';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.