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

.net 网站开发知识点二

程序员文章站 2022-12-01 19:16:13
net 网站开发知识点一 介绍了Form验证,FCKediter控件的使用,以及分页控件的使用,这章将再介绍几点简单的知识点,掌握这些知识,就可以简单一个简单的新闻发布网站。...

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

这样的新闻预览标题可以用reapeter控件实现
 
[html] 
<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 " > 
      <p 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"> 
      <p 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> 
后台代码如下:
[csharp]
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绑定:
[html] 
<p 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> 
               <p style=" width:125px"><%# Eval("fileMonth") %>  <%# Eval("fileYear") %></p> 
               </td></tr> 
               </table>           
             </li>   
        </ItemTemplate> 
   <FooterTemplate> 
   </ul>           
   </FooterTemplate> 
       </asp:Repeater> 
       </p>   

Reperter需要设置一定的CSS样式:
[html]
.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绑定数据也很简单,这里要讲一下操作这个模板项
代码如下:
[html] 
<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>可以确定行号
后台代码:
[csharp] 
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; } 
        } 
    }