DropDownList的动态加载,detailview使用以及FileUpload的上传

所有代码
后台
  1 using  System;
  2 using  System.Data;
  3 using  System.Configuration;
  4 using  System.Collections;
  5 using  System.Web;
  6 using  System.Web.Security;
  7 using  System.Web.UI;
  8 using  System.IO;
  9 using  System.Web.Configuration;
 10 using  System.Web.UI.WebControls;
 11 using  System.Web.UI.WebControls.WebParts;
 12 using  System.Web.UI.HtmlControls;
 13 using  ShoppingChain.ManageSQLServerDAL;
 14 using  ShoppingChain.ManageModel;
 15 using  System.Collections.Generic;
 16
 17 public   partial   class  Manage_BusinessmanNewP : System.Web.UI.Page
 18 {
 19    protected void Page_Load(object sender, EventArgs e)
 20    {
 21        if (!IsPostBack)
 22        
 23            ProductInfo info = new ProductInfo();
 24
 25            if (Request.Cookies["uname"!= null)
 26            {               
 27                HttpCookie cookie = Request.Cookies["uname"];
 28                string uname = Server.HtmlEncode(cookie.Value);
 29                Product p = new Product();
 30                string ucate = p.GetBCatebyUname(uname);
 31                //拼表名
 32                info.GoodsinfoName = "GoodsInfo_" + ucate;
 33                info.GoodsdetailName = "GoodsDetail_" + ucate;
 34                ViewState["ucate"= ucate;             
 35            }

 36            string edit = Request.QueryString["edit"];
 37            if (edit == null)
 38            {
 39                DetailsView1.DefaultMode = DetailsViewMode.Edit;
 40
 41            }

 42            else
 43            {
 44                DetailsView1.DefaultMode = DetailsViewMode.Insert; 
 45                
 46            }
                                                   
 47        }
 
 48    }

 49    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
 50    {
 51
 52    }

 53    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
 54    {
 55        //insert之后执行,图片上传控制
 56        HttpRuntimeSection h = new HttpRuntimeSection();
 57        
 58        FileUpload f1 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload1");
 59        FileUpload f2 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload2");
 60        FileUpload f3 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload3");
 61        FileUpload f4 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload4");
 62        FileUpload f5 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload5");
 63
 64
 65        bool file1OK = false;
 66        bool file2OK = false;
 67        bool file3OK = false;
 68        bool file4OK = false;
 69        bool file5OK = false;
 70        String path = Server.MapPath("~/image/");
 71        String fileExtension;
 72        if (f1.HasFile)
 73        {
 74            fileExtension = Path.GetExtension(f1.FileName).ToLower();
 75            String[] allowedExtensions = ".gif"".png"".jpeg"".jpg" };
 76
 77            for (int i = 0; i < allowedExtensions.Length; i++)
 78            {
 79                if (fileExtension == allowedExtensions[i] && h.MaxRequestLength <= 500)
 80                {
 81                    file1OK = true;
 82                }

 83            }

 84        }

 85        if (file1OK)
 86        {
 87            try
 88            {
 89                f1.PostedFile.SaveAs(path + f1.FileName);
 90            }

 91            catch (Exception ex)
 92            {
 93                Response.Write("<script>window.alert('图片上传失败!')<script>");
 94            }

 95        }

 96        else
 97        {
 98            Response.Write("<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>");
 99        }

100
101        if (f2.HasFile)
102        {
103            fileExtension = Path.GetExtension(f2.FileName).ToLower();
104            String[] allowedExtensions = ".gif"".png"".jpeg"".jpg" };
105
106            for (int i = 0; i < allowedExtensions.Length; i++)
107            {
108                if (fileExtension == allowedExtensions[i] && h.MaxRequestLength <= 500)
109                {
110                    file2OK = true;
111                }

112            }

113        }

114        if (file2OK)
115        {
116            try
117            {
118                f2.PostedFile.SaveAs(path + f2.FileName);
119            }

120            catch (Exception ex)
121            {
122                Response.Write("<script>window.alert('图片上传失败!')<script>");
123            }

124        }

125        else
126        {
127            Response.Write("<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>");
128        }

129
130        if (f3.HasFile)
131        {
132            fileExtension = Path.GetExtension(f3.FileName).ToLower();
133            String[] allowedExtensions = ".gif"".png"".jpeg"".jpg" };
134
135            for (int i = 0; i < allowedExtensions.Length; i++)
136            {
137                if (fileExtension == allowedExtensions[i] && h.MaxRequestLength <= 500)
138                {
139                    file3OK = true;
140                }

141            }

142        }

