<%@ Page Language="C#" ContentType="text/html" Inherits="Ycbw.Data.OleDbClient" %>
<%@ Register TagPrefix="Rajah" TagName="Head" Src="../ctrls/Head.ascx" %>
<%@ Register TagPrefix="Rajah" TagName="Foot" Src="../ctrls/Foot.ascx" %>
<script language="c#" runat="server">

private void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		this.BindGrid();
	}
}

private void BindGrid()
{
	string strSql = "Select * From [b_links]";

	this.BindGrid(strSql + " Order By id Desc", "data_list", this.data_list);
}

private void button_add_click(object sender, EventArgs e)
{
	string err = "";

	//一大堆判断
	if (err.Length > 0)
	{
		Response.Write(err);
	}
	else
	{
		this.Fill("select top 1 * from [b_links] where l_title like '" + this.l_title.Text + "'", "data_links");

		if (this.Data.Tables["data_links"].Rows.Count > 0)
		{
			this.JWrite("alert('添加失败, 数据有重复!');window.location.href='links.aspx';");
		}
		else
		{
			string l_logo 	= this.l_logo.Text;
			string l_title 	= this.l_title.Text;
			string l_url 	= this.l_url.Text;

			//上传图片
			HttpPostedFile inputFile = Request.Files[0];

			if (inputFile.FileName != "")
			{
				string fileExt = inputFile.FileName.Substring(inputFile.FileName.LastIndexOf("."));
				string filename = DateTime.Now.ToString("yyMMddhhmmss") + this.CreateRandomCode(2).ToString() + fileExt;

				this.Upload(inputFile, "../../uploadfiles", filename);
				l_logo = "../../uploadfiles/" + filename;
			}
				
			this.Execute("insert into [b_links] ([l_title], [l_url], [l_logo]) values ('" + l_title + "', '" + l_url + "', '" + l_logo + "')");
			
			this.BindGrid();
		}
	}
}

private void data_list_PageChanged(object sender, DataGridPageChangedEventArgs e)
{
	((DataGrid)sender).CurrentPageIndex = e.NewPageIndex;
	
	this.BindGrid();
}

private void data_edit_click(object sender, DataGridCommandEventArgs e)
{
	((DataGrid)sender).EditItemIndex = (int)e.Item.ItemIndex;
	
	this.BindGrid();
}

private void data_update_click(object sender, DataGridCommandEventArgs e)
{
	string id 		= ((Label)e.Item.FindControl("id")).Text;
	string l_title 	= ((TextBox)e.Item.FindControl("l_title")).Text;
	string l_url 	= ((TextBox)e.Item.FindControl("l_url")).Text;
	string l_logo 	= ((TextBox)e.Item.FindControl("l_logo")).Text;

	this.Execute("Update [b_links] set [l_title] = '" + l_title + "', [l_url] = '" + l_url + "', [l_logo] = '" + l_logo + "' where id = " + id);

	((DataGrid)sender).EditItemIndex = -1;
	
	this.BindGrid();
}

private void data_cancel_click(object sender, DataGridCommandEventArgs e)
{
	((DataGrid)sender).EditItemIndex = -1;
	
	this.BindGrid();
}

private void data_delete_click(object sender, DataGridCommandEventArgs e)
{
	string id = ((Label)e.Item.FindControl("id")).Text;
	
	this.DeleteFiles(id);

	this.Execute("Delete from [b_links] where id = " + id);
	
	this.BindGrid();
}

private void DeleteFiles(string id)
{
	this.Fill("select top 1 * from [b_links] where id = "+ id, "link");
	
	System.Data.DataRow dr = Tables["link"].Rows[0];
	
	if( dr["l_logo"].ToString() != "" )
		this.DeleteFile(dr["l_logo"].ToString());
}

private void data_upload_click(object sender, DataGridCommandEventArgs e)
{
	if (e.CommandName == "item")
	{
		HttpPostedFile inputFile2 = Request.Files[0];
		
		if (inputFile2.FileName != "")
		{
			string fileExt = inputFile2.FileName.Substring(inputFile2.FileName.LastIndexOf("."));
			string filename = DateTime.Now.ToString("yyMMddhhmmss") + this.CreateRandomCode(2).ToString() + fileExt;
			
			this.Upload(inputFile2, "../../uploadfiles", filename);

			((TextBox)e.Item.FindControl("l_logo")).Text = "../../uploadfiles/" + filename;
			
			this.data_update_click(sender, e);
		}
	}
}
</script>

