在Gridview编辑时添加DropDownList控件并设置默认值

 

页面代码:
<asp:GridView ID="GridView1" runat="server" 
           AutoGenerateColumns="False" Width="650px" Font-Size="12px" ForeColor ="#43860c" 
           DataKeyNames="user_id" onrowcancelingedit="GridView1_RowCancelingEdit" 
           onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
           onrowupdating="GridView1_RowUpdating" ondatabound="GridView1_DataBound" 
           AllowPaging="True" onrowdatabound="GridView1_RowDataBound" 
          >
           <Columns>
               <asp:BoundField DataField="user_id" HeaderText="编号"ReadOnly="True" />
               <asp:BoundField DataField="user_name" HeaderText="姓名"/>
               <asp:BoundField DataField="contact" HeaderText="联系电话"/>
               <asp:BoundField DataField="company_name" HeaderText="公司名称"/>
               <asp:BoundField DataField="address" HeaderText="地址"/>
               <asp:BoundField DataField="custom_demand" HeaderText="客户需求"/>
               <asp:BoundField DataField="date" HeaderText="日期"ReadOnly="True" />
                <asp:TemplateField HeaderText="状态">
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%#showUserName(Eval("status"))%>'></asp:Label> 
                                </ItemTemplate>
                                <EditItemTemplate > 
                                    <asp:Label ID="Label2" runat="server" Text='<%#showUserName(Eval("status"))%>' Visible ="false"></asp:Label>                   
                                 <asp:DropDownList ID="DropDownList1" runat="server" Font-Size="12px" ForeColor="#43860c">                                   
                                  
                                             <asp:ListItem Value="0" Text ="有意向"></asp:ListItem>
                                             <asp:ListItem Value="1" Text ="已签约"></asp:ListItem>
                                             <asp:ListItem Value="2" Text ="已关闭"></asp:ListItem>
                                   </asp:DropDownList></EditItemTemplate>
                            </asp:TemplateField>
               <asp:BoundField DataField="remark" HeaderText="备注"/>
               <asp:CommandField ShowEditButton="True" />
               <asp:CommandField ShowDeleteButton="True" DeleteText="删除"/>
           </Columns>
                  <PagerTemplate>
        <br />
         <asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
         <asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" ForeColor="#43860c" Font-Size="12px"
                    Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' 
                    CommandName="Page" CommandArgument="First" Font-Underline="False" ></asp:LinkButton>
       &nbsp <asp:LinkButton ID="lbnPrev" runat="server" Text="上一页"ForeColor="#43860c" Font-Size="12px"
                    Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' 
                    CommandName="Page" CommandArgument="Prev" Font-Underline="False" ></asp:LinkButton>
        &nbsp <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页"ForeColor="#43860c" Font-Size="12px"
                    Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' 
                    CommandName="Page" CommandArgument="Next" Font-Underline="False" ></asp:LinkButton>
        &nbsp <asp:LinkButton ID="lbnLast" runat="Server" Text="尾页"ForeColor="#43860c" Font-Size="12px" 
                    Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' 
                    CommandName="Page" CommandArgument="Last" Font-Underline="False" ></asp:LinkButton>
                     &nbsp;&nbsp; 到第<asp:TextBox runat="server" ID="inPageNum" Height="19px" Width="74px"></asp:TextBox>页 
         <asp:Button ID="Button1" CommandName="go" runat="server" Text="go" Width="50px" />
         <br />
     </PagerTemplate> 
                
           </asp:GridView>
 
C#代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (((DropDownList)e.Row.FindControl("DropDownList1")) != null)//判断有没有DropDownList
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
                  string lbl = ((Label)e.Row.FindControl("Label2")).Text;//获Label2的值
            ddl.Items.FindByText(lbl).Selected = true;//设置ddl的默认值
 
        }
    }
原文地址:https://www.cnblogs.com/lxboy2009/p/3769901.html