.net 网站开发知识点二

.net 网站开发知识点一

介绍了Form验证,FCKediter控件的使用,以及分页控件的使用,这章将再介绍几点简单的知识点,掌握这些知识,就可以掌握一个简单的新闻发布网站。

一、新闻标题

这样的新闻预览标题可以用reapeter控件实现

 

 <asp:Repeater ID="Repeater1" runat="server">      
       <ItemTemplate >
       <table width="550" border="1" cellpadding="0"  style=" border:none">
       <tr>
       <td height="30px" style="border-left-width:0; border-top-width:0; border-right-width:0; border-bottom-width:0 " class="text"><%# Convert.ToDateTime(Eval("NewsTime")).ToShortDateString() %> </td>
       </tr>
       <tr>
       <td  style="border-left-width:0; border-top-width:0; border-right-width:0; border-bottom-width:0 " >
       <div style=" text-align:justify  ; font-size: 12pt; color: #306; font-family: Arial, Helvetica, sans-serif;font-style: italic; font-weight: bold; width:550px;overflow:hidden"> <%# Eval("NewsTitle") %> </td>
       </tr>
       
       <tr>
       <td  style="border-left-width:0; border-top-width:0; border-right-width:0; border-bottom-width:0 ; padding-top:10px" class="text">
       <div style=" text-align:justify  ; width:550px; overflow:hidden "> <%# getchar(Eval("NewsText").ToString(),340)%> </td>
       </tr>

       <tr>
       <td height="30px" style="border-left-width:0; border-top-width:0; border-right-width:0; border-bottom-color:#666;" ><br /><a href="NewsView.aspx?id=<%# Eval("id")%>" class="more">More</a></td>
       </tr>
       </table>
       </ItemTemplate>
 </asp:Repeater>

后台代码如下:

namespace WebAPP
{
    public partial class NewsReleases : System.Web.UI.Page
    {
        private NewsManage newsManage = new NewsManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
              Repeater1.DataSource = newsManage.GetList();
              Repeater1.DataBind();
            }
        }

        public string getchar(string str, int len)
        {
            str = Regex.Replace(str, @"<[^>]+>", "");
            if (str.Length > len)
                str = str.Substring(0, len);
            str += "…";
            return str;
        }  
    }
}

还有一种标题类型:



这种可以用datalist绑定,也可以用repeater绑定:

 <div class="productList">
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
             <ItemTemplate>
               <li>
                <table border="0">
                <tr><td valign="middle" ><img src="<%# Eval("imagePath") %>" width="65px" height="75px" style=" margin-right:5px;border:   1px   solid black;" /></td>
                <td valign="top" height="85px" width="125px" style="font-size:12px; word-spacing:3px; text-align:left"><a href="Media<%# Eval("fileType") %>.aspx?id=<%# Eval("id")%>" target="_blank" class="bottom" > <%# Eval("fileTitle") %></a>
                <div style=" width:125px"><%# Eval("fileMonth") %>  <%# Eval("fileYear") %></div>
                </td></tr>
                </table>          
              </li>  
         </ItemTemplate>
    <FooterTemplate>
    </ul>          
    </FooterTemplate>
        </asp:Repeater>
        </div>  


Reperter需要设置一定的CSS样式:

 .productList{
   border:none;
   margin:0px;
   width:630px;
   }
   
   .productList ul{
   margin:0px;
   padding:0px;
   list-style:none;
   
   }
   
   .productList li{  
   float:left; 
   padding-right:10px;
   width:200px;
   list-style:none;
   }


二、后台编辑模块

分页控件在上一章讲过,Gridview绑定数据也很简单,这里要讲一下操作这个模板项

代码如下:

 <asp:TemplateField HeaderText="操作">
            <ItemTemplate>
            <asp:Button ID="btnView" runat="server" Text="查看详情" ForeColor="#0063DC" CommandName="View" CommandArgument="<%# Container.DisplayIndex %>"   />          
            <asp:Button ID="btnCancel" runat="server" Text="删除" ForeColor="#0063DC" CommandName="Cancel" CommandArgument="<%# Container.DisplayIndex %>"  />
            </ItemTemplate>
                <ItemStyle Width="130px" ForeColor="#0063DC" />
            </asp:TemplateField>


<Container.DisplayIndex>可以确定行号

后台代码:

namespace WebAPP
{
    public partial class EditNews : System.Web.UI.Page
    {
        private NewsManage newsManage = new NewsManage();

        /// <summary>
        /// GridView绑定全部数据
        /// </summary>
        public void GridViewBind()
        {
            txtRowCount.Text = newsManage.ExecuteScalar().ToString();
            AspNetPager.RecordCount = newsManage.ExecuteScalar();
            int intStart = AspNetPager.PageSize * (AspNetPager.CurrentPageIndex - 1);
            int intNum = AspNetPager.PageSize;
            DataGridView.DataSource = newsManage.GetList(intStart, intNum);
            DataGridView.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    GridViewBind();
                }
            }
            catch (Exception ex)
            { throw ex; }
        }

        protected void DataGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            int id = Convert.ToInt32(DataGridView.Rows[rowIndex].Cells[0].Text);
            if (e.CommandName == "Cancel")
            {
                try
                {
                    newsManage.Delete(id);

                }
                catch (Exception ex)
                { throw (ex); }
            }

            if (e.CommandName == "View")
            {
                Response.Redirect("NewsDetail.aspx?id=" + id);
            }        
        }

        protected void DataGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                DataGridView.EditIndex = -1;
                GridViewBind();
            }
            catch (Exception ex)
            { throw ex; }
        }

        protected void DataGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((Button)e.Row.Cells[2].Controls[3]).Attributes.Add("onclick", "javascript:return confirm('你确定要删除此项么?')");
            }
        }

        protected void DropListNumber_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //选择数据显示行数
                AspNetPager.PageSize = Convert.ToInt32(DropListNumber.SelectedValue);
                GridViewBind();
            }
            catch (Exception ex)
            { throw ex; }
        }

        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewBind();
            }
            catch (Exception ex)
            { throw ex; }
        }
    }
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值