DropDownList1.SelectedValue为空?

时间:2011-06-08 17:11:41

标签: c# asp.net drop-down-menu

我无法从我的下拉框中获取任何其他值,我试图将文件上传到不同的目录...

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;
    }
}

4 个答案:

答案 0 :(得分:0)

在访问Value属性之前进行检查。

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

答案 1 :(得分:0)

回发后,下拉列表没有值。您只在第一页加载时绑定,然后页面回发(索引已更改)且项目未重新绑定。

答案 2 :(得分:0)

您是否在页面或任何控件上禁用了viewstate?这可能会导致您所描述的问题。

此外,在回发之间永远不会保留局部变量uploadFolder。您需要将其存储在会话中或页面上的某个位置。

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

答案 3 :(得分:0)

您需要在回发时重新设置DataSource,但不要重新绑定它,否则也会重置所选索引。