Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click event on master page occurs after Pre Init issue 1

Status
Not open for further replies.

daveonion

Programmer
Aug 21, 2002
359
GB
Hi,

I have a button on a master page and fields are created dynamically based upon a variable. The problem i have is creating the fields in the pre init component causes the pre init to occur before the button event on the main page. So the correct fields are only created on the second click of the button, does anyone have any ideas how to get around this issue.
 
The pre init event will always fire prior to a button click event regardless of whether you populate any controls there or not.

What I would suggest though, is that if you either need to make some form of identifier that will be available in the pre init event to work out what controls you need to create i.e. you could use javascript to populate a hidden element that is then posted along with the form. Or, you need your pre init event to create the controls, but use the button click event to actually populate the properties of the controls that you dynamically created.

I'd also suggest reading up in the page life cycle as you will need to understand it if you are dynamically creating controls.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Hi Mark,

Thanks for your reply, i created the controls on the pre init event and then as you mentioned on the click of the button i set the properties and then add the controls to the panel, however they appear and then quickly disappear on the page when i click the button. This has kinda confused me because i originally added the controls on the click of the button which made them appear and they stayed but the events didnt work, so i learnt that to add events to the controls they need to be added in the pre init event. So i guess my question is why would they appear and then quickly disappear. :)

Thanks
 
I think the easiest way for you to figure out how it needs to work in your situation is to go back to a simple example of how it should work, and then apply that logic to you specific needs. For example, take this simple page:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default35.aspx.vb" Inherits="Default35" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Text="Click Me!"></asp:TextBox>
    </div>
    </form>
</body>
</html>
Code:
Partial Class Default35
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim b As New Button
        AddHandler b.Click, AddressOf ButtonClick
        form1.Controls.Add(b)
    End Sub

    Protected Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(sender, Button).Text = "You changed this text to: " & TextBox1.Text
    End Sub
End Class
In this example, we:

1. Create a dynamic button every time the page is loaded.
2. Change the properties of the dynamically created button after the init event to something the user has entered on the page

When you run the page, you'll see that a button is created with no text. On each subsequent run of the page i.e when you click that button, you'll see that the text of the button changes to whatever you typed into the textbox.

This is essentially what you want to do, although obviously this is just a demonstration and your situation may be slightly more complicated. However, the logic is sound and that is what you need to apply to your code.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Hi Mark,

