asp.net下拉列表选中的值显示在gridview内的文本框中

时间:2016-08-09 20:33:42

标签: jquery asp.net gridview

我在gridview中有三个下拉列表,gridview中有一个文本框以及其他控件。我想从每个下拉列表中选择值并添加它们的值,并根据显示结果显示在gridview内的文本框中。必须为gridview的每一行完成此操作。 我能够从每行的下拉列表中获取所选值,而不能在gridview的每一行的文本框中显示它。 所有这一切都必须使用jquery来完成。请帮忙。过去三天,请帮忙。谢谢 任何简单的方法都将受到高度赞赏

GridView代码:               

            <asp:TemplateField HeaderText ="Frequency">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlFrequency" runat="server" CssClass="col-sm-3 it1"> 
                        <asp:ListItem Value="0">Select</asp:ListItem>
                        <asp:ListItem Value="1">1.The lowest score for activity being done a minimal amount up to an hour a day.</asp:ListItem>
                        <asp:ListItem Value="2">2.The activity is done more than an hour up to a quarter of their time at work or about 2 hours a day</asp:ListItem>
                        <asp:ListItem Value="3">3.The activity is done for at least half of the time in a day.</asp:ListItem>
                        <asp:ListItem Value="4">4.The activity is done more than half of the day</asp:ListItem>
                        <asp:ListItem Value="5">5.The activity is done three quarters of the day</asp:ListItem>
                        <asp:ListItem Value="6">6.The highest score for the person performing the task all day alone</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText ="Probability">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlProbability" runat="server" CssClass="col-sm-4 it2"> 
                        <asp:ListItem Value="0">Select</asp:ListItem>
                        <asp:ListItem Value="1">1.The lowest score for any low probability of injury</asp:ListItem>
                        <asp:ListItem Value="2">2.There is a remote chance of injury</asp:ListItem>
                        <asp:ListItem Value="3">3.There is an occasional chance of injury</asp:ListItem>
                        <asp:ListItem Value="4">4.There is a probable chance of injury</asp:ListItem>
                        <asp:ListItem Value="5">5.There is a likely chance of injury.</asp:ListItem>
                        <asp:ListItem Value="6">6.The highest score for any high probability of injury occurring.</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText ="Severity">
                <ItemTemplate>
                    <asp:DropDownList ID="dllSeverity" runat="server" CssClass="col-sm-3 it3" OnSelectedIndexChanged="dllSeverity_SelectedIndexChanged" AutoPostBack="True">
                        <asp:ListItem Value="0">Select</asp:ListItem>
                        <asp:ListItem Value="1">1.The lowest score for any minor injuries such as minor cuts, scrapes requiring first-aid.</asp:ListItem>
                        <asp:ListItem Value="2">2.Multiple first Aid, with possible health care visit with no lost time.</asp:ListItem>
                        <asp:ListItem Value="3">3.Potential for medical aid cases and/or resulting in few days of lost time injuries.</asp:ListItem>
                        <asp:ListItem Value="4">4.Long term lost time injury</asp:ListItem>
                        <asp:ListItem Value="5">5.The highest score for any major injuries such as fatalities/critical injuries</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hazard Rating">
                <ItemTemplate>
                    <asp:TextBox ID="tbHazRat" runat="server" CssClass="col-sm-1 form-control it4"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>   
        </asp:GridView>

jquery的:

<script>
        $(document).ready(function () {
            var a, b, c;

            $('.it1').change(function () {
                var row = $(this);
                a = $(this).find('option:selected').val();

                var d = $('#<%=gvCHA.ClientID%>').find('input:text[id*="tbHazRat"]');
                d.val(a);   
            });

            $('.it2').change(function () {
                var row = $(this);
                b = row.find('option:selected').val();
                alert(b);
            });

            $('.it3').change(function () {
                c = $(this).find('option:selected').val();
               // alert(a);
            });

            var e = a + b + c;


        });
    </script>

2 个答案:

答案 0 :(得分:0)

您的代码有很多冗余。您应该更有效地使用选择器jquery。这就是你需要做的一切。

[ ]

答案 1 :(得分:0)

解决: 我将整个GridView放在UpdatePanel中并使用Ajax,实现了所需的功能。 每个DropDownList SelectedIndexChanged事件都是异步调用的,还有一个名为Calculate的独立方法,该方法从3个下拉列表中获取参数值,并使用GridView Row作为索引在TextBox上显示结果