从中继器控件更新面板和触发器

发布于 2024-11-02 07:34:05 字数 1323 浏览 0 评论 0原文

您好,我在网上发现了类似于以下的代码。将按钮埋在中继器控件中以触发返回服务器的完整循环似乎真的很棒。

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
        </asp:ScriptManager> 

        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
            <ContentTemplate> 
                <%=DateTime.Now.ToString() %> 
            </ContentTemplate> 
            <Triggers> 
                <asp:PostBackTrigger ControlID="HiddenButton" /> 
            </Triggers> 
        </asp:UpdatePanel> 

        <!--Make a hidden button to treat as the postback trigger--> 
        <asp:Button ID="HiddenButton" runat="server" Style="display: none" Text="HiddenButton" /> 


        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> 
            <ItemTemplate> 
                 <!--when cick the button1, it will fire the hiddenButton--> 
                <asp:Button ID="Button1" Text="Trigger" CommandArgument='<%# Eval("Id") %>' OnClientClick="$get('HiddenButton').click();return false;" 
                    runat="server" /> 
            </ItemTemplate> 
        </asp:Repeater>

它使用hiddenButton 通过将原始按钮的单击事件挂钩到此按钮来实现此目的。不过,我对此的补充是按钮的 CommandArgument 设置。我还需要为隐藏按钮设置它。

有谁知道解决这个问题的方法吗?

Hi I found code similiar to the following online. It's seems really great for getting a button buried in a repeater control to trigger a full cycle back to the server.

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
        </asp:ScriptManager> 

        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
            <ContentTemplate> 
                <%=DateTime.Now.ToString() %> 
            </ContentTemplate> 
            <Triggers> 
                <asp:PostBackTrigger ControlID="HiddenButton" /> 
            </Triggers> 
        </asp:UpdatePanel> 

        <!--Make a hidden button to treat as the postback trigger--> 
        <asp:Button ID="HiddenButton" runat="server" Style="display: none" Text="HiddenButton" /> 


        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> 
            <ItemTemplate> 
                 <!--when cick the button1, it will fire the hiddenButton--> 
                <asp:Button ID="Button1" Text="Trigger" CommandArgument='<%# Eval("Id") %>' OnClientClick="$get('HiddenButton').click();return false;" 
                    runat="server" /> 
            </ItemTemplate> 
        </asp:Repeater>

It uses a hiddenButton to achieve this by hooking the click event of the original button to this one. However my addition to this was the setting of the CommandArgument for the button. I would also need it to be set for the HiddenButton.

Does anyone know a way of going about this?

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

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

发布评论

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

评论(2

音盲 2024-11-09 07:34:05

首先,我想使用您发布的相同示例来解释使用更新面板缺点

以下是原始代码

在此处输入图像描述


输出

在此处输入图像描述


要显示 22 个字符串,您可以检查正在接收和发送到服务器的数据量。试想一下,

  1. 如果您考虑使用 Update Panel 将每个请求发送到 Database,并且您的 GridView 位于 Update Panel 中!!!!!!
  2. 假设您将为每个请求使用 ViewState 数据,并在更新面板内使用 GridView

根据我的理解,上述两种技术都是最糟糕的。


现在我将向您描述页面方法

更新面板上的页面方法

页面方法允许ASP.NET AJAX页面直接执行页面的静态方法,使用JSON(JavaScript 对象表示法)。我们可以使用 Web 方法 仅请求我们想要的信息,而不是发回然后接收 HTML 标记来完全替换 UpdatePanel 的内容。有兴趣。

示例代码

在此处输入图像描述
在此处输入图像描述


输出

“在此处输入图像描述”

希望这能清楚地解释用法的差异。


回答原始查询

您必须注册以下RepeaterItemDataBound 事件并使用以下代码。

代码

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
                           e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Button btn = (Button)e.Item.FindControl("Button1");
        btn.OnClientClick = string.Format("SubmitButton('{0}');return false;"
                                                        , HiddenButton.ClientID);

    }
}

JavaScript

<script type="text/javascript">
     function SubmitButton(btn)
     {
         $("#" + btn).click(); 
     }
</script>

//替代

<script type="text/javascript">
     function SubmitButton(btn)
     {
         document.getElementById(btn).click(); 
     }
</script>

参考< /a> & 此处

First I will like to explain the Disadvantage of using the Update Panel using the very same example posted by you.

Below is the Original Code

enter image description here


Output

enter image description here


To display the 22 character string you can check how much data is being received and sent to server. Just imagine following

  1. If you would consider send each request to Database using Update Panel and your GridView is in Update Panel!!!!!!
  2. Suppose you would use ViewState data for each request and with GridView Inside the Update Panel.

Both the above techniques are worst as per my understanding.


Now I will describe you Page Methods

Page Method over Update panel

Page Methods allow ASP.NET AJAX pages to directly execute a Page’s Static Methods, using JSON (JavaScript Object Notation). Instead of posting back and then receiving HTML markup to completely replace our UpdatePanel’s contents, we can use a Web Method to request only the information that we’re interested.

Sample Code

enter image description here
enter image description here


Output

enter image description here

Hope this clearly explains the difference of usage.


Answer to the original Query

You have to register the ItemDataBound event of the below Repeater and use below code for it.

Code Behind

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
                           e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Button btn = (Button)e.Item.FindControl("Button1");
        btn.OnClientClick = string.Format("SubmitButton('{0}');return false;"
                                                        , HiddenButton.ClientID);

    }
}

JavaScript

<script type="text/javascript">
     function SubmitButton(btn)
     {
         $("#" + btn).click(); 
     }
</script>

//Alternative

<script type="text/javascript">
     function SubmitButton(btn)
     {
         document.getElementById(btn).click(); 
     }
</script>

Reference & Here

往事风中埋 2024-11-09 07:34:05

您的 HiddenButton 控件是一个服务器控件,但您尝试使用它的 ASP.Net ID 在 JQuery 中访问它,

<asp:Button ID="Button1" Text="Trigger" CommandArgument='<%# Eval("Id") %>' OnClientClick="$get('**HiddenButton**').click();return false;" 
                runat="server" /> 

修复它的快速方法是创建一个单独的函数,

<script type="text/javascript">
function SubmitButton(btn)
{
     $get("#" . btn).click(); 
}
</script>

并将按钮代码更改为,

<asp:Button ID="Button1" Text="Trigger" CommandArgument='<%# Eval("Id") %>' 
                runat="server" /> 

在代码后面,在中继器的 ItemDataBound 中事件,找到按钮和 HiddenControl 并设置 Button1 的 OnClientClick 属性,

Button1.OnClientClick= string.Format("SubmitButton('{0}');return false;",HiddenButton.ClientID); 

Your HiddenButton control is a server control but you are trying to access it in JQuery using it's ASP.Net ID,

<asp:Button ID="Button1" Text="Trigger" CommandArgument='<%# Eval("Id") %>' OnClientClick="$get('**HiddenButton**').click();return false;" 
                runat="server" /> 

A quick way to fix it is to make a separate function,

<script type="text/javascript">
function SubmitButton(btn)
{
     $get("#" . btn).click(); 
}
</script>

and change your button code to ,

<asp:Button ID="Button1" Text="Trigger" CommandArgument='<%# Eval("Id") %>' 
                runat="server" /> 

In code behind, in repeater's ItemDataBound event, find the button and HiddenControl and set Button1's OnClientClick property,

Button1.OnClientClick= string.Format("SubmitButton('{0}');return false;",HiddenButton.ClientID); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文