Hi Herlan,
When FileUpload control is inside UpdatePanel you need a Full PostBack when Button is clicked.
So instead of placing all controls inside Update Panel, place the DropDownList controls inside Update Panel to prevent page to full post back and place FileUpload control and Button outside the Update Panel.
Else assign the PostBackTrigger to Button control only and remove PostBackTrigger from DropDownList.
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="upCategory" runat="server">
<ContentTemplate>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<asp:Label ID="Label1" runat="server" Text="Category" Font-Bold="true"></asp:Label>
<asp:DropDownList ID="ddlCategory" runat="server" CssClass="form-control" AutoPostBack="true"
OnSelectedIndexChanged="ddlCategory_OnSelectedIndexChanged">
<asp:ListItem Text="Select" Value="0"></asp:ListItem>
<asp:ListItem Text="Beverages" Value="Beverages"></asp:ListItem>
<asp:ListItem Text="Seafood" Value="Seafood"></asp:ListItem>
</asp:DropDownList>
</div>
<div class="form-group">
<asp:Label ID="Label2" runat="server" Text="Sub Category" Font-Bold="true"></asp:Label>
<asp:DropDownList ID="ddlSubCategory" runat="server" CssClass="form-control" AutoPostBack="true"
OnSelectedIndexChanged="ddlSubCategory_OnSelectedIndexChanged">
</asp:DropDownList>
</div>
<div id="divOption" runat="server" class="form-group">
<asp:Label ID="Label3" runat="server" Text="You want to upload an image?" Font-Bold="true"></asp:Label>
<asp:RadioButtonList ID="rbHerramienta" runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No" Selected="True">No</asp:ListItem>
</asp:RadioButtonList>
</div>
<div id="divFileUpload" runat="server" class="form-group">
<asp:FileUpload ID="fuImage" runat="server" />
<asp:RegularExpressionValidator ID="regexValidator" runat="server" ControlToValidate="fuImage"
ErrorMessage="Only .jpg, .png y .jpeg." ValidationExpression="(.*?)\.(jpg|jpeg|png|JPG|JPEG|PNG)$">
</asp:RegularExpressionValidator>
</div>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
</asp:UpdatePanel>
Code
C#
protected void ddlCategory_OnSelectedIndexChanged(object sender, EventArgs e)
{
ddlSubCategory.Items.Clear();
ddlSubCategory.Items.Add(new ListItem { Text = "Select", Value = "0" });
if (ddlCategory.SelectedValue == "Beverages")
{
ddlSubCategory.Items.Add(new ListItem { Text = "Soft drinks", Value = "Soft drinks" });
ddlSubCategory.Items.Add(new ListItem { Text = "Coffees", Value = "Coffees" });
ddlSubCategory.Items.Add(new ListItem { Text = "Teas", Value = "Teas" });
}
else if (ddlCategory.SelectedValue == "Seafood")
{
ddlSubCategory.Items.Add(new ListItem { Text = "Seaweed", Value = "Seaweed" });
ddlSubCategory.Items.Add(new ListItem { Text = "Fish", Value = "Fish" });
}
}
protected void ddlSubCategory_OnSelectedIndexChanged(object sender, EventArgs e)
{
divOption.Visible = false;
divFileUpload.Visible = false;
if (ddlSubCategory.SelectedValue == "Seaweed")
{
divOption.Visible = true;
divFileUpload.Visible = true;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (fuImage.HasFile)
{
string fileName = System.IO.Path.GetFileName(fuImage.PostedFile.FileName);
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + fileName + "')", true);
}
}
VB.Net
Protected Sub ddlCategory_OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
ddlSubCategory.Items.Clear()
ddlSubCategory.Items.Add(New ListItem With {.Text = "Select", .Value = "0"})
If ddlCategory.SelectedValue = "Beverages" Then
ddlSubCategory.Items.Add(New ListItem With {.Text = "Soft drinks", .Value = "Soft drinks"})
ddlSubCategory.Items.Add(New ListItem With {.Text = "Coffees", .Value = "Coffees"})
ddlSubCategory.Items.Add(New ListItem With {.Text = "Teas", .Value = "Teas"})
ElseIf ddlCategory.SelectedValue = "Seafood" Then
ddlSubCategory.Items.Add(New ListItem With {.Text = "Seaweed", .Value = "Seaweed"})
ddlSubCategory.Items.Add(New ListItem With {.Text = "Fish", .Value = "Fish"})
End If
End Sub
Protected Sub ddlSubCategory_OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
divOption.Visible = False
divFileUpload.Visible = False
If ddlSubCategory.SelectedValue = "Seaweed" Then
divOption.Visible = True
divFileUpload.Visible = True
End If
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
If fuImage.HasFile Then
Dim fileName As String = System.IO.Path.GetFileName(fuImage.PostedFile.FileName)
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" + fileName + "')", True)
End If
End Sub
Screenshot