gridview 中dropdownlist 默认值设定

来源:互联网 发布:手机网络时钟不同步 编辑:程序博客网 时间:2024/05/13 13:15

这几天一直在研究gridview的用法,我会陆续将学习心得写出,希望与大家共同讨论。

 1 

图一

图二

点击“编辑”链接,出现如图二所示,dropdownlist与所在行的值一些致。其中dropdownlist的默认值设定方法颇费一番周折。
网上找了很多资料,但都不能很好地解决,综合网上的一些资料我得出了以下成果:

aspx页面部分代码:

<asp:GridView ID="GridView1" runat="server" Width="100%" style="font-size: 12px; color: black; background-color: white; font-family: 宋体;" AllowSorting="True"  BackColor="Red" BorderColor="Transparent" BorderStyle="Solid" BorderWidth="1px" CaptionAlign="Bottom" CellPadding="2" Font-Size="12pt" Font-Strikeout="False" Font-Underline="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCreated="GridView1_RowCreated" AllowPaging="True" CellSpacing="1" ForeColor="Red">
                        <RowStyle HorizontalAlign="Center" Font-Size="13pt" BackColor="Bisque" ForeColor="Black" BorderColor="Black" BorderWidth="1px" VerticalAlign="Middle" />
                        <HeaderStyle BackColor="#990000" BorderColor="Transparent" BorderWidth="0px"
                            Font-Bold="True" Font-Size="14px" Height="20px" HorizontalAlign="Center" Wrap="False" ForeColor="#FFFFCC" />
                        <EditRowStyle Height="20px" />
                        <SelectedRowStyle Font-Size="14pt" Height="50px" BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                        <Columns>
                        <asp:TemplateField HeaderText="编号">
                        <ItemTemplate><%#this.GridView1.PageIndex * this.GridView1.PageSize+this.GridView1.Rows.Count+1%></ItemTemplate>
                        </asp:TemplateField>
                            <asp:TemplateField>
                           
                                <HeaderTemplate>
                                    <asp:CheckBox ID="CheckBox2" runat="server" Text="全选" TextAlign="Left" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                           
                            <asp:TemplateField HeaderText="资产序列号"  SortExpression="assetSN">
                           
                            <ItemTemplate><asp:Label ID="lblAssetSN" Text='<%#Bind("assetSN") %>' runat="server"></asp:Label></ItemTemplate>
                            </asp:TemplateField>
                           <asp:TemplateField HeaderText="资产类别" SortExpression="assetClass">
                            <EditItemTemplate><asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("assetClass") %>' Visible="False"></asp:Label>&nbsp;
                            </EditItemTemplate>
                            <ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("assetClass") %>'></asp:Label></ItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="型号与配置" SortExpression="modAndCon">
                            <EditItemTemplate><asp:TextBox ID="txtModAndCon" runat="server" Text='<%#Bind("ModAndCon") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate><asp:Label ID="lblModAndCon" runat="server" Text='<%#Bind("ModAndCon") %>'></asp:Label></ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="12121"></asp:TemplateField>
                            <asp:TemplateField ShowHeader="False">
                                <EditItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                        Text="更新"></asp:LinkButton>&nbsp;
                                    <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancel">取消</asp:LinkButton>
                                </EditItemTemplate>
                                <HeaderTemplate>
                                    编辑
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                        Text="编辑" OnClick="LinkButton1_Click"></asp:LinkButton>
                                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                        Text="删除" OnClick="LinkButton2_Click" OnClientClick="return confirm('确定要删除吗?')"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataRowStyle Height="30px" VerticalAlign="Top" />
                        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Left" Font-Size="Larger" />
                        <PagerSettings PreviousPageText="上一页" />
                    </asp:GridView>

说明:其中大体字部分是主要代码,EditItemTemplate项中分别添加了dropDownList和 Label两个服务器控件, Label与数据库字段assetClass绑定,dropDownList在后台代码中绑定,这里不给出了。将 Label2的Visible设为False,为的是在显示数据时不出现。
后台代码如下:

    protected void Label GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (((DropDownList)e.Row.FindControl("ddl")) != null)//判断有没有DropDownList
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
            baseCla.getAssetClass(ddl);//通过自定义类将DropDownList绑定,自定义类这就不给出了,大家可以自己写。 
            string lbl = ((Label)e.Row.FindControl("Label2")).Text;//获Label2的值
            ddl.Items.FindByText(lbl).Selected = true;//设置ddl的默认值
        }