使用 UpdatePanel 动态刷新 HTML 表中的行

发布于 2024-07-11 05:50:33 字数 611 浏览 6 评论 0原文

我最近经常在一些我一直在开发的 Intranet 应用程序中使用 ASP.NET AJAX UpdatePanel 控件,并且在大多数情况下,我一直在使用它来动态刷新数据或隐藏和显示基于表单的控件用户的操作。

有一个地方我遇到了一些麻烦,我想知道是否有人有任何建议。 我的表单使用非常典型的基于表格的布局,其中表格用于显示标签和字段的网格以供用户填写。 (我已经知道有些人回避基于表格的布局,并且我了解其优点和缺点。但这就是我所做的选择,所以请不要回答“不要使用基于表格的布局”。)

现在我的问题是,有时我将 UpdatePanel 包裹在一组行周围,以便我可以动态隐藏和显示它们,但 UpdatePanel 并不真正允许您这样做。 基本问题是它在它们周围包裹了一个 div,据我所知,你不能将 div 包裹在表行周围。 它不是有效的 HTML。

因此,我处理它的方法是使我的动态行完全成为整个新表的一部分,这是可以的,但是随后您必须手动将所有列与其上面的表对齐,这是一个真正的问题疼痛而且相当脆弱。

所以,问题是...有人知道使用 UpdatePanel 或类似的东西动态生成或刷新行的技术吗? 希望解决方案几乎与在页面上放置 UpdatePanel 一样简单,但即使不是,我仍然想听听它。

I have been using the ASP.NET AJAX UpdatePanel control a lot lately for some intranet apps I've been working on, and for the most part, I have been using it to dynamically refresh data or hide and show controls on a form based on the user's actions.

There is one place where I have been having a bit of trouble, and I'm wondering if anyone has any advice. My form is using a pretty typical table based layout where the table is used to display a grid of labels and fields for the user to fill out. (I already know table based layouts are eschewed by some people, and I understand the pros and cons. But that's the choice I've made, so please don't answer with "Don't use a table based layout".)

Now my problem is that there are times when I would like to wrap an UpdatePanel around a set of rows so that I can hide and show them dynamically, but the UpdatePanel doesn't really let you do that. The basic problem is that it wraps a div around them, and as far as I know, you cannot wrap a div around table rows. It is not valid HTML.

So the way I have been dealing with it is to make my dynamic rows part of a whole new table entirely, which is OK, but then you have to deal with aligning all the columns manually with the table above it, and that is a real pain and pretty fragile.

So, the question is... does anyone know of any technique for dynamically generating or refreshing rows using either an UpdatePanel or something similar? Hopefully, the solution would be almost as easy as dropping an UpdatePanel on the page, but even if not, I'd still like to hear it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

溺深海 2024-07-18 05:50:33

UpdatePanel 呈现为 DIV 标记,因此在表行之间无效。 如果您只想隐藏内容,同时保持(非常非常糟糕)表布局,请在行中包含一个样式标记,并在其中包含可见性值的 ASP 变量,如下所示:

<tr style="display: <%= visible %>">
     <td></td>
</tr>

然后根据需要操作可见变量。

也就是说,忽视正确的布局会伤害你。

An UpdatePanel renders as a DIV tag and is therefore invalid between table rows. If all you want is to hide the content while maintaining your (very very bad) table layout, include a style tag in the row with an ASP var in there for the visibility value like this:

<tr style="display: <%= visible %>">
     <td></td>
</tr>

Then you manipulate the visible variable as needed.

That said, brushing aside proper layout is hurting you here.

听你说爱我 2024-07-18 05:50:33

UpdatePanels(或一般的 AJAX 回发)不应仅用于隐藏或显示元素。 如果您需要更新数据,这是一回事......但除此之外,只需使用 javascript 更改显示 css 属性即可。

如果您使用像 jQuery 这样的客户端框架,那就更容易了 - 您可以这样做:

<button onclick="$('.inactive').toggle();">Toogle Inactive</button>

<table>
<tr class="inactive"><td>Inactive 1</td></tr>
<tr class="inactive"><td>Inactive 2</td></tr>
<tr><td>Active 1</td></tr>
<tr><td>Active 2</td></tr>
</table>

UpdatePanels (or AJAX postbacks in general) should not be used to just hide or show elements. If you need to update data, that's one thing... but otherwise, just use javascript to change the display css property.

If you use a client framework like jQuery, that makes it even easier - you could do something like this:

<button onclick="$('.inactive').toggle();">Toogle Inactive</button>

<table>
<tr class="inactive"><td>Inactive 1</td></tr>
<tr class="inactive"><td>Inactive 2</td></tr>
<tr><td>Active 1</td></tr>
<tr><td>Active 2</td></tr>
</table>
内心激荡 2024-07-18 05:50:33

如果您动态创建控件,则可以在生成控件时决定显示或隐藏哪些内容。

您还可以执行以下操作:

<table>
    <tr id="row1" runat="server">
        <td>Label</td><td>Field</td>
    </tr>
</table>

从代码后面:对

row1.visible = false;

