博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Gridview编辑时添加DropDownList控件并设置默认值
阅读量:5222 次
发布时间:2019-06-14

本文共 3563 字,大约阅读时间需要 11 分钟。

 

页面代码:
<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

你可能感兴趣的文章
用Linux远程挂载Windows上的共享文件夹.md
查看>>
洛谷 P4317 花神的数论题(组合数)
查看>>
【Python】学习笔记5-利用flask来mock接口
查看>>
vue
查看>>
MySQL存储过程和存储函数
查看>>
【bzoj 2208】[Jsoi2010]连通数(dfs||Tarjan算法+拓扑序+dp)
查看>>
iis 隐藏 banner
查看>>
leetcode[18]4Sum
查看>>
Java ThreadLocal的使用
查看>>
为什么数据库ID不能作为URL中的标识符
查看>>
Mybatis 3.3.0 Log4j配置
查看>>
JavaScript打开窗口与关闭页面操作大全
查看>>
java 接口参数
查看>>
DP:Skiing(POJ 1088)
查看>>
kudu
查看>>
如何得到WAV文件播放的总时间
查看>>
移动端页面兼容性问题解决方案整理(三)
查看>>
c语言以二进制的方式向文件读写一组数据
查看>>
Spring定时器,定时执行(quartz)
查看>>
ASCII码表
查看>>