asp.net Repeater如何按照特定字段绑定特定的内容
|
admin
2017年11月17日 0:14
本文热度 6250
|
一直觉得repeater不灵活,但是其实如果用后台拼接html内容的话,结合object类型,其实可以变化出多变的内容。这里有个需求需要根据不同的基金经理来绑定变动后的基金经理内容,每个基金经理还有特定的ID用来拼接处html超链接来跳转,困难来了,如果知道不同的内容的基金经理,是一个,二个或者多个呢,多个自然可以用逗号分隔,但是保存出来如何让repeater绑定呢!?这里我采用了eval来实现。具体来看代码:
首先要在实体类中定义一个标记字段和两个用来存放基金经理名的string数组或者仅仅是一个基金经理,那用object对象最好不过,对于前段和中间都隐藏其具体含义,最后强绑定转换就行,当然是根据标记字段来绑定。
- public class FundSubscriptionPeriodTip
- {
-
-
-
- public string NOTICEDATE { get; set; }
-
-
-
-
- public string FCODE { get; set; }
-
-
-
-
- public string SHORTNAME { get; set; }
-
-
-
-
- public string ISSBDATE { get; set; }
-
-
-
-
- public string ISSEDATE { get; set; }
-
-
-
-
- public string CHECKTIME { get; set; }
-
-
-
-
- public string CLOSETIME { get; set; }
-
-
-
-
-
-
- public string JJJLID { get; set; }
-
-
-
-
- public string JJJL { get; set; }
-
-
-
-
-
- public string ISSEDATEISSBDATE { get; set; }
-
-
-
-
- public object JJJLArray { get; set; }
-
-
-
-
-
-
-
- public object JJJLIDArray { get; set; }
-
-
-
-
- public string FundMangerNumber { get; set; }
-
- }
数据访问层这边就根据不同的基金经理的数量来分隔或者直接赋值:- #region 基金认购期
- public static ResultInfo<List<FundSubscriptionPeriodTip>> GetFundSubscriptionPeriodTip()
- {
- ResultInfo<List<FundSubscriptionPeriodTip>> ret = new ResultInfo<List<FundSubscriptionPeriodTip>>();
-
- try
- {
- using (OracleConnection Conn = new OracleConnection(conn))
- {
- string sql;
- OracleCommand Cmd;
-
-
- sql = " select noticedate, " +
- " fcode," +
- " shortname, " +
- " ISSBDATE, " +
- " ISSEDATE, " +
- "''7个工作日'' checktime,"+
- "''一般3个月内'' closetime,"+
- " jjjlid," +
- " jjjl " +
- " from fundadmin.fund_jbxx " +
- " where eisdel=0 and noticedate=to_date(sysdate)";
-
-
-
-
-
-
- Cmd = new OracleCommand(sql, Conn);
-
-
-
- Conn.Open();
- OracleDataAdapter oda = new OracleDataAdapter(Cmd);
- DataTable dtb = new DataTable();
- oda.Fill(dtb);
- List<FundSubscriptionPeriodTip> miLst = new List<FundSubscriptionPeriodTip>();
- foreach (DataRow row in dtb.Rows)
- {
- FundSubscriptionPeriodTip entity = new FundSubscriptionPeriodTip();
-
- entity.NOTICEDATE = Convert.ToDateTime((row["NOTICEDATE"])).ToShortDateString();
-
- entity.SHORTNAME = FormatTools.ParseString(row["SHORTNAME"]);
- entity.FCODE = FormatTools.ParseString(row["FCODE"]);
- entity.ISSBDATE = Convert.ToDateTime(row["ISSBDATE"]).ToShortDateString();
- entity.ISSEDATE = Convert.ToDateTime((row["ISSEDATE"])).ToShortDateString();
- entity.JJJLID=FormatTools.ParseString(row["JJJLID"]);
- entity.JJJL=FormatTools.ParseString(row["JJJL"]);
- entity.CHECKTIME = FormatTools.ParseString(row["CHECKTIME"]);
- entity.CLOSETIME = FormatTools.ParseString(row["CLOSETIME"]);
-
-
- <span style="color:#ff0000;"> if (!string.IsNullOrWhiteSpace(entity.JJJLID))
- if (entity.JJJLID.Contains(","))
- {
- entity.JJJLIDArray = entity.JJJLID.Split('','');
- entity.FundMangerNumber = "1";
- }
- else
- {
- entity.JJJLIDArray = entity.JJJLID.ToString();
- entity.FundMangerNumber = "0";
- }
- if (!string.IsNullOrWhiteSpace(entity.JJJL))
- if (entity.JJJL.Contains(","))
- entity.JJJLArray = entity.JJJL.Split('','');
-
- else
- entity.JJJLArray = entity.JJJL.ToString();
- an>
- miLst.Add(entity);
- }
-
- ret.SetValue(miLst);
- if (miLst.Count == 0)
- {
- ret.Message = "未查询到任何记录信息!";
- ret.SetValue(new List<FundSubscriptionPeriodTip>());
- }
- Conn.Close();
- }
- }
- catch (Exception e)
- {
- if (e != null)
- ret.Message += e.Message;
- }
-
- return ret;
-
- }
- #endregion
上面红色部分是关键,后面是用前段来调用:- <tr>
- <td class="w200"> 基金经理姓名 </td>
- <td class="pr200">
-
- <%-- <a href="http://fund.eastmoney.com/manager/<%#Eval("JJJLIDArray[0]")%>.html" target="_blank"> <%#Eval("JJJLArray[0]")%></a>,<a href="http://fund.eastmoney.com/manager/<%#Eval("JJJLIDArray[1]")%>.html" target="_blank"> <%#Eval("JJJLArray[1]")%></a>--%>
- <%#IsFundMangerNumber(Eval("FundMangerNumber").ToString(),Eval("JJJLIDArray"),Eval("JJJLArray"))%>
-
-
- </td>
- </tr>
后台代码就实际来拼接html内容:
- public string IsFundMangerNumber(string FundMangerNumber, object JJJLIDArray, object JJJLArray)
- {
- string Show="";
- if (FundMangerNumber == "1")
- {
- for (int i = 0; i < ((string[])JJJLArray).Count(); i++)
- {
- Show = Show+" <a href=\"http://fund.eastmoney.com/manager/" + ((string[])JJJLIDArray)[i] + ".html\" target=\"_blank\">" + ((string[])JJJLArray)[i] + "</a>";
- if (i < (((string[])JJJLArray).Count()-1))
- Show = Show + ",";
- }
- }
- else
- {
- Show = Show + "<a href=\"http://fund.eastmoney.com/manager/" + JJJLIDArray + ".html\" target=\"_blank\">" + JJJLArray + "</a>";
-
- }
- return Show;
- }
该文章在 2017/11/17 0:14:43 编辑过