@Rob Allen 的答案进行修改,执行以下操作:

CSS

.hidden_row {
    display: none;
}

ASPX

<tr class="<%= variable %>">

相同的想法,只是使用类而不是将 CSS 直接编码到表中。

If you are dynamically creating your controls, you can decide which things to display or hide while generating your controls.

You can also do things like this:

<table>
    <tr id="row1" runat="server">
        <td>Label</td><td>Field</td>
    </tr>
</table>

And from code behind:

row1.visible = false;

A modification of @Rob Allen's answer, do this:

CSS

.hidden_row {
    display: none;
}

ASPX

<tr class="<%= variable %>">

Same idea, just using a class instead of coding the CSS directly into the table.

暗喜 2024-07-18 05:50:33

答案:最终,没有办法使用UpdatePanel 来做到这一点。 您能实现的最好结果是刷新整个表,而不是个别行。

Answer: In the end, there is no way to do this using an UpdatePanel. The best you can achieve is refreshing the entire table, but not individual rows.

水溶 2024-07-18 05:50:33

我遇到了同样的问题,并且偶然发现了一个解决方案。

如果整个表本身也位于 Updatepanel 内,则可以更新表的 TD 部分!

不知道为什么。 这个对我有用。

<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate> <table ID="Table1">
<tr>
<td >
...
</td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<td >
....
</td>
<td >
...
</td>
<td >
...
</td>
</ContentTemplate>
</asp:UpdatePanel> 

</tr>

 </table></ContentTemplate></asp:UpdatePanel>  

I had the same problem and I stubled upon a solution.

You CAN update TD parts of a table if the whole table itself is inside an Updatepanel as well!!

Dont' know why. It works for me.

<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate> <table ID="Table1">
<tr>
<td >
...
</td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<td >
....
</td>
<td >
...
</td>
<td >
...
</td>
</ContentTemplate>
</asp:UpdatePanel> 

</tr>

 </table></ContentTemplate></asp:UpdatePanel>  
∝单色的世界 2024-07-18 05:50:33

Asp代码:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table width="100%">
    <tr>
        <td style="width: 300px">
            Employee First Name
        </td>
        <td>
            <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
        </td>
    </tr>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <tbody>
                <tr>
                    <td style="width: 300px">
                        Date Of Birth
                    </td>
                    <td>
                        <asp:TextBox ID="txtDOB1" runat="server" OnTextChanged="txtDOB_TextChanged" AutoPostBack="true"></asp:TextBox>
                        <asp:CalendarExtender ID="txtDOB1_CalendarExtender" runat="server" Enabled="True"
                            TargetControlID="txtDOB1">
                        </asp:CalendarExtender>
                    </td>
                </tr>
                <tr>
                    <td style="width: 300px">
                        Age
                    </td>
                    <td>
                        <asp:TextBox ID="txtAge" Style="font-weight: bold; font-size: large" runat="server"
                            Enabled="false"></asp:TextBox>
                    </td>
                </tr>
            </tbody>
        </ContentTemplate>
    </asp:UpdatePanel>
    <tr>
        <td align="right" style="width: 300px">
            <asp:Button ID="btnSubmit" runat="server" CssClass="button" Text="Submit" />
        </td>
        <td>
            <asp:Button ID="btnClear" runat="server" CssClass="button" Text="Reset" />
        </td>
    </tr>
</table>

类代码:

protected void txtDOB_TextChanged(object sender, EventArgs e)
{
    try
    {
        DateTime Today = DateTime.Now;
        DateTime DOB = Convert.ToDateTime(txtDOB1.Text);
        ArrayList arr = new ArrayList();
        arr = span(Today, DOB);
        arr[0].ToString();//For Years
        arr[1].ToString();//For Months
        arr[2].ToString();//For Days
        txtAge.Text = "Y: " + arr[0].ToString()+",M: "+arr[1].ToString()+",D: "+arr[2].ToString();

    }
    catch (Exception ex)
    {

        lblError.Text = "Error : " + ex.Message ;
    }
}
public ArrayList span(DateTime f, DateTime l)
{
    int days;
    int months;
    int years;

    int fird = f.Day;
    int lasd = l.Day;

    int firm = f.Month;
    int lasm = l.Month;

    if (fird >= lasd)
    {
        days = fird - lasd;
        if (firm >= lasm)
        {
            months = firm - lasm;
            years = f.Year - l.Year;
        }
        else
        {
            months = (firm + 12) - lasm;
            years = f.AddYears(-1).Year - l.Year;
        }
    }
    else
    {
        days = (fird + 30) - lasd;
        if ((firm - 1) >= lasm)
        {
            months = (firm - 1) - lasm;
            years = f.Year - l.Year;
        }
        else
        {
            months = (firm - 1 + 12) - lasm;
            years = f.AddYears(-1).Year - l.Year;
        }
    }


    if (days < 0)
    {
        days = 0 - days;
    }
    if (months < 0)
    {
        months = 0 - months;
    }
    ArrayList ar = new ArrayList();
    ar.Add(years.ToString());
    ar.Add(months.ToString());
    ar.Add(days.ToString());
    return ar;
}

