DropDownList和GridView用法_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > DropDownList和GridView用法

DropDownList和GridView用法

 2014/6/3 22:40:04  lhb62232397  博客园  我要评论(0)
  • 摘要:DropDownList控件和GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔的,查找、使用、记录,仅此而已。DropDownList常规用法:1、DropDownList绑定简单数据源此处暂且写一个简单的数据源,只是为了说明效果。privatevoidBindDropDownUp(){ArrayListal=newArrayList();al.Add("11");al.Add("22");al.Add("33");this.DropDownList1
  • 标签:用法 view list

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

    

  

 

 

  

 

上一篇: ASP.NET MVC学前篇之请求流程 下一篇: 没有下一篇了!
发表评论
用户名: 匿名