2011-06-08 45 views
0

您訪問Value財產之前,我無法從我的下拉框中得到比一個空值以外的其他,我嘗試將文件上傳到不同的目錄...DropDownList1.SelectedValue爲空?

public class dropDownInfo 
{ 
    public string pathName { get; set; } 
    public string pathValue { get; set; } 
} 

string uploadFolder = ""; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     // reference to directory 
     //DirectoryInfo di = new DirectoryInfo("//DOCSD9F1/TECHDOCS/"); 
     DirectoryInfo di = new DirectoryInfo("D:/SMGUpload/SMGUpload/files/"); 

     // create list of directories 
     List<dropDownInfo> DropDownList = new List<dropDownInfo>(); 
     foreach (DirectoryInfo i in di.GetDirectories()) 
     { 
      dropDownInfo ddInfo = new dropDownInfo(); 
      ddInfo.pathName = i.FullName; 
      ddInfo.pathValue = i.FullName; 
      DropDownList.Add(ddInfo); 
     } 
     DropDownList1.DataSource = DropDownList; 
     DropDownList1.DataTextField = "pathName"; 
     DropDownList1.DataValueField = "pathValue"; 
     DropDownList1.DataBind(); 
    } 
} 

protected void DropDownList1_IndexChanged(object sender, EventArgs e) 
{ 
    uploadFolder = DropDownList1.SelectedItem.Value; 
} 

protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e) 
{ 
    if (e.IsValid) 
    { 
     string uploadDirectory = Server.MapPath("~/files/"); 
     //string uploadDirectory = @"\\DOCSD9F1\TECHDOCS\"; 

     string fileName = e.UploadedFile.FileName; 

     //string uploadFolder = DropDownList1.SelectedValue; 

     //string path = (uploadDirectory + uploadFolder + "/" + fileName); 
     string path = Path.Combine(Path.Combine(uploadDirectory, uploadFolder), fileName); 

     e.UploadedFile.SaveAs(path); 
     e.CallbackData = fileName; 
    } 
} 
+0

並使用dropdownlist1.selecteditem.value;給了我一個參考例外。 – bluetickk 2011-06-08 17:13:42

+0

@bluetickk你可以嘗試DropDownList1.SelectedValue嗎? – thomasvdb 2011-06-08 17:38:03

+0

同樣的事情,它仍然是null – bluetickk 2011-06-08 17:53:03

回答

0

做一次檢查。

if (DropDownList1.SelectedItem != null) 
    uploadFolder = DropDownList1.SelectedItem.Value; 
+0

does not解釋爲什麼這些項目爲空=/ – bluetickk 2011-06-08 17:25:56

0

回發後下拉沒有值。您只在第一次加載頁面時綁定,然後回發頁面(索引已更改),並且不會重新綁定項目。

+0

所以..我需要改變什麼? – bluetickk 2011-06-08 19:12:03

0

您是否在頁面或任何控件上禁用了viewstate?這可能會導致您所描述的問題。

此外,本地變量uploadFolder將永遠不會保留在回發之間。您需要將其存儲在會話中或某個地方的頁面上。

Session["uploadFolder"] = DropDownList1.SelectedItem.Value 
+0

現在它只是上傳文件夾是空..我放置了一個標籤,以確認dropdownlist1.selecteditem.text正在工作,它是...但由於某種原因,我上傳文件夾拒絕採取值 – bluetickk 2011-06-08 19:47:10

+0

'uploadFolder'變量將是重置每個帖子。將其粘貼到會話中或直接使用DropDownList1.SelectedItem.Value。 – nbushnell 2011-06-08 19:57:44

0

您需要在回發時重新設置DataSource,但不要重新綁定或重置所選索引。

相關問題