thanks once again for your help, i am creating a differing amount of datagrids based upon a variable specified in a textbox, the user will then click a button and the variable will determine how many datagrids are created. However as the pre init always occurs first the variable won't get past in. I decided to get around this problem by creating the datagrids as an array, on the click event i then add the relevant amount of datagrids to the panel. I have posted the code below
privat sub blahblah (ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim counter1 As Integer
SQLCmd.CommandText = "SELECT count(*) from dept_types"
CountMatch = SQLCmd.ExecuteScalar
SQLCmd.CommandText = "Select rtype, RDescription, sqlcommandtext from Dept_Types"
SQLdr = SQLCmd.ExecuteReader
ReDim Panelbody(CountMatch), paneltitles(CountMatch), Dg(CountMatch), cpe(CountMatch), panelHead(CountMatch)

'Declares the different array controls to be used within the summary screen
'Each expanding panel will need label panel head, panel body, datagrid and expanding collapsible panel

Dim lbl(CountMatch) As Label, lblhead(CountMatch) As Label
Dim TypeCount As Integer
Dim ButtonColumn(CountMatch) As ButtonField, lblb(CountMatch) As Label, RecordsFound As Boolean
sqldatagrid.Connection = SQLConn
counter1 = 1
RecordsFound = False

' For counter1 = 1 To ReferralCountMatch
Do
While (SQLdr.Read())
panelHead(counter1) = New Panel
panelHead(counter1).ID = "pH" & SQLdr("rType")
' Add Label inside header panel to display text

lblhead(counter1) = New Label
lblhead(counter1).ID = "lblHeader" & SQLdr("RType")
panelHead(counter1).Controls.Add(lblhead(counter1))


'Create Body Panel
Panelbody(counter1) = New Panel
Panelbody(counter1).ID = "pB" & counter1

'set up the datagrids
Dg(counter1) = New GridView
Dg(counter1).ID = "dg" & counter1
AddHandler Dg(counter1).RowCommand, AddressOf AllGridView_RowCommand
AddHandler Dg(counter1).RowDataBound, AddressOf allgridview_rowdatabound
AddHandler Dg(counter1).RowCreated, AddressOf GridView1_RowCreated
ButtonColumn(counter1) = New ButtonField
ButtonColumn(counter1).CommandName = "ViewForm"
ButtonColumn(counter1).ButtonType = ButtonType.Button
ButtonColumn(counter1).HeaderText = "View Form"
ButtonColumn(counter1).Text = "Picture"
Dg(counter1).Columns.Add(ButtonColumn(counter1))
Dg(counter1).DataBind()
Panelbody(counter1).Controls.Add(Dg(counter1))


' Create CollapsiblePanelExtender
cpe(counter1) = New CollapsiblePanelExtender()
cpe(counter1).ID = "cpe" & counter1
cpe(counter1).TargetControlID = Panelbody(counter1).ID
cpe(counter1).ExpandControlID = panelHead(counter1).ID
cpe(counter1).CollapseControlID = panelHead(counter1).ID
cpe(counter1).ScrollContents = False
cpe(counter1).Collapsed = True
cpe(counter1).ExpandDirection = CollapsiblePanelExpandDirection.Vertical
cpe(counter1).SuppressPostBack = True


RecordsFound = True
lblsummary.Dispose()
counter1 += 1
' sqlreadergrid.Close()
End While
Loop While SQLdr.NextResult()
SQLdr.Close()

private sub handles click


Do
While (SQLdr.Read())
counter1 += 1
sqldatagrid.CommandText = "select count(*) from " & SQLdr("type") & " where staff = " & Session("staffid")
recordcounter = sqldatagrid.ExecuteScalar
If recordcounter > 0 Then
' Add Label inside header panel to display text
'Create Body Panel
'set up the datagrids
sqldatagrid.CommandText = SQLdr("sqlcommandtext") & " where staff =" & Session("staffid")
sqlreadergrid = sqldatagrid.ExecuteReader
Dg(counter1).DataSource = sqlreadergrid
Dg(counter1).DataBind()
Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelHead(counter1))
Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(Panelbody(counter1))
Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(cpe(counter1))
RecordsFound = True
lblsummary.Dispose()
sqlreadergrid.Close()
End If
End While
Loop While SQLdr.NextResult()
SQLdr.Close()

End If


So basically the staff("sessionid") is picked up from a textbox and then the find button is used to look through various tables and create a dataview for each table in a collapsible panel. As i said this worked fine by only using the find button click event but i needed to add event to it. Now the panel appears and disappears, any further ideas

thanks
 
As I said above, rather than get drawn into how your specific situation isn't doing what you need, consider going back to the basics on how it should work. Create a sample test page that simply shows what you need, without any database access, update panels, other code etc like my example does and work it from there. This will help you work out exactly where the problem lies and then you can work it into your actual code (it also means that if you have any trouble getting it to work in a simple environment, that you can post the code here and others can use it to recreate your test scenario and attempt to get it working).

So, I'd expect your test harness to:

1. Dynamically create x datagrids
2. Dynamically create the same amount (x) of Panels

Once you've got this simple example working, it should then be easy to understand the logic of how it works, and apply it to your real scenario.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Hi Mark,

It turns out i'm a t**, the code was working it was just that i hadn't set the label to expand the panel so there was no way of expanding it. When it loaded it flashed the data on the screen and then collapsed.

Thanks for all your help. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top