DropDownList和GridView用法
DropDownList和GridView用法
DropDownList控件和GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔的,查找、使用、记录,仅此而已。
DropDownList常规用法:
1、DropDownList绑定简单数据源
此处暂且写一个简单的数据源,只是为了说明效果。

private void BindDropDownUp()
{
ArrayList al = new ArrayList();
al.Add("11");
al.Add("22");
al.Add("33"); this.DropDownList1.DataSource = al;
this.DropDownList1.DataBind();
}

获取DropDownList中选择的值:string text = this.DropDownList1.SelectedItem.Text;
2、DropDownList绑定较为复杂数据源
此处从数据库中提取一个数据集ds,DropDownList控件的text框中显示一个值,选中后在后台可以获取绑定的value。具体如下:

private void BindDropDownUp()
{
string strSql = "select * from [OSCE].[dbo].[QuestionType]";
DataSet ds = Query(strSql); this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "QT_Name";
this.DropDownList1.DataValueField = "QT_ID"; this.DropDownList1.DataBind();//将数据源绑定到类似( GridView) 控件
}

获取DropDownList控件text框的值:string text = this.DropDownList1.SelectedItem.Text;
获取DropDownList控件绑定的value值:string text2 = this.DropDownList1.SelectedValue;
3、在页面初始化时直接给DropDownList赋值
题外话:这个功能用的非常多,实现也很简单,但前提是你必须知道。找了好久才发现的。
ListItem li = DropDownList1.Items.FindByText("外科");//外科是想显现的值,前提是DataTextField中必须有
if (li != null)
{
int index = DropDownList1.Items.IndexOf(li);
DropDownList1.SelectedIndex = index;
}
GridView常规用法
1、gridview前台界面代码
gridview创建列最主要的有两种方式:
1)数据绑定,表示数据绑定控件中作为文本显示的字段。DataField ="AnswerNum",AnswerNum是数据源中的一个字段。举例说明:
<asp:BoundField DataField ="AnswerNum" >
<ItemStyle Width ="8%" HorizontalAlign ="Center" />
</asp:BoundField>
2)使用模板创建,举例说明:

<asp:TemplateField HeaderText ="查看">
<ItemTemplate >
<asp:LinkButton ID ="LinkButtonViewSOption" runat ="server" CommandName ="ViewSOption" CommandArgument ='<%# Bind("QO_ID") %>'>描</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width ="5%" HorizontalAlign ="Center" />
</asp:TemplateField>

ItemStyle是其模板样式,根据具体要求做出调整。
2、绑定数据源
this.gvQuestions.DataSource = ExamQuestionInfoList;
this.gvQuestions.DataBind();
this.gvQuestions.PageIndex = 0;
gvQuestions为GridView控件,ExamQuestionInfoList为数据源,gridview的数据源可以是DataTable或者是数据集DataSet。
3、停留在某一行变色

private void ChangeColor(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
{
ChangeColor(sender, e);
}

4、操作某一行
直接举例说明

protected void gvSubjectiveOption_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowSelected = Convert.ToInt32(e.CommandArgument);
questionOptionInfo = QuestionOptionBLL.GetModel(rowSelected); //查看
if (e.CommandName == "ViewSOption")
{
this.tbOptionStem.Text = questionOptionInfo.QO_Option;
this.tbCorrectAnswer.Text = questionOptionInfo.QO_SubjectAnswer;//主观题答案
this.tbCorrectAnswerExplain.Text = questionOptionInfo.QO_Explain; //选项附件
string optionAccessoryStr = questionOptionInfo.QO_Accessory;
string[] optionAccessoryArr = optionAccessoryStr.Split(',');
for (int i = 0; i < optionAccessoryArr.Length; i++)
{
OptionAccessoryList.Add(optionAccessoryArr[i]);
}
BindOptionAccessoryList();
} if (e.CommandName == "DeleteOption")
{
QuestionOptionBLL.Delete(rowSelected);
int EQ_ID = questionOptionInfo.EQ_ID;
BindSubjectiveOption(EQ_ID);//重新绑定主观题问题信息
}
}

e.CommandName对应前台界面的一些字段:

<asp:TemplateField HeaderText ="查看">
<ItemTemplate >
<asp:LinkButton ID ="LinkButtonViewSOption" runat ="server" CommandName ="ViewSOption" CommandArgument ='<%# Bind("QO_ID") %>'>描</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width ="5%" HorizontalAlign ="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText ="删除" >
<ItemTemplate >
<asp:ImageButton ID ="ImageButtonDelete2" runat ="server" BorderStyle ="None" CommandName ="DeleteOption" CommandArgument ='<%# Bind("QO_ID") %>' ImageUrl ="~/images/delete.gif" />
</ItemTemplate>
<ItemStyle Width ="5%" HorizontalAlign ="Center" />
</asp:TemplateField>

其中CommandName ="DeleteOption" CommandArgument ='<%# Bind("QO_ID") %>代表数据集中的某个字段。
5、添加Checkbox并且初始化台界面:

<asp:TemplateField >
<ItemTemplate >
<asp:LinkButton ID ="LinkButton1" runat ="server" CommandName ="selectCorrectAnswer" CommandArgument ='<%# Bind("QO_ID") %>'>
<asp:CheckBox ID ="cbCorrectAnswer" runat ="server" />
</asp:LinkButton>
</ItemTemplate>

