Update Panel and triggers from a repeater control

瘦欲@ 提交于 2019-12-04 05:29:43

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


Output


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


Output

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

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); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!