How do I get the value out of a dataList control field and put it in a variable? (ASP/C#)

How do I get the value out of a dataList control field and put it in a variable?
The field name is imgID (int)
I would like to put this value in a variable called phID.  How do i do this?
bmanmike39Asked:
Who is Participating?
 
havj123Connect With a Mentor Commented:
ok then in datalist itemcommand event find ur control like this

Label lbl = e.Item.FindControl("Lbl1") as Label;
int i;
if(lbl != null)
{
  i = convert.ToInt32(lbl.Text);
}
0
 
havj123Commented:
Find the control using FindControl method and put it in a variable

Like
TextBox tb = (TextBox)datalist1.FindControl("text1");
int x;
x = convert.ToInt32(tb.Text);
0
 
bmanmike39Author Commented:
I get the following  ERROR:
"object reference not set to an instance"
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
havj123Commented:
in which event of datalist u find ur control. Can u show me the code
0
 
bmanmike39Author Commented:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{


string b;
Label z = (Label)(DataList1.FindControl("Lbl1"));
Response.Redirect(z.ToString());
}

0
 
OmerFarukZCommented:
try this code

TextBox tb = (TextBox)datalist1.Items[0].FindControl("text1");
int x;
x = convert.ToInt32(tb.Text);
0
 
havj123Commented:
You can find textbox inside datalist in a imagebutton click event. IF ur image button is inside the datalist then use your code in datalist itemcommand event.

0
 
havj123Commented:
Is ur image button is inside datalist?
0
 
bmanmike39Author Commented:
yes it is
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.