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;
}
}
}
}
}

    

  

  

 
 
 
标签: C#琐碎

DropDownList和GridView用法的更多相关文章

  1. Gridview用法大总结

    Gridview用法大总结啦!精彩效果截图加详细源代码注释,需要的朋友赶紧过来看看吧:走过路过,千万不要错过哦!     由于篇幅限制,代码就不贴啦,要下载源码的请点击这里:希望朋友们能给出一些好的建 ...

  2. GridView用法详解

    前台页面: Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  3. webForm中dropDownList的一些用法

    DropDownList 控件用于创建下拉列表. DropDownList 控件中的每个可选项都是由 ListItem 元素定义的! 该控件支持数据绑定! DropDownList1.DataSour ...

  4. Android GridView用法介绍

    GridView(网格视图)是按照行和列的方式来显示内容的,一般用于显示图片等内容,比如实现九宫格图: 第一个例子: 实现代码为: MainActivity.java package com.xiao ...

  5. GridView用法的修改和删除

    (前台) <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...

  6. GridView用法

    首先,gridview是封装好的,直接在设计界面使用,基本不需要写代码: 1.绑定数据源 GridView最好与LinQDatasourse配合使用,相匹配绑定数据: 2.外观控制 整体控制 自动选择 ...

  7. DropDownList按照Gridview获取数据获取到的是定义格式

    首先需要把DropDownList改成允许服务器返回. 然后绑定的时候需要以下两项. DropDownList1.DataTextField = "name";DropDownLi ...

  8. DropdownList异步刷新GridView数据

    前台代码: <div style=" clear:both; width:800px; text-align:center; margin-left:auto; margin-righ ...

  9. dropdownlist select的用法

    <tr>        <td></td>        <td>@Html.DropDownList("ddlSex",@Mode ...

随机推荐

  1. Linux系统下启动MySQL报错:Neither host &#39;localhost.localdomain&#39; nor &#39;localhost&#39; could be looked up with

    Linux系统下启动MySQL报错:Neither host 'localhost.localdomain' nor 'localhost' could be looked up with 摘要 Li ...

  2. krpano漫游加方向性3D声音(这篇文章已被移到krpano中国网站 krpano360.com)

    需求:      在场景转换视角时.会出现不同方位的声音以及对应的音量变化,也即是将声音视作hotspot.当视角转到该声音热点时,也随之听到声音.官方样例:点击打开链接 本文已经搬迁到下述网址.请点 ...

  3. C# 线程知识--使用Task执行异步操作

    在C#4.0之前需要执行一个复杂的异步操作时,只能使用CLR线程池技术来执行一个任务.线程池执行异步任务时,不知道任务何时完成,以及任务的在任务完成后不能获取到返回值.但是在C#4.0中引人了一个的任 ...

  4. AES加密CBC模式兼容互通四种编程语言平台【PHP、Javascript、Java、C#】

    原文:AES加密CBC模式兼容互通四种编程语言平台[PHP.Javascript.Java.C#] 由于本人小菜,开始对AES加密并不了解,在网络上花了比较多时间查阅资料整理: 先简单从百度找来介绍: ...

  5. android 实现真正意义上的服务及源代码下载

    android APP后台服务和长期server长期互动,保证实时数据,这个小项目主要实施app退出后仍然能够执行服务. 使用该系统的Intent.ACTION_TIME_TICK现,这个系统的广播每 ...

  6. PostgreSQL 9.3 Streaming Replication 状态监控

    postgresql是使用Streaming Replication来实现热备份的,热备份的作用如下: 灾难恢复 高可用性 负载均衡,当你使用Streaming Replication来实现热备份(h ...

  7. 2014鞍山直播比赛H称号HDU5077(DFS修剪+通过计)

    NAND Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Sub ...

  8. SVN & Git (二)

    Git:是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.Git 是 Linus T ...

  9. 电商指尖---(9).net发展Solr中间Facet特征

    上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计.这一节我们看看如何使用.NET开发Solr中的Facet功能.在讲Facet功能的同一时候, 我们看下.Net中如何使用Sol ...

  10. Ubuntu下的用户和权限(二)

    五.chown.chgrp命令 从名字就能够猜測他们是干嘛的,可是这两个命令须要root权限. chown命令的格式为:chown user:group file  中间的user : group三项 ...