单击 asp:listview 中的链接按钮时如何停止页面加载

发布于 2024-12-17 14:52:22 字数 5738 浏览 0 评论 0原文

Windows 7、Visual Web Developer Express 2010、C#、asp.net、webforms。从 vwd 中使用 ctrl-F5 运行程序。

使用每个选项卡上带有 asp:listview 的选项卡。我在每个列表视图中的项目后面都有代码。 后面的代码使用 ID(来自项目)通过 xsd 从数据库(mdf 文件)中提取记录。填充页面上的文本框。

当我单击某个项目时,程序会正确抓取正确的记录,并将该记录中的信息正确地放入屏幕上的文本框中。问题是它正在刷新或重新加载页面......这导致它转到第一个选项卡而不是我所在的选项卡。我认为这会让用户感到不安和意外(这对我来说是令人不安和意外的)。

以下是我如何处理列表视图中项目单击的隐藏代码:

    protected void lv_PBP_click(object sender, ListViewCommandEventArgs e)
    {
        if (!e.CommandName.Equals("Sort"))
        {
            int searchID = Convert.ToInt32(e.CommandArgument.ToString());
            TableAdapters.PBPTableAdapter pbpAdapt =
                new TableAdapters.PBPTableAdapter();
            PBPDataTable tbl = pbpAdapt.GetData(searchID);
            tbMessage.Text = tbl.Rows[0]["pMessage"].ToString();
            lbField2.Text = tbl.Rows[0]["pField2"].ToString();
            lbField3.Text = tbl.Rows[0]["pField3"].ToString();
        }
    }

这是列表视图的定义方式:

    <div class="tab-content" style="width:1000px;">
           <h1 class="tab" title="title1">title1</h1>
           <asp:ListView runat= "server"   ID="somid" DataSourceID="ads" OnItemCommand="lv_PBP_click">
              <LayoutTemplate>
                <table  id="table1"  style="background-color:White;border-collapse:collapse;" width="100%">      
                <tr>                  
                    <td width="75%">    <asp:Button runat="server" ID="SortButton" 
                     Text="msg" CommandName="Sort" CommandArgument="pMessage" /></td>
                     <td>    <asp:Button runat="server" ID="LinkButton1" 
                     Text="F2 CommandName="Sort" CommandArgument="pField2" /> </td>
                    <td>    <asp:Button runat="server" ID="SortF3" 
                     Text="F3" CommandName="Sort" CommandArgument="pField3" /></td>
                  </tr>
                  <tr runat="server" id="itemPlaceholder" style="background-color:White" >
                  </tr> 
                </table>
                <asp:DataPager runat="server" ID="idxxx" PageSize="10" style="background-color:White">
                    <Fields> 
                      <asp:TemplatePagerField>              
                        <PagerTemplate>
                        <b>
                        Page
                        <asp:Label runat="server" ID="CurrentPageLabel" 
                          Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
                        of
                        <asp:Label runat="server" ID="TotalPagesLabel" 
                          Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
                        (
                        <asp:Label runat="server" ID="TotalItemsLabel" 
                          Text="<%# Container.TotalRowCount%>" />
                        records)
                        <br />
                        </b>
                        </PagerTemplate>
                      </asp:TemplatePagerField>

                      <asp:NextPreviousPagerField
                        ButtonType="Button"
                        ShowFirstPageButton="true"
                        ShowNextPageButton="false"
                        ShowPreviousPageButton="false" />

                      <asp:NumericPagerField 
                        PreviousPageText="&lt; Prev 10"
                        NextPageText="Next 10 &gt;"
                        ButtonCount="10" />

                      <asp:NextPreviousPagerField
                        ButtonType="Button"
                        ShowLastPageButton="true"
                        ShowNextPageButton="false"
                        ShowPreviousPageButton="false" />

                    </Fields>
                </asp:DataPager>
              </LayoutTemplate>
                <ItemTemplate>
                <tr id="Tr1" runat="server" style="background-color:#ECE5B6;" >
                    <td><asp:LinkButton runat="server" ID="ms1"   Text='<%# Eval("pMessage")%>'  CommandArgument='<%# Eval("pID") %>' /></td>
                    <td><asp:Label runat="server" ID="Label1"  Text='<%# Eval("pField2")%>'  /></td>
                    <td><asp:Label runat="server" ID="Status"  Text='<%# Eval("pSField3")%>' /></td>
                </tr>
                </ItemTemplate>
                <AlternatingItemTemplate>
                <tr id="Tr1" runat="server" style="background-color:#FAF8CC;" >
                    <td><asp:LinkButton runat="server" ID="ms2"   Text='<%# Eval("pMessage")%>'  CommandArgument='<%# Eval("pID") %>' /></td>
                    <td><asp:Label runat="server" ID="Label1"  Text='<%# Eval("pField2")%>'  /></td>
                    <td><asp:Label runat="server" ID="yyy"  Text='<%# Eval("pField3")%>' /></td>
                </tr>

                </AlternatingItemTemplate>

            </asp:ListView>
   </div>

代码底部附近的 LinkBut​​tons 对应于列表视图中的项目,这些项目调用上面第一个片段中的隐藏代码。我想我错过了一些关于按钮功能的基本知识。也许按钮(或链接按钮)总是进行页面刷新(似乎不太可能)。不管怎样,我想做的是停止页面重新加载......怎么做?

OTOH,也许有某种原因我不应该停止此页面重新加载......也许设计者想要这种行为。既然如此,我该怎么办呢?我是被迫使用ajax还是有其他方法可以做到这一点? (只需更改某些消息框的内容,无需刷新页面...或刷新到与原来完全相同的状态,这样看起来就不像被刷新了。)