143        if (file3OK)
144        {
145            try
146            {
147                f3.PostedFile.SaveAs(path + f3.FileName);
148            }

149            catch (Exception ex)
150            {
151                Response.Write("<script>window.alert('图片上传失败!')<script>");
152            }

153        }

154        else
155        {
156            Response.Write("<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>");
157        }

158
159        if (f4.HasFile)
160        {
161            fileExtension = Path.GetExtension(f4.FileName).ToLower();
162            String[] allowedExtensions = ".gif"".png"".jpeg"".jpg" };
163
164            for (int i = 0; i < allowedExtensions.Length; i++)
165            {
166                if (fileExtension == allowedExtensions[i] && h.MaxRequestLength <= 500)
167                {
168                    file4OK = true;
169                }

170            }

171        }

172        if (file4OK)
173        {
174            try
175            {
176                f4.PostedFile.SaveAs(path + f4.FileName);
177            }

178            catch (Exception ex)
179            {
180                Response.Write("<script>window.alert('图片上传失败!')<script>");
181            }

182        }

183        else
184        {
185            Response.Write("<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>");
186        }

187
188        if (f5.HasFile)
189        {
190            fileExtension = Path.GetExtension(f5.FileName).ToLower();
191            String[] allowedExtensions = ".gif"".png"".jpeg"".jpg" };
192
193            for (int i = 0; i < allowedExtensions.Length; i++)
194            {
195                if (fileExtension == allowedExtensions[i] && h.MaxRequestLength <= 500)
196                {
197                    file5OK = true;
198                }

199            }

200        }

201        if (file5OK)
202        {
203            try
204            {
205                f5.PostedFile.SaveAs(path + f5.FileName);
206            }

207            catch (Exception ex)
208            {
209                Response.Write("<script>window.alert('图片上传失败!')<script>");
210            }

211        }

212        else
213        {
214            Response.Write("<script>window.alert('文件类型不符或文件过大(500K以内)!')<script>");
215        }

216
217                     
218    }

219    /// <summary>
220    /// 添加附件按钮事件
221    /// </summary>
222    /// <param name="sender"></param>
223    /// <param name="e"></param>

224    protected void Button1_Click(object sender, EventArgs e)
225    {
226        //session记录用户点击次数
227        int session = Convert.ToInt32(Session["btnclick"]);
228        int see = session + 1;
229
230        Session["btnclick"= (object)see;          
231        //HtmlTable tb = (HtmlTable)DetailsView1.Rows[10].FindControl("tb");          
232        //if (session < 5)
233        //{
234        //    FileUpload f = new FileUpload();
235        //    f.Enabled = true;
236        //    f.ID = "f" + Session["btnclick"].ToString();
237        //    tb.Rows[session].Cells[1].Controls.Add(f);
238        //}
239        //else
240        //{
241        //    Response.Write("<script>alert('最多只能添加5个附件!')</script>");
242        //}  
243        
244        //FileUpload f = (FileUpload)DetailsView1.Rows[10].FindControl("tb"); 
245        Button a = (Button)DetailsView1.Rows[10].FindControl("btnaddfile1");
246        Button b = (Button)DetailsView1.Rows[10].FindControl("btnaddfile2");
247        Button c = (Button)DetailsView1.Rows[10].FindControl("btnaddfile3");
248        Button d = (Button)DetailsView1.Rows[10].FindControl("btnaddfile4");
249        Label l = (Label)DetailsView1.Rows[10].FindControl("lbladd");
250
251        FileUpload f1 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload1");
252        FileUpload f2 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload2");
253        FileUpload f3 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload3");
254        FileUpload f4 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload4");
255        FileUpload f5 = (FileUpload)DetailsView1.Rows[10].FindControl("FileUpload5");
256
257        switch (see)
258        {
259            case 1:
260                b.Visible = true;
261                a.Visible = false;
262                f2.Visible = true;
263                break;
264            case 2:
265                c.Visible = true;
266                b.Visible = false;
267                f3.Visible = true;
268                break;
269            case 3:
270                d.Visible = true;
271                c.Visible = false;
272                f4.Visible = true;
273                break;
274            case 4:
275                l.Visible = true;
276                d.Visible = false;
277                f5.Visible = true;
278                break;
279        }

280
281
282    }

283    protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
284    {
285        ProductInfo info = new ProductInfo();
286        foreach (DetailsViewRow row in DetailsView1.Rows) 
287        {
288            FileUpload f1 = (FileUpload)row.FindControl("FileUpload1");
289            FileUpload f2 = (FileUpload)row.FindControl("FileUpload2");
290            FileUpload f3 = (FileUpload)row.FindControl("FileUpload3");
291            FileUpload f4 = (FileUpload)row.FindControl("FileUpload4");
292            FileUpload f5 = (FileUpload)row.FindControl("FileUpload5");
293         
294            info.GoodsImage0 = f1.FileName + "~/image/";
295            info.GoodsImage1 = f2.FileName + "~/image/";
296            info.GoodsImage2 = f3.FileName + "~/image/";
297            info.GoodsImage3 = f4.FileName + "~/image/";
298            info.GoodsImage4 = f5.FileName + "~/image/";
299        }

300    }

301    /// <summary>
302    /// DetailsView事件(在呈现该页前激发)
303    /// </summary>
304    /// <param name="sender"></param>
305    /// <param name="e"></param>

306  
307    protected void DetailsView1_PreRender(object sender, EventArgs e)
308    {       
309        Label lb = (Label)DetailsView1.Rows[1].FindControl("lblcate");
310        lb.Text = ViewState["ucate"].ToString();
311        DropDownList dl = (DropDownList)DetailsView1.Rows[1].FindControl("dl1");
312        Product p = new Product();
313        IList<ProductInfo> list = p.GetAllPbyGoodsType(lb.Text);
314        //绑定DropDownList
315        dl.DataSource = list;
316        dl.DataTextField= "BranchName";
317        dl.DataValueField = "BranchID";
318        dl.DataBind();
319
320    }

321}

