ASP.NET中RadioButtonList綁定後台資料後觸發點擊事件_實用技巧

來源:互聯網
上載者:User

本文執行個體為大家分享了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);  }

既可以實現點擊某個選項按鈕,並觸發事件。

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.