protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string s2 = "select subDivisionName from Tbh_SubDivsionMaster";
            SqlDataAdapter ada = new SqlDataAdapter(s2, con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            DropDownList ddl1 = (DropDownList)e.Row.FindControl("ddl1");
            ddl1.DataSource = ds;
            ddl1.DataTextField = "subDivisionName";
            ddl1.DataBind();
         }
    }
    protected void ddl1_SelectedIndexChanged1(object sender,EventArgs e)
    {
        DropDownList d1=new DropDownList();
        DropDownList d2=new DropDownList();
        DropDownList d3=new DropDownList();
        foreach(GridViewRow row in grid1.Rows)
        {
            d1=(DropDownList)row.FindControl("ddl1");
            d2=(DropDownList)row.FindControl("div");
            d3=(DropDownList)row.FindControl("cir");
            if (d1.SelectedValue.ToString() == "CD I UDR")
            {
                d2.Visible = false;
                d3.Visible = false;
            }
            else
            {
                d2.Visible = true;
                d2.Visible = true;
            }
        }
    }

方法五】如果在模板列中添加一下DropDownList控件,并开启其AutoPostback属性,在DropDownList 的SelectedIndexChanged事件中,获取GridView中的当前行。

下面是SelectedIndexChanged事件的代码摘要:

DropDownList ddl = (DropDownList)sender;

GridViewRow gvr = (GridViewRow)ddl.NamingContainer;

int id = int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString());

int num = int.Parse(ddl.Text);

第一句用来获取触发事件的DropDownList控件。

第二句就利用该控件的NamingContainer属性,获取其容器,也就是GridViewRow对象。

 

提示:由于DropDoweList与button不同,无法指定其CommandName,所以,通过用NamingContainer属性来解决问题。

先来看看微软对该NamingContainer属性的解释:

获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 Control.ID 属性值的服务器控件。

ASP.NET Web 应用程序的每一页均包含控件的层次结构。此层次结构与控件是否生成用户可见的 UI 无关。给定控件的命名容器是层次结构中该控件之上的父控件,此父控件实现 INamingContainer 接口。实现此接口的服务器控件为其子服务器控件的 ID 属性值创建唯一的命名空间。

当针对列表 Web 服务器控件(如 Repeater 和 DataList 服务器控件)进行数据绑定时,为服务器控件创建唯一的命名空间尤其重要。当数据源中的多个项创建服务器控件的多个实例,且该服务器控件是重复控件的子级时,命名容器确保这些子控件的每个实例具有不冲突的 UniqueID 属性值。页的默认命名容器是请求该页时生成的 Page 类的实例。

可以使用此属性确定特定服务器控件所在的命名容器。

 

【方法六】如果模板列中有CheckBox控件的情况,通过CheckBox1_CheckedChanged事件中,获取GridView中的当前行。

CheckBox chk = (CheckBox)sender;

DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;

GridViewRow gvr = (GridViewRow)dcf.Parent;

 posted on 2013-02-04 14:22  纳米程序员  阅读(1940)  评论(0编辑  收藏  举报