I upload an image with AsyncFileUpload control.
On UploadedComplete I try to assign some value to a hidden field using ScriptManager.RegisterClientScriptBlock.
html:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="false">
</asp:ToolkitScriptManager>
<div>
<asp:AsyncFileUpload ID="uplImage" OnUploadedComplete="uplImage_UploadedComplete"
runat="server" Width="285px" ClientIDMode="AutoID" EnableViewState="false" />
<asp:HiddenField runat="server" ID="hdnTempImgNm" />
<input type="button" id="btnCheck" value="Check" class="btnCheck" />
</div>
event handler:
protected void uplImage_UploadedComplete(object sender, EventArgs args)
{
//1 case:
//ScriptManager.RegisterClientScriptBlock(uplImage, uplImage.GetType
// (), "myscript",
//String.Concat(" $(document).ready(function(){$('input:hidden
// [name*=\"hdnTempImgNm\"]').val('Some value');})"),true);
//2 case:
ScriptManager.RegisterClientScriptBlock(uplImage, uplImage.GetType
(), "myscript",
String.Concat("top.document.getElementById('" + hdnTempImgNm.ClientID
+ "').value='Some value'; "),true);
}
javascript:
<script>
$(document).ready(function () {
$('.btnCheck').click(function (e) {
alert($('input:hidden[name*="hdnTempImgNm"]').val());
});
})
</script>
In the 1 case when I click on the Check button to check a hidden filed value I get an empty value. But in the 2 case where I use top.document I get the right value i.e. “Some value”.
My questions are:
1. Why the 1 case doesn’t work?
2. How can I rewrite the 2 case using jquery (i.e. how to write top.document)?
Thank you
AsyncFileUpload is using iframe. Because of that only top.document works in this case.
See link http://www.mikeborozdin.com/post/AJAX-File-Upload-in-ASPNET-with-the-AsyncFileUpload-Control.aspx
So the first case should be like this
ScriptManager.RegisterClientScriptBlock(uplImage, uplImage.GetType(), “myscript”, String.Concat(” $(document).ready(function(){$(‘input:hidden[name*=\”hdnTempImgNm\”]’, window.parent.document).val(\”Some value\”);})”),true);