.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - get ClientID for a specific user control bounded in a repeater - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get ClientID for a specific user control bounded in a repeater - Stack Overflow

programmeradmin0浏览0评论

iv'e got a list of user controls which i bind to a repeater .

the user control : (Example) "AppProduct"

       <div>
            <asp:Button ID="btn_details" runat="server" Text="Trigger" /> 
            <asp:HiddenField ID="pid" runat="server" value="5"/> 
       </div>

the repeater :

       <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                 <App:Product ID="P1" runat="server" />
            </ItemTemplate>
       </asp:Repeater>

the problem :

when i press a certain "btn_details" on a certain user control i need to call a javascript or Jquery function which does something according to the value of "pid" , but those are the Server Side id's how can i get the ClientID for these controls for the user control i clicked on.

iv'e got a list of user controls which i bind to a repeater .

the user control : (Example) "AppProduct"

       <div>
            <asp:Button ID="btn_details" runat="server" Text="Trigger" /> 
            <asp:HiddenField ID="pid" runat="server" value="5"/> 
       </div>

the repeater :

       <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                 <App:Product ID="P1" runat="server" />
            </ItemTemplate>
       </asp:Repeater>

the problem :

when i press a certain "btn_details" on a certain user control i need to call a javascript or Jquery function which does something according to the value of "pid" , but those are the Server Side id's how can i get the ClientID for these controls for the user control i clicked on.

Share Improve this question edited Dec 31, 2011 at 0:01 eran otzap asked Dec 30, 2011 at 22:37 eran otzaperan otzap 12.5k21 gold badges88 silver badges146 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

.NET 4.0 set

 ClientIDMode = "Static"

For older .NET use

 '<%= Control.ClientID %>'

For your case

  function Get_Product_Details(uc) {
     var hidden_pid = uc.getElementById('<%= pid.ClientID %>');
  }

i found a work around for this problem

first i set the repeater ClientIDMode:

       <asp:Repeater ID="Repeater1" runat="server" ClientIDMode="Predictable">
             <ItemTemplate>
                <App:Product ID="prd1" runat="server" />
             </ItemTemplate>
        </asp:Repeater>

secondly i added a function to the btn_details on clientClick

        <asp:Button ID="btn_details" runat="server" Text="פרטים נוספים"  OnClientClick="Get_Product_Details(this);" />

in that function i extract the client id for the button and resolved the client id for the hidden field pid

         <asp:HiddenField ID="pid" runat="server" Value="5"/>

the function which will extract the client id //ContentPlaceHolder1_Repeater1_Prd_2_pid_2:

          function Get_Product_Details(btn) {
//ContentPlaceHolder1_Repeater1_Prd_2_btn_details_2
var s = btn.id;
var start = s.indexOf("btn_details");
var end = s.lastIndexOf("_");
sub = s.substring(start, end);
s = s.replace(sub, "pid");
var hidden = document.getElementById(s);
var id = hidden.value;

Since a repeater can include multiple instances of your control, it will append a number to the ID to uniquely identify it. Use jQuery to find the other element relative to the button. .siblings() should do the trick.

instead of using an hidden element for the pid on the button, you could instead attach the value via the jquery .data:

 private StringBuilder builder; 

 public void Page_Load(object sender, EventArgs e)
 {
     Repeater1.ItemDataBound += generateData;
     Repeater1.DataBound += outputGeneratedData;
     if (!Postback)
     {  
       List<Product> products = new List<Product>();
       // generate our data to bind
       Repeater1.DataSource = products;
       Repeater1.DataBind();
     }
 }
 // it is possible to do this inside the user control as well on page_load which would simplify it.
 public void generateData(objectSender, RepeaterItemEventArgs e)
 {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
            // presumes your custom control is the first control in the repeater.. if not you can use FindControl to get the relevent controly
           MyUserControl ctl = (MyUserControl)e.item.Controls[0];
           // you could expose the buttons id in a property on the control but if prefered just use FindControl.
            builder.AppendFormat("$('{0}').data('PID','{1}');", ctl.FindControl('btn_details').ClientID ,((Product)e.item.DataItem).ProductID); 
      }
  }

  public void outputGeneratedData(object sender,Eventargs e)
  { 
        Response.Write(@"<script type='text/javascript'> + builder.ToString() + "</script>");
  }

an access it like this on though the client js item:

 function Get_Product_Details(item)
 { 
        var productId =  $(item).data('ProductID');
 }
发布评论

评论列表(0)

  1. 暂无评论