Monday, August 9, 2010

Set value to FileUpload Control programmatically

When you use asp.net FileUpload Control, you are not able to set any values to its TextBox Property. The following code demonstrate how to hack around this problem.

Note: it works only when you use tage <form runat="server">

Design Page (Default.aspx)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txbImageFile" runat="server"
Enabled="False" Width="430px"></asp:TextBox>
<asp:FileUpload ID="fileImageUpload" runat="server" Width="70px"
Enabled="True" EnableTheming="False"
onchange="this.form.txbImageFile.value=this.value" />
<br />
</form>
</body>
</html>


Code Behind (Default.aspx.cs)

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txbImageFile.Text = @"C:\text.txt";
}
}

0 comments:

Post a Comment