Multiple DropDownLists Inside the GridView Item Template Field
2010-12-30 16:49
232 查看
Position Reset) >> Multiple DropDownLists Inside the GridView Item Template FieldFriday, September 15, 2006 2:02 PM
Sometimes you have two DropDownLists inside the GridView Item Tempalte Field. This scenario is encountered when the DropDownList have a parent-child relationship. So, you want to select some item from the parent and fill the child list based on the parent selection. If GridView control was not involved in this triangle then it would have been pretty simple but GridView makes it little different.
The first thing that you need to do is to put the AutoPostBack on the parent DropDownList to true. Now, you can implement the SelectedIndex_Changed event of the DropDownList (Parent).
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
string foundID = String.Empty;
GridViewRow row = (GridViewRow)((sender) as Control).NamingContainer;
// get the value from the first dropdownlist
DropDownList ddlFirst = ((sender) as DropDownList);
int ddlFirstSelectedValue = Convert.ToInt32(ddlFirst.SelectedValue);
string firstDropDownListID = ddlFirst.UniqueID;
// and now here is how
DropDownList ddlSecond = row.FindControl("ddl2") as DropDownList;
// now from here you can do whatever you want!
}
Nothing too fancy about the above code. I just used the NamingContainer to find the selected row in which the user has changed the DropDownList (Parent) selection. Then I get the value from the parent DropDownList and populate the child DropDownList by simply referering it by its ID.
This is it!
Sometimes you have two DropDownLists inside the GridView Item Tempalte Field. This scenario is encountered when the DropDownList have a parent-child relationship. So, you want to select some item from the parent and fill the child list based on the parent selection. If GridView control was not involved in this triangle then it would have been pretty simple but GridView makes it little different.
The first thing that you need to do is to put the AutoPostBack on the parent DropDownList to true. Now, you can implement the SelectedIndex_Changed event of the DropDownList (Parent).
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
string foundID = String.Empty;
GridViewRow row = (GridViewRow)((sender) as Control).NamingContainer;
// get the value from the first dropdownlist
DropDownList ddlFirst = ((sender) as DropDownList);
int ddlFirstSelectedValue = Convert.ToInt32(ddlFirst.SelectedValue);
string firstDropDownListID = ddlFirst.UniqueID;
// and now here is how
DropDownList ddlSecond = row.FindControl("ddl2") as DropDownList;
// now from here you can do whatever you want!
}
Nothing too fancy about the above code. I just used the NamingContainer to find the selected row in which the user has changed the DropDownList (Parent) selection. Then I get the value from the parent DropDownList and populate the child DropDownList by simply referering it by its ID.
This is it!
相关文章推荐
- Selecting multiple checkboxes inside a GridView control
- GridView<asp:TemplateField>合并信息相同的行
- JPA Multiple writable mappings exist for the field [{0}]. Only one may be defined as writable, all o
- gridview /TemplateField /RowCommand/ 当前行index /Insert/update/delete
- 获取GridView的TemplateField模版中某列值
- GridView中出现“TemplateField 上不支持回调,因为某些控件不能在回调中正确更新。”的解决方法
- JavaScript访问GridView的绑定列(Access the Bound field of Gridview by javascript)
- 获取GRIDVIEW中的TemplateField显示的文本值
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- jQuery找到GridView控件ItemTemplate模版内的控件
- How to get the control inside the ControlTemplate/DataTemplate on run time.
- This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on GridView
- GridView1_RowDataBound中能不能得到ItemTemplate中Label控件绑定的Text值??
- the DataFormatString not work for the GridView BoundField problem
- GridView自定义TemplateField————把同一行中的内容分成多行显示///////若行数太多,则分行显示
- Selecting multiple checkboxes inside a GridView control - From CodeProject.com
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- GridView控件 单击TemplateField中的Button控件时,获取该行的行号(行索引)
- 获取GridView TemplateField的数据
- 将 TemplateField 字段列动态添加到 GridView 控件