大侠我的button_click里面的查询语句也需要数组里的那些值,但提示"未声明名称strids"怎么解决呢?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
dim strids as string="";
Dim total_topic_num As Integer = 12
Dim test_topic_num As Integer = 5
Dim interval As Integer = total_topic_num \ test_topic_num
Dim big_interval_num As Integer = total_topic_num Mod test_topic_num
Dim array_topic_id(test_topic_num) As Integer
Dim i As Integer = 0
If big_interval_num <> 0 Then
For i = 0 To test_topic_num - 1 - big_interval_num
Randomize()
array_topic_id(i) = Int(interval * Rnd() + i * interval)
strids = strids & array_topic_id(i) & ","
Next
For i = test_topic_num - big_interval_num To test_topic_num - 1
Randomize()
array_topic_id(i) = (test_topic_num - big_interval_num) * interval + Int((interval + 1) * Rnd() + (i - (test_topic_num - big_interval_num)) * (interval + 1))
strids = strids & array_topic_id(i) & ","
Next
Else
For i = 0 To test_topic_num - 1
Randomize()
array_topic_id(i) = Int(interval * Rnd() + i * interval)
strids = strids & array_topic_id(i) & ","
Next
End If
databind1(strids.trimend(","c)
End If
End Sub
Sub databind1(byval strids as string)
Dim connstr As String
connstr = ConfigurationManager.AppSettings("strconn").ToString
Dim conn As New SqlConnection(connstr)
Dim cmd As New SqlCommand("select * from shuju where id in (" & strids & ") ", conn)
conn.Open()
Dim adp As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
adp.Fill(ds, "link")
DataList1.DataSource = ds.Tables("link").DefaultView
DataList1.DataBind()
conn.Close()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connstr As String
connstr = ConfigurationManager.AppSettings("strconn").ToString
Dim conn As New SqlConnection(connstr)
Dim myadapter3 As SqlDataAdapter = New SqlDataAdapter("select * from shuju where id in (" & strids & ")", conn)这里也要用到strids但提示未声明名称strids
.......
[解决办法]
dim strids as string="";
把上面这句提到函数外,作为页面级的成员变量,并把上的dim改成private
即
private strids as string="" '加上这行
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'dim strids as string="";'注销这行
.....