Asp code:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table width="100%">
    <tr>
        <td style="width: 300px">
            Employee First Name
        </td>
        <td>
            <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
        </td>
    </tr>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <tbody>
                <tr>
                    <td style="width: 300px">
                        Date Of Birth
                    </td>
                    <td>
                        <asp:TextBox ID="txtDOB1" runat="server" OnTextChanged="txtDOB_TextChanged" AutoPostBack="true"></asp:TextBox>
                        <asp:CalendarExtender ID="txtDOB1_CalendarExtender" runat="server" Enabled="True"
                            TargetControlID="txtDOB1">
                        </asp:CalendarExtender>
                    </td>
                </tr>
                <tr>
                    <td style="width: 300px">
                        Age
                    </td>
                    <td>
                        <asp:TextBox ID="txtAge" Style="font-weight: bold; font-size: large" runat="server"
                            Enabled="false"></asp:TextBox>
                    </td>
                </tr>
            </tbody>
        </ContentTemplate>
    </asp:UpdatePanel>
    <tr>
        <td align="right" style="width: 300px">
            <asp:Button ID="btnSubmit" runat="server" CssClass="button" Text="Submit" />
        </td>
        <td>
            <asp:Button ID="btnClear" runat="server" CssClass="button" Text="Reset" />
        </td>
    </tr>
</table>

Class code:

protected void txtDOB_TextChanged(object sender, EventArgs e)
{
    try
    {
        DateTime Today = DateTime.Now;
        DateTime DOB = Convert.ToDateTime(txtDOB1.Text);
        ArrayList arr = new ArrayList();
        arr = span(Today, DOB);
        arr[0].ToString();//For Years
        arr[1].ToString();//For Months
        arr[2].ToString();//For Days
        txtAge.Text = "Y: " + arr[0].ToString()+",M: "+arr[1].ToString()+",D: "+arr[2].ToString();

    }
    catch (Exception ex)
    {

        lblError.Text = "Error : " + ex.Message ;
    }
}
public ArrayList span(DateTime f, DateTime l)
{
    int days;
    int months;
    int years;

    int fird = f.Day;
    int lasd = l.Day;

    int firm = f.Month;
    int lasm = l.Month;

    if (fird >= lasd)
    {
        days = fird - lasd;
        if (firm >= lasm)
        {
            months = firm - lasm;
            years = f.Year - l.Year;
        }
        else
        {
            months = (firm + 12) - lasm;
            years = f.AddYears(-1).Year - l.Year;
        }
    }
    else
    {
        days = (fird + 30) - lasd;
        if ((firm - 1) >= lasm)
        {
            months = (firm - 1) - lasm;
            years = f.Year - l.Year;
        }
        else
        {
            months = (firm - 1 + 12) - lasm;
            years = f.AddYears(-1).Year - l.Year;
        }
    }


    if (days < 0)
    {
        days = 0 - days;
    }
    if (months < 0)
    {
        months = 0 - months;
    }
    ArrayList ar = new ArrayList();
    ar.Add(years.ToString());
    ar.Add(months.ToString());
    ar.Add(days.ToString());
    return ar;
}
拥抱我好吗 2024-07-18 05:50:33

在我们的项目中,我们找到了这个问题的一些解决方案。 我们必须创建复杂的报告,每行中有许多活动元素(用于编辑、接受等的按钮)。

我们创建了放置在 updatepanel 中的 div(用于在需要时更新整个表)。 在此 div 中,我们创建了带有标题定义的表和每行的单独表(这些表放置在更新面板中)。 为了在每行中创建相等的列,我们必须为每个表格单元格使用 css 类。

所以,我们有这样的东西(在每一行中我们有几个按钮、下拉菜单、工具提示等,但为了理解我的想法,我将其截断为单列):

<UpdatePanel>
<DIV>
    <TABLE>
    <TR>
    <TH class="h1">Name</TH>
    </TR>
    </TABLE>

    <UpdatePanel Id='InnerPanel1'>
        <TABLE>
        <TR>
        <TD class="h1">John</TD>
        </TR>
        </TABLE>
    </UpdatePanel>
</DIV>
</UpdatePanel>

它并不优雅,会生成很大的 HTML,但可以工作。

In our project we found some solution of this problem. We had to create complex raports with many active elements in each row (buttons for edit, accept etc.).

We created div that is placed in updatepanel (for updating whole table when needed). In this div we created table with header definition and separate tables for eaach rows (those tables are put in update panels). For creating equal columns in each rows we have to use css classes for each table cell.

So, we have something like this (in each row we have few buttons, dropdowns, tooltips, etc. but to understand idea I've truncated it to single column):

<UpdatePanel>
<DIV>
    <TABLE>
    <TR>
    <TH class="h1">Name</TH>
    </TR>
    </TABLE>

    <UpdatePanel Id='InnerPanel1'>
        <TABLE>
        <TR>
        <TD class="h1">John</TD>
        </TR>
        </TABLE>
    </UpdatePanel>
</DIV>
</UpdatePanel>

It is not elegant and generates large HTML, but works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文