为什么我不能使用GridView?
<LoggedInTemplate>
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2">
<Columns>
<asp:BoundField DataField="Name" HeaderText="File Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Id") %>' OnClick="DownloadFile" Text="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" OnClick="Upload" Text="Upload" />
</LoggedInTemplate>
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Id, Name from tblFiles";
cmd.Connection = con;
con.Open();
//GridView1.DataSource = cmd.ExecuteReader();
//GridView1.DataBind();
con.Close();
LoginView3.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
LoginView3.GridView1.DataSource = cmd.ExecuteReader();
}
这是截图。 https://www.dropbox.com/s/81186zjwkp482ms/Zrzut%20ekranu%202014-09-15%2009.10.48.png?dl=0
答案 0 :(得分:1)
您的gridview位于模板控件内,本例中为LoggedInTemplate。因此,它无法直接访问控件,它位于另一个控件的模板中。 您需要使用下面的代码找到控件
GridView gridView = yourMainControl.FindControl("GridView1") as GridView;
然后你可以进一步使用控件。