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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Web Control at runtime

Status
Not open for further replies.

f0z

Programmer
Jan 10, 2003
48
US
Hi,

I have a basic search function that retrives information from a database based on information that is supplied in one or two supplied textboxes. The information that i get back is being put into a asp:table using TableRow.Cells.Add(TableCell) method.

Like I said this is pretty basic, and is working fine.

I want to expand this by letting the users click a LinkButton, corresponding to a certain record returned, to delete this record.

I thought that I'd be able to dynamically add the linkbutton by adding a LinkButton to a TableCell and then adding this TableCell to the TableRow as I've done with all the information out of the database.

Now the link buttons are all being generated, but I can't seem to get them wired up, using a LinkButton.Command += etc... or LinkButton.Click += etc...

In the function I've created to handle the Click event it's been set to just set the text of a label, and it seems that this function just isnt being called.

I've read somewhere that this might be something to do with the web control life cycle, but this didn't really help me.


I'm aware that I could just put the info into a datagird with a delete column, this has just been annoying me today.

Any suggestions are much appreciated.

cheers,

foz
 
What you need to do is use the the AddHandler method. You need to define a function to hanlde the event and then you call the AddHandler routine passing in the controls method and the address of your fuction i.e.

Code:
AddHandler objCommandButton.Click, AddressOf NavigationClick

The following is the signature of the method that the event calls:

Code:
Friend Sub NavigationClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

I am pretty sure that you can play around with the event args to match the signature for the standard linkbutton event signature.

James :)

James Culshaw
james@miniaturereview.co.uk
 
cheers for the quick reply james,

i'll let you know how i get on with the addhandler, if i get anywhere ;)

f0z
 
so i should us lb1.Command += new System.EventHandler(newFunction), and place this in the Page_Load()
 
ok, still having difficulties.

more details:

i have a staticly created asp:table, to which I am dynamically adding TableRows as follows:

TableRow tr;
TableCell tcUID;
TableCell tcDelete;

if(!sqlDR.Read())
{
this.lblResults.Text = "n/a: no matching results!"
}
else
do
{
tr = new TableRow();
tr.ID = "trResult"+i;
tcUID = new TableCell();
tcUID.Text = sqlDR["UID"].ToString();
tcDelete = new TableCell();
// here it starts to get interesting
lb1 = new LinkButton(); // linkbutton already globally defined earlier
lb1.ID = "lb"+i;
lb1.Text = "Delete";
lb1.CommandArgument = i.ToString();
lb1.CommandName = "Delete";
lb1.Command += new System.Web.UI.WebControls.CommandEventHandler(OnLinkClick);
tcDelete.Controls.Add(lb1);
tr.Cells.Add(tcUID);
tr.Cells.Add(tcDelete);
tblResults.Add(tr);
i++;
}while(sqlDR.Read());

------------------------------------------------------------

public void OnLinkClick(object o, System.Web.UI.WebControls.CommandEventArgs e)
{
// not getting here!
Response.Write("anything");
}

again any help is greatly appreciated.

cheers f0z
 
What I do is that I build the table and then when I have finished building the table I loop through all the controls in the correct columns cells and if they are a link button I then add the event handler by calling a sub i.e.

Code:
Private Sub AddNavigationEventHandler()

        Dim objHolderCell As TableCell
        Dim objTable As Table
        Dim objRow As TableRow
        Dim objPanel
        Dim objCommandButton As Button
        Dim objControl, objControl2 As Control

        objHolderCell = CType(phForm.Controls(0), Table).Rows(0).Cells(0)

        For Each objControl In objHolderCell.Controls

            objTable = CType(objControl.Controls(0), Table)

            For Each objRow In objTable.Rows

                If objRow.Cells.Count = 1 Then

                    For Each objControl2 In objRow.Cells(0).Controls

                        If objControl2.GetType.FullName = "System.Web.UI.WebControls.Button" Then

                            objCommandButton = CType(objControl2, Button)
                            AddHandler objCommandButton.Click, AddressOf NavigationClick

                        End If

                    Next objControl2

                End If

            Next
        Next objControl

    End Sub

The reason I do this is because the table is created as a completely new table by another calss and passed back as a return parameter from a function, and I then add it to a placeholders control collection. After I have added the table object I then called this method.

james :)



James Culshaw
james@miniaturereview.co.uk
 
This seems like a very messy way of doing what i want to do, but if it works, great.
 
ok i've got a working solution.

i set the function that i've created to generate the tablerows of results (generate_table) to run when the page loads.

this creates duplicate results when i click on my search button, which calls generate_table and reloads the page.

so i've just removed the generate_table from the search button click and everything works fine.

not the prettiest coding every, but it's working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top