<Rajah:Head ID="Head" runat="Server" />
<script language="javascript" src="../js/drag.js"></script>
<form id="form1" runat="server">
<div id="body">
	<div class="title3">友情链接管理</div>
	<div>
	<table width="100%" align="center" border="0" cellspacing="8" cellpadding="0">
	  <tr>
		<td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="table3">
			<tr>
			  <td><input type="button" onclick="javascript:showAdd();" value=" 添加 " /></td>
			  <td align="right"><asp:Label ID="text_pages1" runat="server" /></td>
			</tr>
		  </table>
		  <asp:DataGrid ID="data_list" Width="100%" BorderWidth="0" CellPadding="0" CellSpacing="0" AutoGenerateColumns="false" runat="server" 
			CssClass="autotext" HeaderStyle-CssClass="header1" ItemStyle-CssClass="item1" 
			OnItemCommand="data_upload_click" OnEditCommand="data_edit_click" OnUpdateCommand="data_update_click" OnCancelCommand="data_cancel_click" OnDeleteCommand="data_delete_click">
			<columns>
			
			<asp:TemplateColumn HeaderStyle-Width="24">
			  <headertemplate>
				<input type="checkbox" onclick="checkAll(this)">
			  </headertemplate>
			  <itemtemplate>
				<input type="checkbox" name="ids" value="<%# Eval("id") %>">
				<asp:Label ID="id" Text='<%# Eval("id") %>' Visible="false" runat="server" />
			  </itemtemplate>
			</asp:TemplateColumn>
			
			<asp:TemplateColumn HeaderStyle-Width="400" ItemStyle-Height="50">
			  <headertemplate>LOGO</headertemplate>
			  <itemtemplate> <img src="<%# DataBinder.Eval( Container.DataItem, "l_logo" ).ToString() %>" width="114" height="40" border="0"> </itemtemplate>
			  <edititemtemplate>
			    <div style="float:left"><asp:TextBox ID="l_logo" CssClass="text2" Width="200" Text='<%# DataBinder.Eval( Container.DataItem, "l_logo" ) %>' runat="server" /></div>
				<div style="float:left; padding-left:3px;">
				<asp:FileUpload ID="FileUpload2" runat="server" width="69" height="21" /></div><div style="float:left; padding-left:3px;"><asp:Button runat="server" CommandName="item" Text=" 上传 "/></div>
			  </edititemtemplate>
			</asp:TemplateColumn>
			
			<asp:TemplateColumn HeaderStyle-Width="200">
			  <headertemplate>名称</headertemplate>
			  <itemtemplate> <%# DataBinder.Eval( Container.DataItem, "l_title" ).ToString() %> </itemtemplate>
			  <edititemtemplate>
				<asp:TextBox ID="l_title"  CssClass="text2" Width="90%" Text='<%# DataBinder.Eval( Container.DataItem, "l_title" ) %>' runat="server" />
			  </edititemtemplate>
			</asp:TemplateColumn>
			
			<asp:TemplateColumn HeaderStyle-Width="200">
			  <headertemplate>地址</headertemplate>
			  <itemtemplate> <%# DataBinder.Eval( Container.DataItem, "l_url" ).ToString() %> </itemtemplate>
			  <edititemtemplate>
				<asp:TextBox ID="l_url" CssClass="text2" Width="90%" Text='<%# DataBinder.Eval( Container.DataItem, "l_url" ) %>' runat="server" />
			  </edititemtemplate>
			</asp:TemplateColumn>
			
			<asp:TemplateColumn HeaderStyle-Width="100%">
			  <itemtemplate>
				<asp:LinkButton Text="修改" CommandName="edit" runat="server" />
				<span onclick="return confirm('确定要删除该条资料吗?')"><asp:LinkButton Text="删除" CommandName="delete" runat="server" /></span>
			  </itemtemplate>
			  <edititemtemplate>
				<asp:LinkButton Text="确定" ForeColor="#FF0000" CommandName="update" runat="server" />
				<asp:LinkButton Text="取消" ForeColor="#FF0000" CommandName="cancel" runat="server" />
			  </edititemtemplate>
			</asp:TemplateColumn>
			</columns>
		  </asp:DataGrid>
	  </tr>
	  <tr>
		<td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="table3">
			<tr>
			  <td>&nbsp;</td>
			  <td align="right"><asp:Label ID="text_pages" runat="server" /></td>
			</tr>
		  </table></td>
	  </tr>
	</table>
	</div>
</div>

<div id="add" style="position:absolute; left:100px; top:100px; display:none; border:1px solid #4E86C4;">
	<div class="title4" onmousedown="setMove(this);" style="cursor:move;">新增友情链接</div>
	<div>
		<table border="0" align="center" cellpadding="5" cellspacing="0">
          <tr bgcolor="#FFFFFF">
            <td width="60" height="24" align="right"> 名称</td>
            <td width="180"><asp:TextBox ID="l_title" CssClass="text2" Width="100%" runat="server" />
            </td>
            <td width="60" align="right"> 地址</td>
            <td width="180"><asp:TextBox ID="l_url" CssClass="text2" Width="100%" runat="server" />
            </td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td height="24" align="right"> LOGO</td>
            <td><asp:TextBox ID="l_logo" CssClass="text2" Width="100%" runat="server" />
            </td>
            <td align="right"> 上传图片</td>
            <td><asp:fileupload id="FileUpload1" runat="server" width="100%" cssclass="text2" />
			<asp:Label ID="logo" Visible="false" style="display: none" runat="server" Text="Label"></asp:Label>
            </td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td colspan="4" class="table4">
              <asp:Button Text=" 提交 " runat="server" OnClick="button_add_click" OnClientClick="return checkSubmit();" />
              <input type="button" value=" 取消 " onclick="hideAdd()"></td>
          </tr>
        </table>
		<script language="javascript">
		function checkSubmit()
		{
			if( document.form1.l_title.value == "" )
			{
				alert('名称必须填写');
				return false;
			}
			
			if( document.form1.l_url.value == "" )
			{
				alert('地址必须填写');
				return false;
			}

			return true;
		}
		</script>
	</div>
</div>
</form>
<Rajah:Foot ID="Foot" runat="Server" />
