开发者

DropDownList1.SelectedValue is null?

开发者 https://www.devze.com 2023-03-11 03:13 出处:网络
I cannot get anything other than a null value from my drop down box, im trying to upload files to different directories...

I cannot get anything other than a null value from my drop down box, im trying to upload files to different directories...

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, EventArg开发者_如何学JAVAs 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;
    }
}


Do a check before you access the Value property.

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


The dropdown has no values after postback. You are only binding at first page load, then the page posts back (index changed) and the items are not re-bound.


Do you have viewstate disabled on the page or any of the controls? This could cause the issue you are describing.

Also, the local variable uploadFolder will never be preserved between post backs. You need to store it in the session or on the page somewhere.

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


You need to re-set the DataSource on post back, but don't re-bind it or that will reset the selected index as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消