<%@ Page Language="C#" ContentType="text/html" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="My" TagName="MyHead" Src="head.ascx" %>
<%@ Register TagPrefix="My" TagName="MyFoot" Src="foot.ascx" %>
<%@ Register TagPrefix="My" TagName="MyMessage" Src="message.ascx" %>
 <!-- #INCLUDE file="../inc/Function.aspx" -->

<script runat="server">
 string AdvUserName;
     String strSQL;
	 String UserName;
     String strConn = ConfigurationSettings.AppSettings["ConnectionString"];
	 DataSet ds=new DataSet();
     SqlDataAdapter da = new SqlDataAdapter();
		 const int Record_Per_Page=20; //每页的记录数
         int nPagecount; //总页数
         int nRecCount; //定义总记录数
         int nPage; //定义当前页

protected void Page_Load(Object Src, EventArgs E)
{
    //------判断用户是否登录---
     if(Session["Login_UserName"]==null || Session["IsLogin"]==null || Session["Userclass"]==null || Session["Login_UserName"]=="" || Session["IsLogin"].ToString().ToLower()!="true" )
   {   
      Response.Write("<script language=javascript>alert ('登录超时');document.location='../ ';<");
	  Response.Write("/script>");
	  Response.End();
    }
   AdvUserName=Session["Login_UserName"].ToString();
  //-------------------------


   
  strSQL="Select * From News where ToUserName='"+AdvUserName+"' and NewsType=5 and toAffairs=2 order by id desc";
    if (!IsPostBack)
     {
	  nPage=0;
      CreateDataSetObject();
      CmdDataBind();
      }
   else
   {
	//strSQL=Temp_strSQL_1 + Page_strSQL.Text.ToString();
   	CreateDataSetObject();
	//Response.Write(strSQL);
	
   }
   
}


 //绑定DataGrid
 protected void CmdDataBind()
  {
   
    ds.Clear();
	
    da = new SqlDataAdapter(strSQL,strConn);
    da.Fill(ds,nPage*Record_Per_Page,Record_Per_Page,"News");
    DataGrid1.DataSource=ds;
	DataGrid1.DataBind(); 
	ispages.Text=(nPage).ToString();
	Page_nRecCount_1.Text=nRecCount.ToString();
	Page_nPage.Text=(nPage+1).ToString();
	Page_nRecCount.Text=nPagecount.ToString();
	//Console.WriteLine(ds.Tables["AdvCount"].Rows[1]);
	SetupPage();
	IsFirstLastPage();
   }

 protected void CreateDataSetObject()
 {   
     ds.Clear();
     da = new SqlDataAdapter(strSQL,strConn);
	 da.Fill(ds,"News"); 
	 nRecCount=ds.Tables[0].Rows.Count;
	 if(nRecCount%Record_Per_Page==0)
	 {
	  nPagecount=nRecCount/Record_Per_Page;
	  }else
	   {
	   nPagecount=nRecCount/Record_Per_Page+1;
	   }
  }

//设置分页列表框
 protected void SetupPage()
  {
   thispages.Items.Clear();
   for(int i=1;i<=nPagecount;i++)
   {
    thispages.Items.Add(i.ToString());
	}
	
	if(nPagecount>0){thispages.SelectedIndex=(int)Math.Min(nPage,nPagecount);}
	
   }


//选择分页
 private void Page_DropDownList(Object Src, EventArgs E)
  {
    nPage=Convert.ToInt32(thispages.SelectedItem.Value)-1;
	 IsFirstLastPage();
	 Page_nPage.Text=(nPage+1).ToString();
	 Page_nRecCount.Text=nPagecount.ToString();
	 CmdDataBind();
   }
 
   
