欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中RadioButtonList绑定后台数据后触发点击事件

程序员文章站 2023-12-13 12:07:46
本文实例为大家分享了radiobuttonlist绑定后台数据,触发点击事件的方法 首先前台页面放置一个radiobuttonlist 控件

本文实例为大家分享了radiobuttonlist绑定后台数据,触发点击事件的方法

首先前台页面放置一个radiobuttonlist 控件

<asp:radiobuttonlist runat="server" id="radiobuttonlist1" borderstyle="none" repeatcolumns="3" cssclass=""
      repeatlayout="flow" autopostback="true" onselectedindexchanged="radiobuttonlist1_selectedindexchanged">
    </asp:radiobuttonlist>

.cs文件 后台绑定数据

namespace btapp
{
 public partial class technology : system.web.ui.page
 {
  string id;
  protected void page_load(object sender, eventargs e)
  {
   if (!ispostback)
   {
    aspnetpager1.pagesize = 10;
    if (request.querystring["id"] != null)
    {
     id = request.querystring["id"];
    }
    else
    { id = ""; }
    getdatabind(id);
    dropdownlistdatabind();
   }
  }
  //radiobuttonlist绑定后台数据
  private void dropdownlistdatabind()
  {
   expertinfobll bll = new expertinfobll();
   datatable dt = bll.getdepinfo();
   foreach (datarow dr in dt.rows)
   {
    radiobuttonlist1.items.add(dr["name"].tostring());//循环读出数据库的数据
    
   }
   this.radiobuttonlist1.datasource = dt;
   this.radiobuttonlist1.datatextfield = "name";
   this.radiobuttonlist1.datavaluefield = "id";
   this.radiobuttonlist1.repeatdirection = repeatdirection.horizontal;
   this.radiobuttonlist1.databind();
  
  }
  private void getdatabind(string id)
  {
   //这里写解码和数据库返回结果
   technologybll bll = new technologybll();
   string strwhere = " 1=1 ";
   if (id != "" && id != null)
   {
    strwhere += string.format(" and a.depinfo_id = '{0}'", id);
   }
   aspnetpager1.recordcount = bll.getcountlist(strwhere);
   //绑定数据 
   datatable dt = bll.getlist((aspnetpager1.currentpageindex - 1) * aspnetpager1.pagesize, aspnetpager1.pagesize, strwhere, "createtime");
   this.repeater1.datasource = dt;
   this.repeater1.databind();


  }
  protected void aspnetpager1_pagechanged(object sender, eventargs e)
  {
   getdatabind(id);
  }

//根据选择单选按钮的不同id,触发事件
  protected void radiobuttonlist1_selectedindexchanged(object sender, eventargs e)
  {
    string id;
    id = radiobuttonlist1.selectedvalue;
    getdatabind(id);
  }
  
 }
}

technologybll 层的方法

namespace btappbll
{
 public class technologybll
 {
  technologydal dal = new technologydal();
  public datatable getlist(int startpage, int pagesize, string where, string orderby)
  {


   datatable dtable = dal.getlist(startpage, pagesize, where, orderby);
   return dtable;
  }
  public int getcountlist(string where)
  {


   int record = dal.getcountlist(where);
   return record;
  }
  public datatable getlistshow(string technologyid)
  {
   datatable dtable = dal.getmodel(technologyid);
   return dtable;
  }
  public datatable getpicture(string technologyid)
  {
   datatable dtable = dal.getpicture(technologyid);
   return dtable;
  }
 }
}

technologydal层的方法

namespace btappdal
{
 public class technologydal
 {
  public datatable getlist(int startpage, int pagesize, string where, string orderby)
  {
   string strsql = string.format("select a.technologyid,a.technologyname,a.summarize,a.effect,a.mainpoint,a.appropriatearea,a.attention,a.createtime,a.creatuser,a.updatetime,b.name from technology as a \n" +
    "left join sys_depinfo as b on a.depinfo_id=b.id \n" +
    "where a.isactive='1' and {0} ", where);


   string proc = "proc_commonpagerwithstatement";
   sqlconnection con = sqldbhelper.connection;
   sqlparameter[] sp = { new sqlparameter("@intstartindex", startpage), 
         new sqlparameter("@intpagesize", pagesize),
         new sqlparameter("@varstatement", strsql), 
         new sqlparameter("@varsortexpression", orderby+" desc") };
   datatable dt = sqldbhelper.getdataset(proc, sp, con);
   return dt;


  }
  public int getcountlist(string where)
  {
   int countrecord = 0;
   string strsql = string.format("select count(technologyid) as countrecord from(select a.technologyid,a.technologyname,a.summarize,a.effect,a.mainpoint,a.appropriatearea,a.attention,a.createtime,a.creatuser,a.updatetime,b.name from technology as a \n" +
    "left join sys_depinfo as b on a.depinfo_id=b.id \n" +
    "where a.isactive='1' and {0} ) as c", where);
   sqlconnection con = sqldbhelper.connection;
   try
   {
    if (con.state == system.data.connectionstate.closed)
     con.open();
    datatable dt = sqldbhelper.getdatatable(strsql);
    if (dt.rows.count > 0)
     countrecord = int.parse(dt.rows[0]["countrecord"].tostring());
   }
   catch (exception)
   {
    throw;
   }
   finally
   {
    if (con.state == connectionstate.open)
    {
     con.close();
    }
   }


   return countrecord;
  }
  public datatable getmodel(string technologyid)
  {
   string strsql = string.format("select a.technologyid,a.technologyname,a.summarize,a.effect,a.mainpoint,a.appropriatearea,a.attention,a.createtime,a.creatuser,a.updatetime,b.name from technology as a \n" +
    "left join sys_depinfo as b on a.depinfo_id=b.id \n" +
    "where a.isactive='1' and a.technologyid = '{0}' ", technologyid);


   datatable datatable = sqldbhelper.getdatatable(strsql);
   return datatable;
  }
  public datatable getpicture(string technologyid)
  {
   string strsql = string.format("select top 5 a.files_id,a.files_name,a.files_path from dbo.com_files as a \n" +
    "left join dbo.technology as b on a.foreignkey_id=b.technologyid \n" +
    "where b.isactive=1 and a.foreignkey_id = '{0}' ", technologyid);


   datatable datatable = sqldbhelper.getdatatable(strsql);
   return datatable;
  }
 }
}

expertinfobll 层的方法

 public datatable getdepinfo()
  {
   datatable dtable = dal.getdepinfo();
   return dtable;
  }

expertinfodal层的方法

 public datatable getdepinfo()
  {
   try
   {
    stringbuilder str = new stringbuilder(@"select id,name from dbo.sys_depinfo where is_active='1' and depinfotype='1'");
    datatable data = sqldbhelper.getdatatable(str.tostring());
    if (data.rows.count > 0)
    {
     return data;
    }
    else
    {
     return null;
    }
   }
   catch (exception)
   {
    return null;
   }
  }

在页面加载的时候调用dropdownlistdatabind()方法
触发radiobuttonlist的点击事件

 protected void radiobuttonlist1_selectedindexchanged(object sender, eventargs e)
  {
    string id;
    id = radiobuttonlist1.selectedvalue;
    getdatabind(id);
  }

既可以实现点击某个单选按钮,并触发事件。

以上就是本文的全部内容,希望对大家的学习有所帮助。

上一篇:

下一篇: