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

dynamic button in VB

Status
Not open for further replies.

kaijanm

Programmer
May 14, 2003
102
US
Hi All,

Thanks in advance for any help. I know this has been covered elsewhere, but I can't seem to get it to work. The examples I have seen have been in C#, and I'm trying them in VB.

I'm creating a button for each row pulled from a database and assigning the commandargument to the the key of that row. I have two things I can't get to work.

One: When I create the button in the code behind file:
Dim lnkChange As New LinkButton()
lnkChange.Text = "Change"
lnkChange.ID = "ChangeDep"
lnkChange.CommandName = "ChangeDependent"
lnkChange.CommandArgument = rdrDependents.Item("dep_access_cntr").ToString

I don't get an OnClick or OnCommand option for lnkChange. I can't find anything that seems to serve the same purpose. I tried putting it and seeing what would happen (even with the wiggly blue line), but it didn't work...

This is some code I pulled from Microsoft's help:
<asp:Button id=&quot;Button1&quot;
Text=&quot;Sort Ascending&quot;
CommandName=&quot;Sort&quot;
CommandArgument=&quot;Ascending&quot;
OnCommand=&quot;CommandBtn_Click&quot;
runat=&quot;server&quot;/>

but I don't get that OnCommand option in the code behind.

Two: Even if I could get the button to know what to do when it was clicked, I'm having these problems: I wrote a sub procedure that's supposed to run when the button is clicked:
Private Sub ChangeDependent(ByVal sender As Object, ByVal e As EventArgs)
Session(&quot;DepCntr&quot;) = e
Server.Transfer(&quot;ChDependent.aspx&quot;)
End Sub

This shows up without problem in the code behind, but the example I found from Microsoft says:
Sub CommandBtn_Click(sender As Object, e As CommandEventArgs)
' Insert code to sort in ascending order here.
Message.Text = &quot;You clicked the &quot; & e.CommandName & _
&quot; - &quot; & e.CommandArgument & &quot; button.&quot;
End Sub

So they're referring to e.CommandArgument, but when I do this, I get the wiggly blue line under the code and it says:
'CommandArgument is not a member of 'System.EventArgs''.

I tried adding Handles lnkChange after the function head line (like VS does when it creates the function), but it gives me the wiggly blue line once again.

Ok, well I hope this is clear!

Thanks a bunch!
Kimberly
 
What language are you using? I was unsure if your using vb or c#.

This line
<asp:Button id=&quot;Button1&quot;
Text=&quot;Sort Ascending&quot;
CommandName=&quot;Sort&quot;
CommandArgument=&quot;Ascending&quot;
OnCommand=&quot;CommandBtn_Click&quot;
runat=&quot;server&quot;/>


is not code behind it is part of declaration. The onCommand is setting which function to call when the button fires. Check out the help files on dynamically allocating event handlers for the language your working in. VB and C# do this a bit differently.

As for e.CommandArgument I used this when I was handling a button click event that come from a datagrid. The event handler actually handles the ItemCommand event. One of the arguments for this event handler is System.Web.UI.WebControls.DataGridCommandEventArgs. Which contains the CommandArgument Property.

I hope this clears some things up for you.


That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I'm using VB. I realize the first code I posted was part of th e declaration and not the code behind, but it seemed strange that there is an oncommand there and nothing similar in the code behind.

I guess I'm still highly confused. I haven't gotten anywhere on it.

Thanks for your ideas, though! :)

Kimberly
 
Well the oncommand sets the method to fire when the button is clicked. In code behind this is done differently. In VB with the handles bit after the method declaration. When your adding the handles bit make sure to make use of VS's intellisense so you get your syntax correct. Should be something like Handles ButtonID.Click or something similiar. I haven't used vb in a while.

You said your creating a button for each row? Where is this button? In a datagrid? On the page?. If it's within a datagrid, use the buttoncolumn column and set up the properties for the button within the property builder. This will let you set your command arugment which you can check within the ItemCommand event handler for the datagrid.

If it's on the page then you'll have to check into dynamic event handling. This may be of some help
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I tried using the IntelliSense (for handles), but it only gives me options for items that were dragged onto the page, not for items that were created in the code behind.

The buttons are going into a table that I'm creating in code-behind. There's too much data for a datagrid to be practical (unless there's a lot I don't know about them).

I'll take a look at the page you posted and let you know if it answers my question! :)

Thanks! :)
Kimberly
 
There is alot to the datagrid. The more I work with it the more I like it. I am still finding new ways that I can use that paticular control.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Well, since we're on the subject...

Can you do multiple rows on a datagrid for one row of data from SQL server? :)

By the way, I think I may have found something from the link you sent me. I'll post if it works. :)
 
Do you mean like a child table or having a single row from a table displayed as more than 1 row?

If you mean a child table then you can nest a grid within the other. On each row this second grid will appear and you can fill it with the data that corresponds to the row it's in.

If you want a single row displayed as more than one then a datalist rather than a data grid will be what you want.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top