ASP.NET DevExpress - 如何在另一个函数中获取ASPxComboBox的选定值

时间:2015-04-30 13:19:56

标签: c# asp.net devexpress

我有一个ASPxUploadControl项目(带有集成的上传按钮),然后是一个ASPxComboBox,如enter image description here

在第一个我需要浏览并选择一个项目(图片),然后我必须在第二个项目中选择一个应用程序,最后我需要上传它(点击下面的按钮)。 所以我为客户端做了这个:

<dx:ASPxUploadControl ID="ASPxUploadControl_Browse" runat="server" ShowUploadButton="True" AddUploadButtonsHorizontalPosition="Left"
                UploadMode="Auto" OnFileUploadComplete="UploadControl_FileUploadComplete" AllowedFileExtensions=".jpg,.jpeg,.gif,.png" Width="280px">
                <BrowseButton Text="Sfoglia" />
                <UploadButton Text="Carica"/>
</dx:ASPxUploadControl>
<dxe:ASPxComboBox ID="ASPxComboBox_Select" runat="server" DataSourceID="SqlDataSourceApplications"
                TextField="name" ValueField="applicationid" OnSelectedIndexChanged="ApplicationList_SelectedIndexChanged" Height="25px">
</dxe:ASPxComboBox>

然后是服务器端:

protected void ApplicationList_SelectedIndexChanged(object sender, EventArgs e)
{
    ASPxComboBox cb = (ASPxComboBox)sender;
    ASPxGridCustomers.FilterEnabled = true;
    ASPxGridCustomers.FilterExpression = "( applicationid = " + cb.SelectedItem.Value + ")";

    ASPxButtonAll.ClientEnabled = true;
}

protected void UploadControl_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
    try
    {
        FileInfo fileinfo = new FileInfo(e.UploadedFile.FileName);
        string s = "";
        try
        {
            s = ASPxComboBox_Select.Value.ToString();
        }
        catch (Exception ex) 
        {
            throw new Exception(ex.Message);
        }
        byte[] attachmentfile = GetBytes(fileinfo.Name);
        putDocumentToDB(selectedApp, attachmentfile);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

由于我需要将文件(以字节为单位)放在数据库中,我需要知道用户选择的应用程序。问题是我无法理解如何从函数UploadControl_FileUploadComplete中调用它。现在字符串完全是空的..

谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为UploadControl_FileUploadComplete是回调事件处理程序,而不是回发事件处理程序。

作为解决方法,尝试在会话变量中保存组合框选择,如下所示:

protected void ApplicationList_SelectedIndexChanged(object sender, EventArgs e)
{
    ASPxComboBox cb = (ASPxComboBox)sender;
    ASPxGridCustomers.FilterEnabled = true;
    ASPxGridCustomers.FilterExpression = "( applicationid = " + cb.SelectedItem.Value + ")";

    Session["cbSelectedValue"] = cb.SelectedItem.Value;

    ASPxButtonAll.ClientEnabled = true;
}

protected void UploadControl_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
    try
    {
        FileInfo fileinfo = new FileInfo(e.UploadedFile.FileName);
        string s = "";
        try
        {
            s = Session["cbSelectedValue"].ToString();
        }
        catch (Exception ex) 
        {
            throw new Exception(ex.Message);
        }
        byte[] attachmentfile = GetBytes(fileinfo.Name);
        putDocumentToDB(selectedApp, attachmentfile);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

// in case your combobox has a selected value on page load
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["cbSelectedValue"] = cb.SelectedItem.Value;
    }
}