Click here to Skip to main content
15,121,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
want be able to find control from SelectIndexChanged in listview..
TexBox comment= (TextBox)item.FindControl("TextBoxL");// this code in not working in SelectIndexChanged

TextBox btnModify = (TextBox)FindControl("TextBoxL");// i tried this one also but this is also not working...

protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
   TextBox btnModify = (TextBox)FindControl("TextBoxL");
   SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

   int myindex = ListView1.SelectedIndex;
   string myId = ListView1.DataKeys[myindex][0].ToString();
   //testing the value returned correctly
   SqlCommand cmd = new SqlCommand("insert into rply(task_id,comment,rply_name,rpl_date)values('" +  myId + "','" + btnModify.Text + "','" + s2 + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "')", c);

Updated 22-Oct-14 1:31am
ZurdoDev 21-Oct-14 8:01am
FindControl is not recursive so if you have a table structure or panel or some other control you may have to do item.Controls[0].FindControl. You'll have to examine your output to see where that control really is.
Aarti Yadav 21-Oct-14 9:05am
i tried this but item.Controls[0].FindControl. but null value is saving in databse
ZurdoDev 21-Oct-14 9:08am
Then perhaps it is Controls[1]. The point is you have to figure it out. We can't see what you have.
Richard Deeming 21-Oct-14 8:32am
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
ashish__shukla 21-Oct-14 9:03am
I had got your point.I had updated the article as well but it was closed before the changes could be approved :-).

Which is the control in which your textbox is inside.Findcontrol searches the control tree inside a parent can use it by specifying the parent control name as:

Aarti Yadav 21-Oct-14 7:47am
TextBox is inside a listview..
and i am saveing a data into a database from SelectedIndexChanged

so in SelectedIndexChanged TextBox 's value in not saving in database.. or its not finding a textbox

and the code which u gave that is also not working..
Aarti Yadav 21-Oct-14 9:04am
there is any1 who can help me to get out of this
Hi,below is the pseudo code you can try.You may need to modify it a bit depending on the control id's.

public void GetControl(Control control)
        //parent could be listview use its id
        foreach (Control ctrl in parent.Controls)

            if (ctrl.GetType() == typeof(TextBox))
              //control  found  ... return here

use casting or making an object of the textbox on the selectedindexchanged event and then u can directly use the value of that textbox as txtboxname.text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900