【问题标题】:asp.net dropdownlist selected value display in textbox inside gridviewasp.net dropdownlist 选定值显示在gridview内的文本框中
【发布时间】:2016-08-09 20:33:41
【问题描述】:

我在 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>

【问题讨论】:

    标签: jquery asp.net gridview


    【解决方案1】:

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

    $(document).ready(function () {
            $('select[id^=ddl]').change(function () { // target all select element whose ID starts with ddl
                var selectedValue = $(this).find('option:selected').val();
                $("input[id*=tbHazRat]").val(selectedValue);                
            });
        });
    

    【讨论】:

    • 如果gridview 中有超过一行,所有文本框的值都会改变。我想为 gridview 行的文本框设置不同的值。谢谢
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多