Last post Jun 16, 2009 04:44 AM by GuyCre8ive
Jun 15, 2009 01:25 PM|GuyCre8ive|LINK
I have a datalist that returns multiple entries and I need to insert values via an OnClick command for a button I have with each entry. Problem is it's saying that a label I have in the datalist is not declared, in particular the
MyLabel.Text from my Insert statement below. Can someone show me with code how I can declare this label or is it even possible? When I put the label outside of the datalist, everything works fine but when it's inside it says it's not declared.
I've also tried using a gridview but it shows as not declared as well.
Protected Sub mybutton_Click(ByVal sender As Object, ByVal e As Object)
Dim cmd As New SqlCommand("INSERT INTO [myTable] ([SessionID], [myID]) VALUES ('" & Session.SessionID & "', '" & MyLabel.Text & "')", New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString.ToString()))
Jun 15, 2009 01:49 PM|ramireddyindia|LINK
you can't use the controls which are in the gridview or datalist,
you need to find that control using findcontrol() method, then u need to use that.
Label mylbl = (Label)DataList1.Items[datalist itemno].FindControl("MyLabel");
then u can use that mylbl text property.
in that above code, getting that datalist item no depends on the scenarion and the event you are implementing.
Jun 15, 2009 02:01 PM|GuyCre8ive|LINK
I've tried the findcontrol() method every way I can think of and I can't seem to get it to work. I'm still kind of new to .NET, any chance you have code you can show me what you mean with? I've been looking all over and I can't find any examples anywhere.
Jun 15, 2009 02:09 PM|ramireddyindia|LINK
and also post your code so far what you did.
Jun 15, 2009 06:22 PM|BlueJam|LINK
You should be able to find your label by grabbing the parent control of the button (the DataListItem) then using FindControl to look for your label.
For example :-
Protected Sub mybutton_Click(ByVal sender
As Object, ByVal e
Dim MyLabel As Label =
Jun 16, 2009 04:44 AM|GuyCre8ive|LINK
Hey that works, thank you very much..