后台逻辑:

/// <summary>
/// 初始化checkbox值
/// </summary>
/// <param name="gv">gridview控件</param>
/// <param name="dtSource">数据源</param>
/// <param name="cbName">checkbox控件名称</param>
/// <param name="cbValue">checkbox的值</param>
private void InitializeCheckBox(GridView gv, DataTable dtSource, string cbName, string cbValue)
{
int count = dtSource.Rows.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
CheckBox cb = gv.Rows[i].FindControl(cbName) as CheckBox; if (cb != null)
{
if (dtSource.Rows[i][cbValue].ToString() == "0")
{
cb.Checked = false;
}
else
{
cb.Checked = true;
}
}
}
}
}

DropDownList和GridView用法的更多相关文章
- Gridview用法大总结
Gridview用法大总结啦!精彩效果截图加详细源代码注释,需要的朋友赶紧过来看看吧:走过路过,千万不要错过哦! 由于篇幅限制,代码就不贴啦,要下载源码的请点击这里:希望朋友们能给出一些好的建 ...
- GridView用法详解
前台页面: Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile=&qu ...
- webForm中dropDownList的一些用法
DropDownList 控件用于创建下拉列表. DropDownList 控件中的每个可选项都是由 ListItem 元素定义的! 该控件支持数据绑定! DropDownList1.DataSour ...
- Android GridView用法介绍
GridView(网格视图)是按照行和列的方式来显示内容的,一般用于显示图片等内容,比如实现九宫格图: 第一个例子: 实现代码为: MainActivity.java package com.xiao ...
- GridView用法的修改和删除
(前台) <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...
- GridView用法
首先,gridview是封装好的,直接在设计界面使用,基本不需要写代码: 1.绑定数据源 GridView最好与LinQDatasourse配合使用,相匹配绑定数据: 2.外观控制 整体控制 自动选择 ...
- DropDownList按照Gridview获取数据获取到的是定义格式
首先需要把DropDownList改成允许服务器返回. 然后绑定的时候需要以下两项. DropDownList1.DataTextField = "name";DropDownLi ...
- DropdownList异步刷新GridView数据
前台代码: <div style=" clear:both; width:800px; text-align:center; margin-left:auto; margin-righ ...
- dropdownlist select的用法
<tr> <td></td> <td>@Html.DropDownList("ddlSex",@Mode ...
随机推荐
- hadoop 开始时间datanode一个错误 Problem connecting to server
刚刚配置hadoop,namenode常开,但datanode但保留了错误.但不启动: 2014-05-04 10:43:33,970 WARNorg.apache.hadoop.hdfs.serve ...
- CentOS安装KVM步骤虚拟机,绝对实用!
KVM(Kernel-based Virtual Machine)速记.这是rhel5.4最新的虚拟化技术启动,现在只支持红帽64位rhel5.4在执行KVM,硬件必须支持同一时间VT技术,网上找 ...
- 《UNIX级别编程环境》注意读出信号(2)
1.功能sigaction sigaction动与指定信号相关联的处理动作.其函数原型例如以下: #inlcude <signal.h> int sigaction(int signo,c ...
- nodejs中使用monk訪问mongodb
mongodb 安装mongodb 我认为还是用mannual install靠谱一点儿:http://docs.mongodb.org/manual/tutorial/install-mongodb ...
- SQL Server的备份
原文:SQL Server的备份 0.参考文献 1.恢复模式 SQL Server 备份和还原操作发生在数据库的恢复模式的上下文中. 恢复模式旨在控制事务日志维护. “恢复模式”是一种数据库属性,它控 ...
- cocos2d-x-2.2的SimpleAudioEngine::sharedEngine()->playEffect()计划中断bug
在该计划已经正常,但现在突然发iphone播放声音上就挂了.播放音乐是没有问题的. android没问题. xcode给定的位置,如下面的附图: 网上搜了一下,说是有全局断点造成的.于是command ...
- Android批量图片加载经典系列——使用二级缓存、异步网络负载形象
一.问题描写叙述 Android应用中常常涉及从网络中载入大量图片,为提升载入速度和效率,降低网络流量都会採用二级缓存和异步载入机制.所谓二级缓存就是通过先从内存中获取.再从文件里获取,最后才会訪问网 ...
- Linux命令学习篇0——原产地
昨天在用curl发送简单的HTTP请求做測试的时候发现自己每次使用的时候都是在网络上查看别人的演示样例才干想起来怎么用,这样效率太低了.尽管有网络依旧在,可是总感觉不是被自己掌握着,心里不踏实,回忆起 ...
- crawler_基础之_java.net.HttpURLConnection 访问网络资源
java访问网络资源 由底层到封装 为 scoket==> java.net.HttpURLConnection==>HttpClient 这次阐述先 java.net.HttpURL ...
- CSS3火焰文字特效制作教程
原文:CSS3火焰文字特效制作教程 用一句很俗气的话概括这两天的情况就是:“最近很忙”,虽然手头上有不少很酷的HTML5和CSS3资源,但确实没时间将它们的实现过程写成教程分享给大家.今天刚完成了一个 ...