当前位置:编程学习 > C#/ASP.NET >>

|zyciis| 在Repeater的ItemTemplate中,如何一个js如何最快得到里面的一个控件的ID


        <asp:Repeater ID="rptRecprd" runat="server" OnItemDataBound="rptRecprd_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td align="left" class="mytd">
                        <asp:TextBox ID="txtSrc" Width="200" runat="server" CssClass="txtChn" Text='' />
                        <script>alert('我的ID是' + ???)</script>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>

如上面的 HTML,如果不是在绑定列中的话我们可以用<%=txtSrc.ID %>
但在绑定列中要怎么做呢

谢谢 --------------------编程问答-------------------- 绑定的目标列要在操作该itme时才会能获取的. --------------------编程问答--------------------     public string firstRowId;
    protected void Page_Load(object sender, EventArgs e)
    {
        firstRowId = (GridView1.Rows[1].FindControl("txtSrc") as TextBox).ClientID;
    }


    <input type="button" value="看第一行ID" onclick="alert('<%=firstId %>')" />


想看其他行自己改 --------------------编程问答-------------------- 后台绑定的时候把ID绑到里边就好了吧~ --------------------编程问答--------------------

        <asp:Repeater ID="rptRecprd" runat="server" OnItemDataBound="rptRecprd_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td align="left" class="mytd">
                        <asp:TextBox ID="txtSrc" Width="200" runat="server" CssClass="txtChn" Text='' />
                        <script>alert('我的ID是' + <%# (gvDepartment.Rows[Container.DataItemIndex].FindControl("btnDelete") as LinkButton).ClientID %>)</script>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>


为什么这样不行呢 --------------------编程问答-------------------- 开始没注意是Repeater,下面这样看起来更直观,每个文本框显示自己的ID

    <table>
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
            onitemdatabound="Repeater1_ItemDataBound">
                <ItemTemplate>
                <tr>
                    <td align="left" class="mytd">
                        <asp:TextBox ID="txtSrc" Width="200" runat="server" CssClass="txtChn" Text='' />
                    </td>
                </tr>
            </ItemTemplate>
    </asp:Repeater>
    </table>

设置Repeater1的ItemDataBound事件
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        TextBox txt = e.Item.FindControl("txtSrc") as TextBox;
        txt.Text = "我的ID是" + txt.ClientID;
    } --------------------编程问答-------------------- 我就想不要在cs中写代码,而只在这个ItemTemplate中写
谢谢 --------------------编程问答-------------------- 路过的给你顶一下啊,我还是个菜鸟 --------------------编程问答--------------------
引用 6 楼 zyciis310 的回复:
我就想不要在cs中写代码,而只在这个ItemTemplate中写 
谢谢

那你就直接用js遍历吧.

 var list=new Array();
 var listIpt=document.getelementsByTagName("input");
 function SelAll()
 {
   for(var i=0;i<listIpt.length;i++)
   {
     list[i]=listIpt.id;
   }
 }

--------------------编程问答-------------------- getelementsByTagName==>getElementsByTagName --------------------编程问答-------------------- UP --------------------编程问答-------------------- 变通一下:

                    <span id="span<%# Container.ItemIndex %>">
                        <asp:TextBox ID="txtSrc" Width="200" runat="server" CssClass="txtChn" Text='' />
                    </span>

                    <script>
                        alert(document.getElementById("span<%# Container.ItemIndex %>").getElementsByTagName("input")[0].id);
                    </script>

--------------------编程问答--------------------
引用楼主 zyciis310 的帖子:
HTML code
        <asp:Repeater ID="rptRecprd" runat="server" OnItemDataBound="rptRecprd_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td align="left" class="mytd">
                        <asp:TextBox ID="txtSrc" Width="200" runat="server" CssClass="txtChn" Text='' />
                        <script>alert('我的ID是' + ???)</script>
                 …




<asp:Repeater ID="rptRecprd" runat="server" OnItemDataBound="rptRecprd_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td align="left" class="mytd">
                        <asp:TextBox ID="txtSrc" Width="200" runat="server" CssClass="txtChn" Text='' />
                        <script>alert('我的ID是'+'<%# ((sender as DataBoundLiteralControl).FindControl("txtSrc") as TextBox).ClientID  %>');</script>                      </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>

  
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,