1

I have a GridView with a few labels and two DropDownList controls. At the end of each row, I have a "Print" button (normal form button, not asp button). The onclick event produces javascript and passes in values from the grid.

How do I get the Dropdownlist values?

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <input type="button" 
            style="font-size: 18px" 
            onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val() + '&lblNDC=<%# Eval("CODE1") %>    ' + '&unit=<%# Eval("ddUnit.Text") %>    ');" 
            value="Print" />
    </ItemTemplate>
</asp:TemplateField>

It doesn't like trying to Eval ddUnit.Text, but that's the value I'm trying to pass as a URL variable.

1
  • you are using the library jquery right? Commented Nov 7, 2014 at 6:53

2 Answers 2

2

This is hardly the most elegant solution, but you could continue along the jquery path to get the DropDownList value:

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val() + '&lblNDC=<%# Eval("CODE1") %>' + '&unit=' + $('#<%# ((GridViewRow)Container).FindControl("ddUnit").ClientID %>').text());" value="Print" />
    </ItemTemplate>
</asp:TemplateField>
Sign up to request clarification or add additional context in comments.

3 Comments

This almost worked. It doesn't take the selected value, it passes in all possible values for the dropdown. I tried .text and .value, but neither worked.
$('#<%= ddUnit.ClientID %> option:selected').val(); You put me on the right track. Many thanks.
Gah! Of course option:selected...my mistake. Glad it got you going though.
0

Try this.

Markup

<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList CssClass="ddlUnit" runat="server">
            <asp:ListItem Text="text1" Value="1" />
            <asp:ListItem Text="text2" Value="2" />
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <input type="button"
            style="font-size: 18px"
            onclick='<%# "MyPrintWrapper(this, \""+ Eval("CODE1") + "\")" %>'
            value="Print" />
    </ItemTemplate>
</asp:TemplateField>

JavaScript

function MyPrintWrapper(elm, code) {
    var paramString = 'useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked')
        + '&printerName=' + $('#installedPrinterName').val()
        + '&lblNDC=' + code
        //take the clicked button, find its row, 
        //then find the dropdownlist using its class name
        + '&unit=' + $(elm).closest("tr").find(".ddlUnit").val();
    //console.log(paramString);
    jsWebClientPrint.print(paramString);
}

Here we created a class name for the dropdownlist and a wrapper js function so that markup looks cleaner.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.