從服務器端,我向客戶端動態注入輸入和按鈕元素。我也想在每個中注入一個JavaScript函數。輸入的功能應該被觸發onchange
和按鈕的,應該是onclick
如何將JavaScript函數添加到代碼隱藏的HmlGenericControl中
這是我迄今所做的:
//js definition
string script = @"<script>";
script += @" function OnCroquis0BtnClick()
{
var fileUp = $('#File');
fileUp.trigger('click');
}";
script += @"function OnCroquis0FileSelected()
{
txtBx = document.getElementById('MainContent_txtBx');
var fileUpload = document.getElementById('File');
txtBx.value = fileUpload.files[0].name;
}";
script += @"</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "testScript0" ,
script);
//html elemets definition
HtmlGenericControl input = new HtmlGenericControl(),
btn = new HtmlGenericControl();
input.TagName = "input";
input.ID = lbl.Text + "File";
input.Attributes["onchange"] = "OnCroquis0Fileselected()";
input.Attributes["style"] = "display: none;";
docRequeridosMainDiv.Controls.Add(input);
btn.TagName = "button";
btn.ID = "btn";
btn.InnerText = "Anejar";
btn.Attributes["onclick"] = "OnCroquis0BtnClick(); return false";
btn.Attributes["style"] = "margin-right:2%;";
docRequeridosMainDiv.Controls.Add(btn);
他們是可見的,風格的被應用,但JS是不是。由於任何html屬性都是通過Attributes["attri"]
添加的,所以我認爲js函數可能也是如此。
謝謝,提前。