Access Control in WebUserControl Fail via aspx page

M

MING@HongKong

I have a question about modify standard web control (textbox,listbox,label
etc....) inside ascx via aspx page.
the question detail is :
i have a aspx page( main.aspx) and a WebUserControl (MyControl.ascx)

main.aspx contain a button (btnadditem) and the control.ascx.

and control.ascx have one Listbox (Lstbox) and a Subroutine (AddItem) who
add some dummy item to (Lstbox) by For...Next ....

everything is work fine , but only not working is when i press the
btnadditem on main.aspx ,it WILL FIRE the Sub : AddItem and Postback

But the ListItem is not add the Listbox. But i believe that the sub have
been run, because i have add Debug.writeline(something) inside the for..next

I have added the <%@ Reference Control="~/MyControl.ascx" %> to main.aspx,
but it not help.

below is the source code for reference:

main.aspx.vb

1 Partial Class _Default
2 Inherits System.Web.UI.Page
3 Protected Sub btnadditem_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnadditem.Click
4 Dim ctl As MyControl= CType(Page.LoadControl("MyControl.ascx"),
MyControl)
5 ctl.addItem()
6
7 End Sub
8 End Class



MyControl.ascx.vb

1 Imports System.Diagnostics
2
3 Partial Class MyControl
4 Inherits System.Web.UI.UserControl
5
6 Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
7 ' addItem() ' NOT USE LstBox.items.add("Dummy")
LstBox.items.add("Dummy")
8 End Sub
9
10
11 Sub addItem()
12 LstBox.Items.Clear()
13 Response.Write("LstBox Clear : " & LstBox.Items.Count & "<BR>")
14 Response.Write("LstBox Start Add Item : <BR>")
15 For i As Integer = 0 To 10
16 Dim itemx As New ListItem
17 itemx.Text = Rnd(99)
18 itemx.Value = Rnd(99)
19 LstBox.Items.Add(itemx)
20 Response.Write(i & vbTab & itemx.Text & vbTab & itemx.Value
& "<BR>") 'It can show the i and itemx
21 Next
22
23 End Sub
24
25 End Class

the result is , the LstBox have not clear the 2 dummy item and have add the
listitem, but the i will print out follow 1,2,3,4,5,6,7,8....with the
Rnd(99)
But when i uncomment line 7, the Sub:additem will run correctly, so i think
the problem not come from Sub:additem.
i find answer on google, but really not much result i got.Thx for help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top