zoukankan      html  css  js  c++  java
  • 关于dropdwonlist无法获得值问题

    问题描述:

    用dropdwonlist绑定了一个数据源做选择,但是当提交时,用控件属性无法获得相应的值,打印出来每次都是显示的第一个值。

    前端控件:
    <label>发布栏目:<asp:DropDownList ID="sectionDropDownList" runat="server"></asp:DropDownList></label


    数据绑定:
    SourceDb DropDwonListData
    = new SourceDb();
    string DropDwonSelect = "SELECT * FROM [Section]"
    ;
    sectionDropDownList.DataSource
    = DropDwonListData.DatasetDb(DropDwonSelect).Tables[0
    ].DefaultView;
    sectionDropDownList.DataTextField
    = "name"
    ;
    sectionDropDownList.DataValueField
    = "code"
    ;
    sectionDropDownList.DataBind();


    Button事件:
    string newsTitle = sectionDropDownList.SelectedValue;
    Response.Write(newsTitle);

    问题分析:

    因为在page_load中每次都绑定了数据源,而去调用Button事件时,实际是每次都刷新了页面的,于是每次在打印出来前都是初始化的值,于是每次都是输出的的一个值。

    问题解决:

    判断是否是页面回调。

    前端控件:
    <label>发布栏目:<asp:DropDownList ID="sectionDropDownList" runat="server"></asp:DropDownList></label



    数据绑定:

    if(!IsPostBack){
      SourceDb DropDwonListData

    = new SourceDb();
      string DropDwonSelect = "SELECT * FROM [Section]"
    ;
      sectionDropDownList.DataSource
    = DropDwonListData.DatasetDb(DropDwonSelect).Tables[0
    ].DefaultView;
      sectionDropDownList.DataTextField
    = "name"
    ;
      sectionDropDownList.DataValueField
    = "code"
    ;
      sectionDropDownList.DataBind();

    }

    
    


    Button事件:
    string newsTitle = sectionDropDownList.SelectedValue;
    Response.Write(newsTitle);

    知识共享许可协议
    作品Tim Zhang创作,采用知识共享署名 3.0 中国大陆许可协议进行许可。 。
  • 相关阅读:
    使用nginx搭建https服务器
    CentOS6.*安装gitolite
    Nginx 下配置SSL证书的方法
    Nginx Location配置总结
    最优二叉树(哈夫曼树)知识点
    utf8字节
    utf8字节
    nginx 配置日志
    nginx 配置日志
    elk 索引
  • 原文地址:https://www.cnblogs.com/ccdc/p/2115374.html
Copyright © 2011-2022 走看看