在DataGrid中使用DropDownList(原创)

  B/S架构的MIS中,在DataGrid中使用DropDownList是很常见的事情
Html代码如下:
< asp:TemplateColumn HeaderText = " 细目 " >

       
< HeaderStyle Width = " 15% " ></ HeaderStyle >

              
< ItemTemplate >

                     
< asp:Label Runat = server Text = ' <%#DataBinder.Eval(Container, "DataItem.TicketType")%> ' ></ asp:Label >

              
</ ItemTemplate >

       
< EditItemTemplate >

                     
< asp:DropDownList id = " ddl_Detail "  runat = " server "  Width = " 100% " >

                            
< asp:ListItem Value = " Train " > 火车票 </ asp:ListItem >                                                   < asp:ListItem Value = " Air " > 机票 </ asp:ListItem >

                            
< asp:ListItem Value = " Taxi " > 计程车费 </ asp:ListItem >

                            
< asp:ListItem Value = " Bus " > 公共汽车费 </ asp:ListItem >

                            
< asp:ListItem Value = " Book " > 订票费 </ asp:ListItem >

                            
< asp:ListItem Value = " Other " > 其它 </ asp:ListItem >

                     
</ asp:DropDownList >

                     
< asp:Label Runat = server ID = lbl_Detail Text = ' <%#DataBinder.Eval(Container, "DataItem.TicketType")%> '  Visible = False ></ asp:Label >

       
</ EditItemTemplate >

</ asp:TemplateColumn >

       
< asp:TemplateColumn HeaderText = " 来程/去程 " >

         
< HeaderStyle Width = " 15% " ></ HeaderStyle >

           
< ItemTemplate >

                
< asp:Label Runat = server Text = ' <%#DataBinder.Eval(Container, "DataItem.ComeOrBack")%> ' ></ asp:Label >

              
</ ItemTemplate >

       
< EditItemTemplate >

              
< asp:DropDownList id = " ddl_ComeOrBack "  runat = " server "  Width = " 100% " >

                     
< asp:ListItem Value = " Come " > 来程 </ asp:ListItem >

                     
< asp:ListItem Value = " Back " > 去程 </ asp:ListItem >

              
</ asp:DropDownList >

              
< asp:Label Runat = server ID = " lbl_ComeOrBack "  Text = ' <%#DataBinder.Eval(Container, "DataItem.ComeOrBack")%> '  Visible = False ></ asp:Label >

       
</ EditItemTemplate >

</ asp:TemplateColumn >

这个DataGrid的第一个Column是编辑列,DropDownList放在EditItemTemplate里,点击“修改”,问题出现了,修改前的细目是“计程车票”,点击“编辑”后,如何使ddlindex还保持在继承车票上呢?C#代码如下:
private   void  dg_Detail_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         
{

              
if( e.Item.ItemType == ListItemType.EditItem )//判断是否是EditItemTemplate

              
{

                   DropDownList ddl_Detail 
= (DropDownList)e.Item.FindControl("ddl_Detail");

                   DropDownList ddl_ComeOrBack 
= (DropDownList)e.Item.FindControl("ddl_ComeOrBack");

                   Label lbl_Detail 
= (Label)e.Item.FindControl("lbl_Detail");

                   Label lbl_ComeOrBack 
= (Label)e.Item.FindControl("lbl_ComeOrBack");

 

                   
string str1 = lbl_Detail.Text;

                   
string str2 = lbl_ComeOrBack.Text;

                   ddl_Detail.SelectedIndex = ddl_Detail.Items.IndexOf( ddl_Detail.Items.FindByText(str1) );               ddl_ComeOrBack.SelectedIndex = ddl_ComeOrBack.Items.IndexOf( ddl_ComeOrBack.Items.FindByText(str2) );//这里的代码就是为了设置ddl的索引

              }
        

         }




 OK!

你可能感兴趣的:(datagrid)