在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的默认值

 

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值