码东东666 2017-04-17 02:25 采纳率: 0%
浏览 720

UpdatePanel 在Listview嵌套Listview中失效

Listview嵌套Listview

UpdatePanel 实现局部刷新

第二个 Listview中有一个RadioButtonList,选择NG时,后台实现备注文本框必填。这个动作需要局部刷新,而不是刷新整个页面,目前这个方法是整体刷新。
HTML代码如下:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Laser_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>TEST</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>
            <table class="TbTemplate">
                <tr>
                    <td colspan="8">
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:ListView ID="ListView1" runat="server"
                                    OnItemDataBound="ListView1_ItemDataBound">
                                    <LayoutTemplate>
                                        <table style="width: 100%; border-collapse: collapse">
                                            <tr id="Tr1" class="LV1TbTh" runat="server" style="background-color: #3a90c9; color: #ffffff">
                                                <th class="LV1td1">检查项目</th>
                                                <th class="LV1td2">检查内容</th>
                                                <th class="LV1td3">检查结果</th>
                                                <th class="LV1td4">备注</th>
                                            </tr>
                                            <tr id="itemPlaceholder" runat="server">
                                            </tr>
                                        </table>
                                    </LayoutTemplate>
                                    <ItemTemplate>
                                        <tr class="LV1Item">
                                            <td class="LV1td1"><%# Eval("Item_Name") %></td>
                                            <td colspan="3">
                                                <asp:ListView ID="ListView2" runat="server" OnItemDataBound="ListView2_ItemDataBound">
                                                    <LayoutTemplate>
                                                        <table class="LV2itemPlaceholderContainer" style="width: 100%; border-collapse: collapse">
                                                            <tr>
                                                                <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                                                            </tr>
                                                        </table>
                                                    </LayoutTemplate>
                                                    <ItemTemplate>
                                                        <tr class="LV2TbItem">
                                                            <td class="LV1td2"><%# Eval("Content_Name") %></td>
                                                            <td class="LV1td3">
                                                                <div style="width: 80%; float: left">
                                                                    <asp:RadioButtonList ID="RadioButtonList_LV2" runat="server" RepeatDirection="Horizontal" Width="100%"
                                                                        OnTextChanged="RadioButtonList1_TextChanged" AutoPostBack="true">
                                                                        <asp:ListItem>OK</asp:ListItem>
                                                                        <asp:ListItem>NG</asp:ListItem>
                                                                        <asp:ListItem>NA</asp:ListItem>
                                                                    </asp:RadioButtonList>
                                                                </div>
                                                                <div style="width: 20%; float: left">
                                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="检查结果必选!"
                                                                        ControlToValidate="RadioButtonList_LV2" ValidationGroup="Laser_New_Submit" ForeColor="Red" Text="*"></asp:RequiredFieldValidator>
                                                                </div>
                                                            </td>
                                                            <td class="LV1td4">
                                                                <asp:TextBox ID="TextBox_LV2" runat="server" CssClass="LV1asptxt" TextMode="MultiLine" Rows="2" Height="48px"></asp:TextBox>
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="选择NG后必选!"
                                                                    ControlToValidate="TextBox_LV2" ValidationGroup="Laser_New_Submit" ForeColor="Red" Text="*" Enabled="false"></asp:RequiredFieldValidator>
                                                            </td>
                                                        </tr>
                                                    </ItemTemplate>
                                                </asp:ListView>
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                </asp:ListView>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </td>
                </tr>
            </table>
            <div style="margin-bottom: 10px; width: 99%">
                <asp:Button ID="Button1" runat="server" Text="提交" CssClass="btnsubmit" ValidationGroup="Laser_New_Submit" OnClick="Button1_Click" />
                <asp:ValidationSummary ID="ValidationSummary1" HeaderText="错误列表:" runat="server" EnableClientScript="true"
                    ShowMessageBox="true" ShowSummary="false" DisplayMode="BulletList" ValidationGroup="Laser_New_Submit" />
            </div>
        </div>
    </form>
</body>
</html>

后台代码部分:

 protected void RadioButtonList1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            RadioButtonList rbl = sender as RadioButtonList;
            if (rbl != null)
            {
                ListViewDataItem LV1dataitem = rbl.Parent as ListViewDataItem;
                if (rbl.SelectedValue == "NG")
                {

                    ((RequiredFieldValidator)LV1dataitem.FindControl("RequiredFieldValidator2")).Enabled = true;
                }
                else
                {
                    ((RequiredFieldValidator)LV1dataitem.FindControl("RequiredFieldValidator2")).Enabled = false;
                }
            }
        }
        catch (Exception ex)
        {
            Literal1.Text = ex.ToString();
        }
    }

