猿问

如何在GridView的TemplateField中找到控件?

如何FindControl方法的工作,如果我需要找到任何Control这里面GridView Template,更具体ItemTemplate?


我有一个,hyperlink但找不到它。


问题已用代码更新


GridView 代码如下:


<asp:GridView ID="grvYourOpportunities" runat="server" AllowPaging="True" AutoGenerateColumns="False"

        DataKeyNames="ID#,B,H" PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="Both"

        OnRowDataBound="grvYourOpt_RowDataBound">

        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

        <Columns>

          <asp:TemplateField>

                <HeaderTemplate>

                    i

                </HeaderTemplate>

                <ItemTemplate>

                    <%# Eval("i") %>

                </ItemTemplate>

            </asp:TemplateField>


            <asp:TemplateField>

                <HeaderTemplate>

                    H

                </HeaderTemplate>

                <ItemTemplate>


                    <%--<a href="javascript:ShowChildGrid('div<%# Eval("ID#") %>');">

                    <img id="imgdiv<%# Eval("ID#") %>" alt="Click" border="0" src="plus.gif" runat="server" />  </a>  --%>


                    <asp:HyperLink runat="server" ID="hlPlus" ImageUrl="~/plus.gif"></asp:HyperLink>


                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField>

                <HeaderTemplate>


RowDataBound父母的事件GridView如下。

如何FindControl方法的工作,如果我需要找到任何Control这里面GridView Template,更具体ItemTemplate?

繁星点点滴滴
浏览 639回答 3
3回答

ITMISS

尝试这个:foreach(GridViewRow row in GridView1.Rows) {&nbsp; &nbsp; if(row.RowType == DataControlRowType.DataRow) {&nbsp; &nbsp; &nbsp; &nbsp; HyperLink myHyperLink = row.FindControl("myHyperLinkID") as HyperLink;&nbsp; &nbsp; }}如果要处理RowDataBound事件,则如下所示:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){&nbsp; &nbsp; if(e.Row.RowType == DataControlRowType.DataRow)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HyperLink myHyperLink = e.Row.FindControl("myHyperLinkID") as HyperLink;&nbsp; &nbsp; }}

暮色呼如

您可以使用此代码HyperLink在GridView中查找。用于e.Row.Cells[0].Controls[0]在GridView中查找控件的第一个位置。protected void AspGrid_RowDataBound(object sender, GridViewRowEventArgs e){&nbsp; &nbsp;&nbsp;&nbsp; if(e.Row.RowType == DataControlRowType.DataRow)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DataRowView v = (DataRowView)e.Row.DataItem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (e.Row.Cells.Count > 0 && e.Row.Cells[0] != null && e.Row.Cells[0].Controls.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HyperLink link = e.Row.Cells[0].Controls[0] as HyperLink;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (link != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link.Text = "Edit";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}

慕森王

我已经完成了访问单元格控件内的控件的操作。在所有控件集合中查找。&nbsp;ControlCollection cc = (ControlCollection)e.Row.Controls[1].Controls;&nbsp;Label lbCod = (Label)cc[1];
随时随地看视频慕课网APP
我要回答