无法从列表视图中的selectedindexchanged中找到控件 [英] cant find control from selectedindexchanged in listview

查看:100
本文介绍了无法从列表视图中的selectedindexchanged中找到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望能够从listview中的SelectIndexChanged中找到控件..

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
   Response.Write(myId);
   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);

   c.Open();
   cmd.ExecuteNonQuery();
   c.Close();          
}

推荐答案

您的文本框所在的控件.Findcontrol搜索父控件内的控件树。你可以通过指定父控件名称来使用它:



parentControl.Controls.FindControl(ControlName)
Which is the control in which your textbox is inside.Findcontrol searches the control tree inside a parent control.you can use it by specifying the parent control name as:

parentControl.Controls.FindControl("ControlName")


你好,下面是您可以尝试的伪代码。您可能需要根据控件ID进行一些修改。



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 ..so use its id
        foreach (Control ctrl in parent.Controls)
        {

            if (ctrl.GetType() == typeof(TextBox))
            {
              //control  found  ... return here
            }
           else
           {
              GetControl(ctrl);
           }

        }
    }


在selectedindexchanged事件中使用转换或制作文本框的对象然后你可以直接使用该文本框的值作为txtboxname.text
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


这篇关于无法从列表视图中的selectedindexchanged中找到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