322

页面
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="BusinessmanNewP.aspx.cs" Inherits="Manage_BusinessmanNewP"  %>

<! 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  runat ="server" >
    
< title > 无标题页 </ title >

</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:DetailsView  ID ="DetailsView1"  runat ="server"  AutoGenerateRows ="False"
            DataSourceID
="ObjectDataSource1"  Height ="32px"  Width ="466px"  OnItemInserted ="DetailsView1_ItemInserted"  OnPreRender ="DetailsView1_PreRender" >
            
< Fields >
                
< asp:TemplateField  HeaderText ="商品名称" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox2"  runat ="server"  Text ='<%#  Bind("GoodsName") % > '> </ asp:TextBox >
                        
< asp:HiddenField  ID ="HiddenField1"  runat ="server"  Value ='<%#  Bind("GoodsID") % > ' />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="所属分类" >
                    
< ItemTemplate >
                        
&nbsp;
                        
< asp:Label  ID ="lblcate"  runat ="server"  Text ="Label" ></ asp:Label >
                        
< asp:DropDownList  ID ="dl1"  runat ="server"  AutoPostBack ="True"  SelectedValue ='<%#  DataBinder.Eval(Container.DataItem,"BranchName") % > ' AppendDataBoundItems="True">
                            
< asp:ListItem  Selected ="True" ></ asp:ListItem >
                            
                        
</ asp:DropDownList >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="市场价" >
                    
< ItemTemplate >                      
                        
< asp:TextBox  ID ="TextBox3"  runat ="server"  Text ='<%#  Bind("MarketPrices") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="发布价" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox4"  runat ="server"  Text ='<%#  Bind("StockPrice") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="商品产地" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox5"  runat ="server"  Text ='<%#  Bind("GoodsOrigin") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="商品售后" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox6"  runat ="server"  Text ='<%#  Bind("GoodsService") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="商品上市时间" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox7"  runat ="server"  Text ='<%#  Bind("GoodsTime") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="商品参数" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox8"  runat ="server"  Text ='<%#  Bind("GoodsParameters") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="商品数量" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox9"  runat ="server"  Text ='<%#  Bind("GoodsCount") % > '> </ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="退货条款" >
                    
< ItemTemplate >
                        
< asp:TextBox  ID ="TextBox10"  runat ="server"  Height ="233px"  Text ='<%#  Bind("GoodsReturn") % > '
                            Width="263px">
</ asp:TextBox >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField  HeaderText ="上传图片" >
                    
< ItemTemplate >
                        
&nbsp;&nbsp;
                        
< table  style ="width: 210px; height: 123px"  runat ="server"  id ="tb" >
                            
< tr >
                                
< td >
                        
< asp:FileUpload  ID ="FileUpload1"  runat ="server"   /></ td >
                                
< td >
                        
< asp:Button  ID ="btnaddfile1"  runat ="server"  OnClick ="Button1_Click"  Text ="继续添加附件"  Height ="20px"  Width ="97px"   />
                                    
< asp:HiddenField  ID ="HiddenField2"  runat ="server"  Value ='<%#  Bind("GoodsImage0") % > ' />
                                
</ td >
                            
</ tr >
                            