//分页
 private void Page_Click(Object Src, EventArgs E)
  {
   string commangArg=((LinkButton)Src).CommandArgument;
   span1.InnerHtml="";
   if(!IsNum(ispages.Text))
    {
	 nPage=0;
	 }
	 else
	 {
      nPage=Convert.ToInt32(thispages.SelectedItem.Value.ToString())-1;
	 }
	 
   switch(commangArg)
    {
	 case "First": //如果点首页
	  {
	  nPage=0;
	  break;
	  }
	  case "Prev": //上一页
	  {
	   nPage=(int)Math.Max(0,nPage-1);
	   break;
	   }
	  case "Next":  //下一页
	  {
	   nPage=(int)Math.Min(nPagecount-1,nPage+1);
	   break;
	   }
       case "Last": //尾页
	    {
		 nPage=nPagecount-1;
		 break;
		 }
		default:{break;};  
	 }
	 
	 //Response.Write(nPage);
	// Response.End();
	 
	 IsFirstLastPage();
	 Page_nPage.Text=(nPage+1).ToString();
	 Page_nRecCount.Text=nPagecount.ToString();
	 CmdDataBind();
   }
   
 // 分页按妞是否可见
 private void IsFirstLastPage()
  {
   if (nPagecount>1)
    {
	 if(nPagecount!=1)
	  {
	    if(nPage==0){PagePrev.Enabled=false;PageFirst.Enabled=false;}
		else{PagePrev.Enabled=true;PageFirst.Enabled=true;}
	    if(nPage==nPagecount-1){PageNext.Enabled=false; PageLast.Enabled=false;}
		else{PageNext.Enabled=true; PageLast.Enabled=true;}
		
	   }
	   else
	   {
	    PagePrev.Enabled=false; PageNext.Enabled=false;
	   }
	 }
   else
   {
    PageFirst.Enabled=false; PagePrev.Enabled=false; PageNext.Enabled=false; PageLast.Enabled=false;
    }
   }
   
  //删除记录
 private void Del_DataSet(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  	{
	  string DelSql;
	  
       if(e.CommandName.ToString()=="Del")
	   {
	    SqlConnection cn = new SqlConnection(strConn);
		 DelSql="delete from News where ToUserName='"+AdvUserName+"'and id="+e.Item.Cells[0].Text.ToString();
         SqlCommand SComm= new SqlCommand(DelSql,cn);
         cn.Open();
         SComm.ExecuteNonQuery();
		 cn.Close();
		 span1.InnerHtml=msgbox_1("删除成功");
		 CmdDataBind();
	   }
	   
	  if(e.CommandName.ToString()=="Reply")
	  {
	   Response.Redirect("ReplyNews.Aspx?Action=Reply&id="+e.Item.Cells[0].Text.ToString());
	   }     
	}


</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>5507广告联盟 - 广告商管理后台 -我的收件箱</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<my:MyHead runat="server"/>

<table width="980" border="0" align="center" cellpadding="0" cellspacing="0" class="table_3">
  <tr>
    <td width="20" height="15"><img src="../web/images/body_1.gif" /></td>
    <td width="926" background="../web/images/body_2.gif"></td>
    <td width="20"><img src="../web/images/body_3.gif" width="20" height="15" /></td>
  </tr>
</table>
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="15" background="../web/images/body_7.gif" bgcolor="#FFFFFF"><p>&nbsp;</p>
        <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td width="953" valign="top" bgcolor="#FFFFFF"><table width="950" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="140" valign="top">
		<form runat="server">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30"><table width="98%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="6%"><div align="center"><img src="../images/adv_list.gif" width="25" height="25" /></div></td>
                <td width="15%"><strong>我的收件箱</strong></td>
                <td width="79%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="30">
			<asp:DataGrid
			 AllowSorting="true"
			   AlternatingItemStyle-BackColor="#F7F7F7" 
			 AutoGenerateColumns="false"
			 BackColor="#ffffff"
			  BorderColor="#dddddd" 
			  BorderWidth="1"
			   DataKeyField="id"
			   FooterStyle-ForeColor="#EAEAEA"
			   HeaderStyle-BackColor="#F3F3F3" 
			   HeaderStyle-Height="25"
			   HeaderStyle-HorizontalAlign="Center" 
			  Height="25"
			  ID="DataGrid1" 
			   runat="server" 
			   ShowFooter="false" 
			   ShowHeader="true"
			   Width="98%" 
			   gridline="Both" 
			   OnItemCommand="Del_DataSet"
			   >
			<columns>
			 
			 <asp:boundcolumn datafield="id" ItemStyle-Width="30" ItemStyle-Height="25" ItemStyle-HorizontalAlign="Center" HeaderText="NO"/>
			 			 
                <asp:HyperLinkColumn
                    DataNavigateUrlField="id"
					DataNavigateUrlFormatString="ReplyNews.aspx?Action=Read&id={0}"
                    DataTextField="Title"
                    HeaderText="标题"
                    NavigateUrl="read.aspx"
                    SortExpression="string"
                    Target="_blank"
                    />
			 
	<asp:boundcolumn datafield="UserName" ItemStyle-Height="25"  HeaderText="发件人" ItemStyle-HorizontalAlign="Center" />
			
               <asp:TemplateColumn HeaderText="阅读" ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center"> 
                <itemtemplate>	
			      <%#(DataBinder.Eval(Container.DataItem,"hits").ToString()=="0")?"<font color=ff00ff>未读</a>":"<font color=ff0000>已读</font>"  %>	 
				 
				</itemtemplate>
              </asp:TemplateColumn>

               <asp:TemplateColumn HeaderText="操作" ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center"> 
                <itemtemplate>		
				 <asp:LinkButton ID="del" runat="server" Text="删除" OnClientClick="return confirm('你确定要删除该记录吗？')" CommandName="Del"></asp:LinkButton>
				 
				 <asp:LinkButton ID="Reply" runat="server" Text="回复"  CommandName="Reply"></asp:LinkButton>
				 
				</itemtemplate>
              </asp:TemplateColumn>

               
    </columns>
    </asp:DataGrid>			
			
			</td>
          </tr>
          <tr>
            <td height="30">
			
			<div align="center">
				 <asp:LinkButton ID="PageFirst" OnClick="Page_Click" Text="首页" runat="server" CommandArgument="First"/> 
				   |  
				     <asp:LinkButton ID="PagePrev" OnClick="Page_Click" Text="上一页" runat="server" CommandArgument="Prev"/>
				 | 
				 <asp:LinkButton ID="PageNext" OnClick="Page_Click" Text="下一页" runat="server" CommandArgument="Next"/> 
				 |  
				 <asp:LinkButton ID="PageLast" OnClick="Page_Click" Text="尾页" runat="server" CommandArgument="Last"/>
				 <asp:Label ID="CountPage" runat="server" />                  
				 转到:
				 <asp:TextBox AutoPostBack="true" Columns="2" ID="ispages" runat="server" Visible="false" />   
				                
				 <asp:DropDownList AutoPostBack="true" ID="thispages" runat="server" OnSelectedIndexChanged="Page_DropDownList"></asp:DropDownList>
				 页  第 
				 <asp:Label ID="Page_nPage" runat="server" ForeColor="Red"></asp:Label> 页/共 <asp:Label ID="Page_nRecCount" ForeColor="Red" runat="server"></asp:Label> 
				 页 一共 
				 <asp:Label ID="Page_nRecCount_1" runat="server" ForeColor="Red"></asp:Label>
条  <asp:Label ID="Page_strSQL" runat="server" Visible="false"></asp:Label>	

</div>	
			
			
			
			</td>
          </tr>
        </table>
		<SPAN id="span1" runat="server"></SPAN>
		</form>
		
		</td>
        <td width="220" valign="top"><my:MyMessage runat="server"/></td>
      </tr>
    </table></td>
    <td width="15" background="../web/images/body_8.gif" bgcolor="#FFFFFF">&nbsp;</td>
  </tr>
</table>

<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="20"><img src="../web/images/body_4.gif" width="20" /></td>
    <td width="926" background="../web/images/body_5.gif">&nbsp;</td>
    <td width="20"><div align="right"><img src="../web/images/body_6.gif" width="20" height="20" /></div></td>
  </tr>
</table>

<my:MyFoot runat="server"/>

</body>
</html>
