gridview 中dropdownlist 默认值设定
来源:互联网 发布:手机网络时钟不同步 编辑:程序博客网 时间:2024/05/13 13:15
图一
图二
点击“编辑”链接,出现如图二所示,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>
</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>
<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的默认值
}
- gridview 中dropdownlist 默认值设定
- 设定DropDownList的默认值
- DropDownList默认值的设定
- C#中DropDownList显示默认值
- 在gridview编辑中添加dropdownlist控件并使其为数据库的默认值
- Gridview中嵌入DropDownList
- GridView 中绑定 DropDownList
- GridView中DropDownList联动
- GridView中DropDownList更新
- GridView中DropDownList绑定
- DropDownList默认值
- 将DropDownList放入GridView中
- gridview中dropdownlist的赋值
- GridView中DropDownList的事件
- GridView中编辑加入DropDownLIst
- GridView中DropDownList的应用
- sybase中为字段设定默认值
- 设定下拉列表控件DropDownList,复选按钮CheckBox,RadioButton默认值总结测试
- 关于8086汇编栈空栈满的问题
- 在Linux的控制台中使用GDB调试程序
- [英语阅读]Relish solitude
- sql中用什么替代in
- 强类型和弱类型
- gridview 中dropdownlist 默认值设定
- 名字改编(name mangling)、调用约定与对策
- C++中Name-mangling
- 一个 mysql 行 转 列 的存储过程
- 企业服务总线ESB
- [c/c++]构造函数、析构函数中可不可以抛出异常
- HOWTO-OCSP-RESPONDER (水平有限,仅供参考,进行中.....)
- JSF文件上传功能
- 802.11标准