< tr  id ="tr2" >
                                
< td  id ="td21"  style ="height: 21px" >< asp:FileUpload  ID ="FileUpload2"  runat ="server"  Visible ="False" /></ td >
                                
< td  id ="td22"  style ="height: 21px" >< asp:Button  ID ="btnaddfile2"  runat ="server"  OnClick ="Button1_Click"  Text ="继续添加附件"  Height ="20px"  Width ="97px"  Visible ="False"   />
                                    
< asp:HiddenField  ID ="HiddenField3"  runat ="server"  Value ='<%#  Bind("GoodsImage1") % > '/>
                                
</ td >
                            
</ tr >
                            
< tr  id ="tr3" >
                                
< td  id ="td31" >< asp:FileUpload  ID ="FileUpload3"  runat ="server"  Visible ="False" /></ td >
                                
< td  id ="td32" >< asp:Button  ID ="btnaddfile3"  runat ="server"  OnClick ="Button1_Click"  Text ="继续添加附件"  Height ="20px"  Width ="97px"  Visible ="False"   />
                                    
< asp:HiddenField  ID ="HiddenField4"  runat ="server"  Value ='<%#  Bind("GoodsImage2") % > '/>
                                
</ td >
                            
</ tr >
                            
< tr  id ="tr4" >
                                
< td  id ="td41" >< asp:FileUpload  ID ="FileUpload4"  runat ="server"  Visible ="False" /></ td >
                                
< td  id ="td42" >< asp:Button  ID ="btnaddfile4"  runat ="server"  OnClick ="Button1_Click"  Text ="继续添加附件"  Height ="20px"  Width ="97px"  Visible ="False"   />
                                    
< asp:HiddenField  ID ="HiddenField5"  runat ="server"  Value ='<%#  Bind("GoodsImage3") % > '/>
                                
</ td >
                            
</ tr >
                            
< tr  id ="tr5" >
                                
< td  id ="td51" >< asp:FileUpload  ID ="FileUpload5"  runat ="server"  Visible ="False" /></ td >
                                
< td  id ="td52" >
                                    
< asp:Label  ID ="lbladd"  runat ="server"  Height ="18px"  Text ="最多只能添加5张图片"  Visible ="False"
                                        Width
="153px" ></ asp:Label >
                                    
< asp:HiddenField  ID ="HiddenField6"  runat ="server"  Value ='<%#  Bind("GoodsImage4") % > '/>
                                
</ td >
                            
</ tr >
                        
</ table >
                        
< br  />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:CommandField  ShowCancelButton ="False"  ShowEditButton ="True"  ShowInsertButton ="True"   />
            
</ Fields >
            
< HeaderTemplate >
                
&nbsp;   &nbsp;   &nbsp;&nbsp;
            
</ HeaderTemplate >
        
</ asp:DetailsView >
        
&nbsp;&nbsp;
        
< asp:ObjectDataSource  ID ="ObjectDataSource1"  runat ="server"  OldValuesParameterFormatString ="original_{0}"
            SelectMethod
="GetPInfobyPID"  TypeName ="BLL.Product"  DataObjectTypeName ="ShoppingChain.ManageModel.ProductInfo"  InsertMethod ="InsertP"  UpdateMethod ="UpdateP"  OnInserting ="ObjectDataSource1_Inserting" >
            
< SelectParameters >
                
< asp:QueryStringParameter  Name ="pid"  QueryStringField ="pid"  Type ="Int32"   />
                
< asp:CookieParameter  CookieName ="uname"  Name ="uname"  Type ="String"   />
            
</ SelectParameters >
        
</ asp:ObjectDataSource >
        
&nbsp;&nbsp; < br  />
        
&nbsp; < br  />
    
    
</ div >
    
</ form >
</ body >
</ html >

 
我的代码很糟糕~~
期间遇到的问题:
1. Eval()、XPath() Bind() 这类数据绑定方法只能在数据绑定控件...
解决:把绑定语句改成了SelectedValue='<%# DataBinder.Eval(Container.DataItem," BranchName")%>'
2.无效的selectedvalue
解决:将Dropdownlist的item加一列空白项
3. 记录用户点击次数的方案选择:开始想用cookie,但是发现cookie的修改很麻烦。viewstate也不好。最后决定用session
4.int型主键自增长主子表的插入  ,表名为变量
解决:动态执行sql,拼接字符串(加倍谨慎,极易出错,极难发现错误所在)
5.上传图片以及验证格式和大小
6.DetailsView的使用(好几天才学会)
7.想到再写。年前就这样了。明早回家喽~

你可能感兴趣的:(fileupload)