在将数据显示到gridview时遇到了这个问题。我想将数据从 BindData 显示到如下所示的自定义网格视图中。我已经尝试过 在与html>正常的页面上编写代码(但我的高级工程师不希望这样做),所以现在我需要使用后端代码来重做它,并使用数据集将那些(ID,Name,Age)传递给那些标签ID 。我该怎么做到?
<asp:GridView ID="MyGridView" runat="server" CellPadding="5" CssClass="ControlStyle"
ForeColor="#333333" Font-Size="8px" GridLines="Vertical" width="40%" CaptionAlign="Left"
PageSize="15" AllowPaging="True" OnPageIndexChanging="MyData_OnPageIndexChanging"
AutoGenerateColumns="False" enableEventValidation="false">
<Columns >
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="img" runat="server" ImageUrl="~/Images/arrow-2a.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lbl_Age" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
我想在下面显示数据。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindData()
End If
End Sub
Private Sub BindData()
Dim sCon As String = "MyConnectionString"
Using con As New SqlConnection(sCon)
Using cmd As New SqlCommand(" select * from users ")
Dim sda As New SqlDataAdapter()
Try
cmd.Connection = con : con.Open()
sda.SelectCommand = cmd
Dim dt As New DataTable
sda.Fill(dt)
'BIND DATABASE WITH THE GRIDVIEW.
MyGridView.DataSource = dt
MyGridView.DataBind()
Catch ex As Exception
txtMsg.Text = ex.Message
End Try
End Using
End Using
End Sub
使用上面的代码,我的页面什么也不显示,只有白色画布,我在哪里做错了?请指导。
谢谢
答案 0 :(得分:0)
感谢您的辛勤工作,我已经设法通过修改此代码来解决此问题
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindData()
End If
End Sub
进入此
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindData()
End If
End Sub
我仍然不知道这两者之间有什么区别,但是我设法取得了预期的结果。