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

GridView怎么去取Label 在线等 没分了 不好意思


                for (int i = 0; i < DataList1.Items.Count; i++) 
                {
                    string lab = ((Label)DataList1.Items[i].FindControl("Label1")).Text.ToString();
                }
                

//DataList是这样取出Label1的Text的  那么GridView怎么去取? 谢谢  在线等马上结贴 --------------------编程问答-------------------- for (int i = 0; i < GridView1.Rows.Count; i++) 
                {
                    string lab = ((Label)GridView1.Rows[i].FindControl("Label1")).Text;
                }
--------------------编程问答-------------------- GridView也可以这样 啊 
GridView1.Rows[0].Cells[0].FindControl("Label1") --------------------编程问答-------------------- 没有测试,应该没有什么问题。 --------------------编程问答-------------------- Label Label1= (Label)row.FindControl("Label1");
string lb=Label1.text --------------------编程问答-------------------- for (int i = 0; i < GridView1.Rows.Count; i++)
{
  string Lab = ((HyperLink)GridView1.Rows[i].FindControl("HyperLink2")).Text.ToString();
  Response.Write(Lab);
}

这样还是取不出来 是空的!~ --------------------编程问答-------------------- HyperLink2是HyperLink?id没错么?是在模板列里面的么? --------------------编程问答-------------------- 恩ID可定没错!~~~ 是GridView在ItemTemplate模板里面的


for (int i = 0; i < GridView1.Rows.Count; i++)
{
  string Lab = ((HyperLink)GridView1.Rows[i].Controls[i].FindControl("HyperLink2")).Text.ToString();
  Response.Write(Lab);
}

HyperLink2的.Text是文章名字 --------------------编程问答--------------------
引用 7 楼 he_yang 的回复:
恩ID可定没错!~~~ 是GridView在ItemTemplate模板里面的

C# codefor(inti=0; i<GridView1.Rows.Count; i++)
{stringLab=((HyperLink)GridView1.Rows[i].Controls[i].FindControl("HyperLink2")).Text.ToString();
  Response.Write(Lab);
}
HyperLink2的.Text是文章名字

这个Controls[i]不需要
另外最好能把你GridView的前台代码贴出来看看 --------------------编程问答--------------------  
Label Lab = (Label)GridView1.Rows[i].FindControl("Label"); --------------------编程问答--------------------

<asp:GridView ID="GridView1" style="border-top:none; border-right:none; border-left:none; border-bottom-color:#33ff66" BorderStyle="None" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" BackColor="Transparent" BorderColor="White" BorderWidth="0px" Width="450px">
                    <Columns>
                        <asp:TemplateField HeaderText="查询结果:">
                            <ItemTemplate>
                                <table border="0" cellpadding="0" cellspacing="0" style="width: 450px; border-bottom-width: 2px; border-bottom-color: whitesmoke;">
                                    <tr>
                                        <td colspan="2" style="height: 20px" align="left">
                                            <asp:HyperLink ID="HyperLink2" runat="server" ForeColor="#FF8000" NavigateUrl='<%# "Lookxinxi.aspx?CompanyDetailsID="+DataBinder.Eval(Container.DataItem,"CompanyDetails") %>'
                                                Text='<%# DataBinder.Eval(Container.DataItem,"KeywordTitle") %>'></asp:HyperLink></td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="left">
                                            <asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"KeywordContent") %>'></asp:Label></td>
                                    </tr>
                                    <tr>
                                        <td style="height: 20px; border-bottom: whitesmoke 2px dashed;" align="left" valign="top">
                                           <%-- <asp:Label ID="Label5" runat="server" ForeColor="DeepSkyBlue" Text='<%# DataBinder.Eval(Container.DataItem,"CompanyName") %>'
                                                Width="221px" Font-Size="12px"></asp:Label>--%>
                                            <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CompanyName") %>'></asp:Label></td>
                                        <td style="height: 20px; border-bottom: whitesmoke 2px dashed;" align="left" valign="bottom">
                                            <asp:HyperLink ID="HyperLink3" Target="_blank" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "www")%>'
                                                Text='<%# DataBinder.Eval(Container.DataItem,"www")%>' Font-Size="12px" Width="223px"></asp:HyperLink></td>
                                    </tr> 
                                    <tr>
                                        <td align="left" style="width: 225px; height: 20px" valign="top">
                                             </td>
                                        <td align="left" style="height: 20px" valign="bottom">
                                             </td>
                                    </tr>
                                </table>
                                 
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle BackColor="White" />
                </asp:GridView>
--------------------编程问答-------------------- 前天没有什么东西 就是几个绑定 --------------------编程问答-------------------- for (int i = 0; i < GridView1.Rows.Count-1; i++) 
                { 
                    string lab = ((Label)GridView1.Rows[i].FindControl("Label1")).Text; 
                } 
--------------------编程问答-------------------- for (int i = 0; i < GridView1.Rows.Count; i++)
{
  string Lab = ((HyperLink)GridView1.Rows[i].Cells[你那一列的Index].FindControl("HyperLink2")).Text;
  Response.Write(Lab);
}

Text本来就是string类型的,不用再ToString. --------------------编程问答-------------------- 看你的代码没什么问题,你是在哪调用的这个for循环? 会不会是在没绑定数据之前呢
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,