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!

Accessing Info from Datalist Items 1

Status
Not open for further replies.

nwt002

MIS
Jun 16, 2005
58
US
Hello everyone.
I am using Microsoft Visual Studio 2005's Visual Web Developer to create a web application. On one page of this application I am using a Datalist. For each record that is returned in the datalist, it contains a label for the RecordID and labels for personal information(name, Birthdy, stateID, etc.) for each persons record. I don't want the SSN of each person to be listed for the world to see whenever someone is searching the database.

What I am trying to do is, each records listitem also has a textbox and a button. If the user has it available, I want them to be able to enter the SSN that they have, into the textbox and click the button to compare the SSN they entered with the SSN on that record, in cases where they may need to verify the social when searching for someone.

I have created the datalist and have all the fields and buttons where I want them, but I do not know how to reference the datalist items(labels, textbox, etc). When I click the button, I would like it to get the text from the label that contains the RecordID of that record, then have it search the database for that record and check if the SSN field for that record matches my textbox. Can someone please show me how to retrieve information from the datalist items? If you need any additional information, please let me know. Thanks.
 
This should let you get the appropriate control:

Dim lblSSN as Label
lblSSN = CType(DataList1.Items(index).FindControl("Control_Name"), Label)

Where:

index = index of the current item in the DataList
Control_Name = name of the control you want to find

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the quick response jebenson.

I tried your code by supplying the index manually and it worked great. I did have to add .text at the end like so,
Code:
 lbl1.Text = CType(DataList1.Items(1).FindControl("FirstNameLabel"), Label).Text
How do I get the index of the current datalist item, which contains the button I am clicking. I read online that I should be able to use e.Item.ItemIndex, but "Item" is not one of the properties for me to choose from when I type "e". Thanks.
 
e.Item.ItemIndex is used in the ItemDataBound event (among others), when data is loaded into the control.

Try using DataList1.Items(Datalist1.SelectedIndex)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I can't get this to work. What I am trying now is, when I click the button(inside each list item) it performs the following code so I can see what the index is:
Code:
lbl1.Text = DataList1.SelectedIndex
For some reason DataList1.SelectedIndex keeps returning a -1, like nothing is selected.

Thanks.
 
I figured out what was wrong. I had to set the CommandName property of my button to [blue]select[/blue] (case sensitive). I guess this actually allows the button to select the record.

After doing this, I also had to move my action code from the Button1_Click section to the DataList1_SelectedIndexChanged section. It appears that the Button Click actions run before the SelectedIndex gets changed, which was giving me the previous index value. Another link that I used is [URL unfurl="true"]http://msdn.microsoft.com/en-us/library/98fy3w7e(VS.71).aspx[/url].

I appreciate all the help!
 
No problem, and thanks for posting your final solution, this will help others in the future who may have the same issue.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top