1

Hi I want to call javacript code that populates the parent window. In my aspx page I have an sqlDataSource followed by a DataList.

    <asp:DataList runat="server" id="DataList1" DataSourceID="SqlDataSource2">
    <ItemTemplate>
        Id:
        <asp:Label Text='<%# Eval("Id") %>' runat="server" id="IdLabel" />
        <br />
        CustomerName:
        <asp:Label Text='<%# Eval("CustomerName") %>' runat="server" id="CustomerNameLabel" />
        <br />
        CustomerAddress:
        <asp:Label Text='<%# Eval("CustomerAddress") %>' runat="server" id="CustomerAddressLabel" />
        <br />
        CustomerDOB:
        <asp:Label Text='<%# Eval("CustomerDOB") %>' runat="server" id="CustomerDOBLabel" />
        <br />
        <br />

        <input name="BtnSelect" type="button" value="Select" onclick="javascript:PopulateParent("<%# Eval("CustomerAddress") %> ");"/>


    </ItemTemplate>
</asp:DataList>

when the button is pressed i want the values of CustomerName and CustomerAddress to be passed to my Javascript code. I've added the onclick to standard button within ItemTemplate but i recieve errors.

What’s the best way to do this, I dont necessarily need to use a DatList, also tried a GridView but still have a problem incorporating onclick.

onclick works without the server code bit i.e.

1 Answer 1

1

why dont you just call the JS function onclick event, within your JS even use:

aspx

<asp:HiddenField runat="server" ID="woop" />

Javascript

var i = document.getElementById('woop').value;

to get the value from the control ;)

so in your case its:

JS

PopulateParent()
{
    var i = document.getElementById('CustomerAddressLabel').value;
}

obviously the format is wrong ;) but im just trying to give you an example!

for your aspx you call the above Js only without passing the values as you already grab it within the function!

<input name="BtnSelect" type="button" value="Select" onclick="javascript:PopulateParent();"/>
3
  • Dont follow last sentence.. 'as you already grab it within the function!' Because ItemTemplate iterates through several items and several same Id's will be created. JS wont have unique id to use this way, I think. Can the asp arguments be added in the function ? Apr 27, 2013 at 7:58
  • thats right if its being used more than once in the page, but in this case it doesnt look like it hence why ive done it that way ;), if it is than i would change the code accordingly :)
    – Ali Jafer
    Apr 27, 2013 at 8:54
  • Hi In my newform a user types unique customer reference. then clicks a link. The verify box opens with a list of possible customers (should only be one). Then the user selects the correct row, this calls the javascript to populate newform with all details. Is it possible to send variables from the verify form to javascript function as I originally intended. Is there a better way to do this? Apr 29, 2013 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.