Windows 7, visual web developer express 2010, c#, asp.net, webforms. Running program with ctrl-F5 from vwd.

Using tabs with an asp:listview on each tab. I have code-behind the items in each listview.
The code behind uses an ID (from the item) to pull a record from a database (mdf file) via xsd. to populate textboxes on the page.

When I click on an item, the program properly grabs the correct record and properly puts the information from that record into the textboxes on the screen. The problem is that it's either refreshing or reloading the page ... which causes it to go to the first tab instead of the tab I'm on. I assume this would be disconcerting and unexpected to the user (it's disconcerting and unexpected to me).

Here's how I handle the code behind for an item click in the listview:

    protected void lv_PBP_click(object sender, ListViewCommandEventArgs e)
    {
        if (!e.CommandName.Equals("Sort"))
        {
            int searchID = Convert.ToInt32(e.CommandArgument.ToString());
            TableAdapters.PBPTableAdapter pbpAdapt =
                new TableAdapters.PBPTableAdapter();
            PBPDataTable tbl = pbpAdapt.GetData(searchID);
            tbMessage.Text = tbl.Rows[0]["pMessage"].ToString();
            lbField2.Text = tbl.Rows[0]["pField2"].ToString();
            lbField3.Text = tbl.Rows[0]["pField3"].ToString();
        }
    }

Here's how the the listview is defined:

    <div class="tab-content" style="width:1000px;">
           <h1 class="tab" title="title1">title1</h1>
           <asp:ListView runat= "server"   ID="somid" DataSourceID="ads" OnItemCommand="lv_PBP_click">
              <LayoutTemplate>
                <table  id="table1"  style="background-color:White;border-collapse:collapse;" width="100%">      
                <tr>                  
                    <td width="75%">    <asp:Button runat="server" ID="SortButton" 
                     Text="msg" CommandName="Sort" CommandArgument="pMessage" /></td>
                     <td>    <asp:Button runat="server" ID="LinkButton1" 
                     Text="F2 CommandName="Sort" CommandArgument="pField2" /> </td>
                    <td>    <asp:Button runat="server" ID="SortF3" 
                     Text="F3" CommandName="Sort" CommandArgument="pField3" /></td>
                  </tr>
                  <tr runat="server" id="itemPlaceholder" style="background-color:White" >
                  </tr> 
                </table>
                <asp:DataPager runat="server" ID="idxxx" PageSize="10" style="background-color:White">
                    <Fields> 
                      <asp:TemplatePagerField>              
                        <PagerTemplate>
                        <b>
                        Page
                        <asp:Label runat="server" ID="CurrentPageLabel" 
                          Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
                        of
                        <asp:Label runat="server" ID="TotalPagesLabel" 
                          Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
                        (
                        <asp:Label runat="server" ID="TotalItemsLabel" 
                          Text="<%# Container.TotalRowCount%>" />
                        records)
                        <br />
                        </b>
                        </PagerTemplate>
                      </asp:TemplatePagerField>

                      <asp:NextPreviousPagerField
                        ButtonType="Button"
                        ShowFirstPageButton="true"
                        ShowNextPageButton="false"
                        ShowPreviousPageButton="false" />

                      <asp:NumericPagerField 
                        PreviousPageText="< Prev 10"
                        NextPageText="Next 10 >"
                        ButtonCount="10" />

                      <asp:NextPreviousPagerField
                        ButtonType="Button"
                        ShowLastPageButton="true"
                        ShowNextPageButton="false"
                        ShowPreviousPageButton="false" />

                    </Fields>
                </asp:DataPager>
              </LayoutTemplate>
                <ItemTemplate>
                <tr id="Tr1" runat="server" style="background-color:#ECE5B6;" >
                    <td><asp:LinkButton runat="server" ID="ms1"   Text='<%# Eval("pMessage")%>'  CommandArgument='<%# Eval("pID") %>' /></td>
                    <td><asp:Label runat="server" ID="Label1"  Text='<%# Eval("pField2")%>'  /></td>
                    <td><asp:Label runat="server" ID="Status"  Text='<%# Eval("pSField3")%>' /></td>
                </tr>
                </ItemTemplate>
                <AlternatingItemTemplate>
                <tr id="Tr1" runat="server" style="background-color:#FAF8CC;" >
                    <td><asp:LinkButton runat="server" ID="ms2"   Text='<%# Eval("pMessage")%>'  CommandArgument='<%# Eval("pID") %>' /></td>
                    <td><asp:Label runat="server" ID="Label1"  Text='<%# Eval("pField2")%>'  /></td>
                    <td><asp:Label runat="server" ID="yyy"  Text='<%# Eval("pField3")%>' /></td>
                </tr>

                </AlternatingItemTemplate>

            </asp:ListView>
   </div>

The LinkButtons near the bottom of the code corresponds to the items in the listview which invoke the code behind in the first snippet above. I think I'm missing something fundamental about what buttons do. Maybe buttons (or linkbuttons) always do a page refresh (seems unlikely). Anyway, what I want to do is stop the page reload ... how?

OTOH, maybe there's some reason I should not stop this page reload ... maybe the designers want this behavior. In that case, what should I do? Am I forced into ajax or is there another way to do this? (Just change the contents of some message boxes without the page refreshing... or refreshing into the exact same state it was left in so that it doesn't look like it was refreshed.)

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

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

发布评论

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

评论(1

尐籹人 2024-12-24 14:52:22

你的怀疑是正确的。
与服务器的所有交互都涉及回发,这会重新加载页面。

如果希望页面与服务器交互而不需要回发,则需要使用 AJAX。

Your suspicion is correct.
All interactions with the server involve a postback, which reloads the page.

If you want the page to interact with the server without a postback, you need to use AJAX.

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