ASP.NET中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);
  }

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

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

(0)

相关推荐

  • ASP.NET控件之RadioButtonList详解

    "RadioButtonList"控件表示一个封装了一组单选按钮控件的列表控件. 可以使用两种类型的 ASP.NET 控件将单选按钮添加到网页上:各个"RadioButton"控件或一个"RadioButtonList"控件.这两类控件都允许用户从一小组互相排斥的预定义选项中进行选择.使用这些控件,可定义任意数目的带标签的单选按钮,并将它们水平或垂直排列. 命名空间:System.Web.UI.WebControls 程序集:System.Web

  • ASP.NET jQuery 实例16 通过控件CustomValidator验证RadioButtonList

    界面代码: 复制代码 代码如下: <form id="form1" runat="server"> <div align="center"> <fieldset style="width: 350px; height: 200px;"> <table border="0" cellpadding="3" cellspacing="3&q

  • JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结

    一: DropDownList ------------------------------------------------------------------------------------------- 在使用 JQuery 进行遍历操作时, $("input").each(function(i) { ...... } 当操作对象的类型为 dropdownlist时:(备注:在firefox下DropDownList的类型为"select-one") 获

  • jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值

    -.获取dropdownlist的text(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList option:selected").text() 二.获取dropdownlist的value(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList").val() 三.获取radiobuttonlist的t

  • js获取RadioButtonList的Value/Text及选中值等信息实现代码

    HTML代码 复制代码 代码如下: <asp:RadioButtonList ID="rbtnCompany" runat="server" RepeatColumns="4" RepeatDirection="horizontal"> <asp:ListItem Value="1" Text="A"></</SPAN>asp:ListItem&

  • RadioButtonList绑定图片及泛型Dictionary应用

    本博文是让你学会读取站点某一目录的图片,掌握LINQ与泛型Dictionary<TKey,TValue>的使用. 首先准备好几张图片存在站点某一目录之下,本例中的存储图片的目录名为MsSiteImages,图片你可以从微软网站下载http://windows.microsoft.com/en-US/windows/home 我们写一个泛型数据集,将存储目录的图片信息: 复制代码 代码如下: View Code private Dictionary<int, string> GetD

  • 如何为CheckBoxList和RadioButtonList添加滚动条

    如何给CheckBoxList和RadioButtonList添加滚动条? 继承基类CheckBoxList和RadioButtonList,添加滚动属性,重写Render方法即可. 属性列表: #region 滚动控制 private bool _ShowScrollBar = false; /// <summary> /// 显示滚动条 /// </summary> [ System.ComponentModel.Description("是否显示显示滚动条"

  • ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法

    这三个控件都有一个Items集合,可以用 RepeatLayout 和 RepeatDirection 属性来控制列表的呈现形式.如果 RepeatLayout 的值为 Table,那么将在表中呈现列表.如果设置成 Flow,那么将在没有任何表结构的情况下呈现列表.默认情况下,RepeatDirection 的值为 Vertical.将此属性设置成 Horizontal 将会使列表水平呈现. RadioButtonList:控件提供已选中一个选项的单项选择列表(数据源单选).与其他列表控件相似,

  • jquery判断RadioButtonList和RadioButton中是否有选中项示例

    复制代码 代码如下: <pre class="html" name="code"> <%--Body 代码--%> <div> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Value="A">a</asp:ListItem> <a

  • javascript判断是否有对RadioButtonList选项选择

    写Javascript来判断是否有对RadioButtonList选项选择,效果如下: 准备好RadioButtonList数据源: Cosmetic.vb 复制代码 代码如下: Imports Microsoft.VisualBasic Namespace Insus.NET Public Class Cosmetic Private _ID As Integer Private _Type As String Private _Name As String Private _Weight A

随机推荐