请各位大神帮忙看看:
1.为什么不能实现局部刷新,实际状况为整体页面刷新(测试过如果listview不嵌套,只有一个listview,最外面放一个UpdatePanel是起作用的,嵌套之后就不行了。)
2.还有什么方式实现,listview2中选择NG,备注项目必填!
图片说明

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-05 17:55
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要解决这个问题,您可以尝试以下步骤:

    1. 使用UpdatePanelJavaScript结合使用,以便在RadioButtonList选择“NG”时触发局部刷新。
    2. RadioButtonListOnTextChanged事件处理程序中,添加一个条件判断:只在RadioButtonList的选择值为“NG”时才调用TextBox_LV2控件的RequiredFieldValidator控件,并设置其有效性状态。

    以下是修改后的HTML和ASP.NET代码示例:

    HTML:

    <div>
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        <table class="TbTemplate">
            <tr>
                <td colspan="8">
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:ListView ID="ListView1" runat="server"
                                OnItemDataBound="ListView1_ItemDataBound">
                                <LayoutTemplate>
                                    <table style="width: 100%; border-collapse: collapse">
                                        <tr id="Tr1" class="LV1TbTh" runat="server" style="background-color: #3a90c9; color: #ffffff">
                                            <th class="LV1td1">检查项目</th>
                                            <th class="LV1td2">检查内容</th>
                                            <th class="LV1td3">检查结果</th>
                                            <th class="LV1td4">备注</th>
                                        </tr>
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </LayoutTemplate>
                                <ItemTemplate>
                                    <tr class="LV1Item">
                                        <td class="LV1td1"><%# Eval("Item_Name") %></td>
                                        <td colspan="3">
                                            <asp:ListView ID="ListView2" runat="server" OnItemDataBound="ListView2_ItemDataBound">
                                                <LayoutTemplate>
                                                    <table class="LV2itemPlaceholderContainer" style="width: 100%; border-collapse: collapse">
                                                        <tr>
                                                            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                                                        </tr>
                                                    </table>
                                                </LayoutTemplate>
                                                <ItemTemplate>
                                                    <tr class="LV2TbItem">
                                                        <td class="LV1td2"><%# Eval("Content_Name") %></td>
                                                        <td class="LV1td3">
                                                            <div style="width: 80%; float: left">
                                                                <asp:RadioButtonList ID="RadioButtonList_LV2" runat="server" RepeatDirection="Horizontal" Width="100%"
                                                                    OnTextChanged="RadioButtonList1_TextChanged" AutoPostBack="true">
                                                                    <asp:ListItem>OK</asp:ListItem>
                                                                    <asp:ListItem>NG</asp:ListItem>
                                                                    <asp:ListItem>NA</asp:ListItem>
                                                                </asp:RadioButtonList>
                                                            </div>
                                                            <div style="width: 20%; float: left">
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="检查结果必选!"
                                                                    ControlToValidate="RadioButtonList_LV2" ValidationGroup="Laser_New_Submit" ForeColor="Red" Text="*"></asp:RequiredFieldValidator>
                                                            </div>
                                                        </td>
                                                        <td class="LV1td4">
                                                            <asp:TextBox ID="TextBox_LV2" runat="server" CssClass="LV1asptxt" TextMode="MultiLine" Rows="2" Height="48px"></asp:TextBox>
                                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="选择NG后必选!"
                                                                ControlToValidate="TextBox_LV2" ValidationGroup="Laser_New_Submit" ForeColor="Red" Text="*" Enabled="false"></asp:RequiredFieldValidator>
                                                        </td>
                                                    </tr>
                                                </ItemTemplate>
                                            </asp:ListView>
                                        </td>
                                    </tr>
                                </ItemTemplate>
                            </asp:ListView>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </table>
        <div style="margin-bottom: 10px; width: 99%">
            <asp:Button ID="Button1" runat="server" Text="提交" CssClass="btnsubmit" ValidationGroup="Laser_New_Submit" OnClick="Button1_Click" />
            <asp:ValidationSummary ID="ValidationSummary1" HeaderText="错误列表:" runat="server" EnableClientScript="true"
                ShowMessageBox="true" ShowSummary="false" DisplayMode="BulletList" ValidationGroup="Laser_New_Submit" />
        </div>
    </div>
    

    ASP.NET代码:

    protected void RadioButtonList1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            RadioButtonList rbl = sender as RadioButtonList;
            if (rbl != null && rbl.SelectedValue == "NG")
            {
                TextBox_LV2.Enabled = true;
                RequiredFieldValidator1.Enabled = true;
            }
            else
            {
                TextBox_LV2.Enabled = false;
                RequiredFieldValidator1.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            Literal1.Text = ex.ToString();
        }
    }
    

    这样,当RadioButtonList中的选项变为“NG”时,TextBox_LV2控件将被禁用,同时RequiredFieldValidator1也将禁用。这将阻止用户输入任何内容到这个字段。

    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格