如何动态填充数据列表控件? [英] how to fill the datalist control dynamically?
本文介绍了如何动态填充数据列表控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按钮单击事件发生后显示数据列表控件.
我有以下代码:
i want to display the datalist control after the button click event occurs.
i have this code:
Imports system.data
Imports System.Data.SqlClient
Partial Class aboutus
Inherits System.Web.UI.Page
Dim c As String = "Data Source=VALUED-P0B2H9XS\SQLEXPRESS;Initial Catalog=dictionary1;Integrated Security=True"
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(c)
Dim s As String = "INSERT INTO postcomment (pname,pemail,pcomment,pdate) VALUES('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "','" + DateTime.Now + "');"
Dim com As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(s, con)
com.Connection = con
con.Open()
Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
adapter.SelectCommand = com
Dim dictionary1 As System.Data.DataSet = New System.Data.DataSet()
adapter.Fill(dictionary1, "postcomment")
con.Close()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(c)
Dim s1 As String = "SELECT * FROM postcomment;"
Dim com1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(s1, con1)
com1.Connection = con1
con1.Open()
Dim adapter2 As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
adapter2.SelectCommand = com1
Dim dictionary1 As System.Data.DataSet = New System.Data.DataSet()
adapter2.Fill(dictionary1, "postcomment")
Dim reader As SqlDataReader
reader = com1.ExecuteReader()
reader.Read()
If reader.Read() Then
DataList1.DataSource = dictionary1.Tables("postcomment")
Me.DataBind()
Else
DataList1.Visible = False
nocomment.Text = "No Comments Posted"
End If
reader.Close()
con1.Close()
End Sub
End Class
它无法正常工作.我认为这里有很多错误.有人帮助我.
it does not work properly. i think there is lot of mistakes. someone help me.
推荐答案
首先,您需要创建一个DataList项目Templet,其中将显示数据,然后尝试此操作...
First of All you need to create a Item Templet of DataList in which data will be displayed, then try this...
If reader.Read() Then
DataList1.DataSource = dictionary1.Tables("postcomment")
Me.DataBind()
Else
DataList1.Visible = False
nocomment.Text = "No Comments Posted"
End If
插入了Me.DataBind(),请尝试调用
的bind方法
DataList1.DataBind()
Inseted of Me.DataBind() please try to call a bind method of
DataList1.DataBind()
这篇关于如何动态填充数据列表控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文