顶好的小窝

闲时研究些小程序,编编小软件,写点小心得,兴许对你有用,欢迎来坐坐。

导航

用列表框选择图片(C#,写给小菜菜们)

 

  在论坛或留言板中选择表情的时候,用下拉列表是常用的方法,以下的代码演示了如何用c#实现。

HTML: 

形象选择:<asp:dropdownlist id="PicList" onChange="document.images['face'].src=options[selectedIndex].value;"
runat="server" Width="80px">
<BR>
&nbsp;&nbsp;&nbsp;
<IMG id="face" src="images\face\gg01.gif" runat="server">

PicList在变化后执行的是客户端的javascript程序:将img的名字设为face,在js中必须用document.images['face']来引用,如果代码改成

<img id="face" name="face" src="images\face\gg01.gif">

    则可以用document.face来访问。

    以下是C#代码,

if (!Page.IsPostBack)
{
    
this.PicList_Init();
}
  private void PicList_Init()// FillPicList 填充图片列表框
  {
   
this.PicList.Items.Clear();
   
string  FileName,ImgDir;
   System.IO.FileInfo Fi;
   ImgDir
="images\\face\\";
   System.IO.DirectoryInfo Dir
=new System.IO.DirectoryInfo(Server.MapPath("images\\face\\"));
   foreach (System.IO.FileSystemInfo Fsi in Dir.GetFileSystemInfos())
   
{
    
if (Fsi is System.IO.FileInfo)
    
{
     Fi
=(System.IO.FileInfo) Fsi;
     FileName 
= Fi.Name;
     
if (((Fi.Extension==".gif")||(Fi.Extension==".jpg"))&& (Fi.Name!="admin.gif"))
      
this.PicList.Items.Add(new ListItem(FileName, ImgDir+FileName));

    }

    
this.PicList.SelectedIndex=0;
    
this.face.Src=this.PicList.SelectedValue;
   }
 //end of foreach 
  }

    第一次执行时,通过遍历image\face\文件夹下所有文件,将后缀名为.gif和.jpg的文件(除去管理员形象的admin.gif)加入PicList的列表。小菜菜们(我也是哦)要注意了,为了防止黑客,别忘了在接受返回数据的时候,服务器端也应该加入代码,谨防有人使用admin.gif将自己打扮成管理员模样,如:

try
{
    .
    
if (this.PicList.SelectedValue.ToString().IndexOf("admin.gif")>-1)
    
throw (new System.Exception());
    .
}

posted on 2004-10-22 23:45  顶好  阅读(2806)  评论(0编辑  收藏  举报