CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Problem with ASP.Net and Javascript

    Hi there,

    So I have a problem with ASP.Net and JavaScript.
    The problem is this:

    I am wanting to write the value of the upload form field in the popup window to a textbox.
    i realized due to security you cant write the value of the file upload field in a client side function and if you do it returns c:/fakepath/realfilename.extention which is what is happening with me. It has to be done with server side code. At the moment I have a JavaScript function doing it Called WriteTextBoxValue. I'm wondering how I can alter my code or logic to make it so that it can write the file upload text to it. Keep in mind the popup needs to know which textbox to write the value in which is why I've passed the reference to the textbox in the ShowControl function.

    I have a simple div popup window which i show with a click of a button using the ShowControl function I have created. The popup has an asp file upload field and an asp textbox. The popup window and button are on the same page.


    This Code below is like what i have on 1 asp.net page:

    Page Code:

    <div class="fieldwrapper">
    <div class="buttonsdiv">
    Product Image:
    <asp:TextBox ID="txtProductImage" Width="400px" runat="server"></asp:TextBox>
    <input type="button" id="uploadProductImage" value="Upload" onclick="ShowControl('<%= txtProductImage.ClientID %>');" />
    </div>
    </div>


    <div class="fieldwrapper">
    <div class="buttonsdiv">
    Product Image:
    <asp:TextBox ID="txtProductImage" Width="400px" runat="server"></asp:TextBox>
    <input type="button" id="uploadProductImage" value="Upload" onclick="ShowControl('<%= txtProductImage.ClientID %>');" />
    </div>
    </div>


    PopUp Window Code:

    <div id="divUploadControl" class="UploadWindow">
    <div class="fieldwrapper" style="width:300px;">
    <div class="buttonsdiv" style="width:300px;">
    <div style="float:left; display:inline; text-align:left; font-size:16pt;">Upload Image</div>
    <div style="float:right; display:inline;"><a href="javascript:void(0);"><img src="close.gif" border="0" height="25px" onclick="CloseUploadControl();"/></a></div>
    <div style="background-color:#ebebeb; padding:10px; width:300px; text-align:left;">
    <br />
    File Name: <asp:FileUpload ID="fileUploadForm" Width="300px" runat="server" />
    <br />
    <br />
    Destination:
    <asp:TreeView ID="treeview1" ExpandDepth="10" runat="server">
    </asp:TreeView>
    <br />
    <asp:Button ID="SubmitButton" Text="Submit" OnClientClick="return WriteTextBoxValue();" OnClick="SubmitButton_Click" runat="server" />
    <br />
    <br />
    </div>
    </div>
    </div>
    </div>


    Script to show popup:

    var selectedUploadField;
    function ShowControl(controlName){
    var upControl = document.getElementById("divUploadControl");
    upControl.style.display = "block";
    selectedUploadField = controlName;
    }



    Script that writes the value of the file upload field to the textbox associated with the button that was clicked:

    function WriteTextBoxValue(){
    var txtControl = document.getElementById(selectedUploadField);
    var fileUploadControl = document.getElementById("<%= fileUploadForm.ClientID %>");
    txtControl.value = fileUploadControl.value;
    document.getElementById("divUploadControl").style.display = "none";
    return false;
    }

  2. #2
    Join Date
    Jan 2011
    Posts
    2

    Re: Problem with ASP.Net and Javascript

    Edit To Page Code:
    Made a mistake by textboxes having the same ids.
    Just note they have different id values and are passed accordingly to ShowControl.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured