RE: File Upload control not working in Datagrid
- From: Tyler Wilson <ikaikaw@xxxxxxxxxxxxxx>
- Date: Tue, 9 Jan 2007 13:10:01 -0800
Walter-
Thanks for the information. I am still not retrieving the file upload
control with the file still "attached". I got the code you sent to work (at
least it says there is a file "attached" to the control). however the page I
am working on is written in VB. Could there be a difference between the two
languages and being able to pull the file upload control from the datagrid?
Thanks for your insight.
--
Tyler Wilson
Colorado State University
"Walter Wang [MSFT]" wrote:
Hi Tyler,.
I'm not sure if the posted code is right or not, but I think in in
DataGrid's UpdateCommand event, e.Item is not the direct parent of the
FileUpload control if you're putting the FileUpload control inside the
EditItemTemplate of a TemplateColumn.
I've modified the sample code from
#Cutting Edge: DataGrid In-place Editing -- MSDN Magazine, June 2001
http://msdn.microsoft.com/msdnmag/issues/01/06/cutting/
a little to add a TemplateColumn with a FileUpload control to test the
function, the FileUpload.HasFile correctly returns true when the
UpdateCommand event is fired after a file is browsed:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<title>Editing Rows</title>
<style>
a {behavior:url(..\..\mouseover.htc);}
hr {height:2px;color:black;}
.StdText {font-family:verdana;font-size:9pt;font-weight:bold;}
.StdTextBox {font-family:verdana;font-size:9pt;border:solid 1px
black;filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2,
Color='gray', Positive='true');}
.Shadow {filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2,
OffY=2, Color='gray', Positive='true');}
</style>
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Initialize only the first time...
if (!Page.IsPostBack)
{
lblURL.Text = Request.Url + "<hr>";
}
}
public void OnLoadData(Object sender, EventArgs e)
{
UpdateView();
}
public void EditCommand(Object sender, DataGridCommandEventArgs e)
{
// Set the current item to edit mode
grid.EditItemIndex = e.Item.ItemIndex;
// Refresh the grid
UpdateView();
}
public void UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
// TODO: Retrieve new text and update the data source
FileUpload file1 = (FileUpload)FindControlRecursive(e.Item, "file1");
Response.Write(file1.HasFile);
// Reset the edit mode for the current item
grid.EditItemIndex = -1;
// Refresh the grid
UpdateView();
}
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
public void CancelCommand(Object sender, DataGridCommandEventArgs e)
{
// Reset the edit mode for the current item
grid.EditItemIndex = -1;
// Refresh the grid
UpdateView();
}
private void UpdateView()
{
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlDataAdapter da = new SqlDataAdapter(txtCommand.Text, conn);
DataSet ds = new DataSet();
da.Fill(ds, "MyTable");
// Bind the data
grid.DataSource = ds.Tables["MyTable"];
// Display the data
grid.DataBind();
}
</script>
<body bgcolor="ivory" style="font-family:arial;font-size:9pt">
<!-- ASP.NET topbar -->
<h2>Editing DataGrid's Items</h2>
<asp:Label runat="server" cssclass="StdText" font-bold="true">Current path:
</asp:label>
<asp:Label runat="server" id="lblURL" cssclass="StdText"
style="color:blue"></asp:label>
<form runat="server">
<table>
<tr>
<td><asp:label runat="server" text="Connection String"
cssclass="StdText" /></td>
<td><asp:textbox runat="server" id="txtConn"
Enabled="false"
cssclass="StdTextBox"
width="600px"
text="DATABASE=Northwind;SERVER=chnwawang01;Integrated Security=SSPI;"
/></td></tr>
<tr>
<td><asp:label runat="server" text="Command Text"
cssclass="StdText"/></td>
<td><asp:textbox runat="server" id="txtCommand"
Enabled="false"
width="600px"
cssclass="StdTextBox"
text="SELECT employeeid, titleofcourtesy, firstname, lastname, title,
country FROM Employees" /></td></tr></table>
<br><br>
<asp:linkbutton runat="server" text="Go get data..."
onclick="OnLoadData" />
<hr>
<asp:DataGrid id="grid" runat="server"
AutoGenerateColumns="false"
CssClass="Shadow" BackColor="white"
CellPadding="2" CellSpacing="0"
BorderStyle="solid" BorderColor="black" BorderWidth="1"
font-size="x-small" font-names="verdana"
OnEditCommand="EditCommand"
OnUpdateCommand="UpdateCommand"
OnCancelCommand="CancelCommand">
<AlternatingItemStyle BackColor="palegoldenrod" />
<ItemStyle BackColor="beige" />
<EditItemStyle BackColor="yellow" Font-Bold="true" />
<HeaderStyle ForeColor="white" BackColor="brown" HorizontalAlign="center"
Font-Bold="true" />
<columns>
<asp:BoundColumn runat="server" DataField="employeeid" HeaderText="ID"
Readonly="true"
DataFormatString="<span style='margin-left:5;margin-right:5'>{0}</span>"
<itemstyle backcolor="lightblue" font-bold="true" HorizontalAlign="right"
/>
</asp:BoundColumn>
<asp:TemplateColumn runat="server" HeaderText="Employee Name">
<itemtemplate>
<asp:label runat="server"
style="margin-left:5;margin-right:5"
Text='<%# DataBinder.Eval(Container.DataItem, "TitleOfCourtesy") + "<b>
" +
DataBinder.Eval(Container.DataItem, "LastName") + "</b>" + ", " +
DataBinder.Eval(Container.DataItem, "FirstName") %>' />
</itemtemplate>
</asp:TemplateColumn>
<asp:BoundColumn runat="server" DataField="title" HeaderText="Position"
/>
<asp:BoundColumn runat="server" DataField="country" HeaderText="From" />
<asp:TemplateColumn>
<EditItemTemplate>
<asp:FileUpload ID="file1" runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn runat="server"
EditText="Edit"
UpdateText="OK"
CancelText="Cancel">
<itemstyle BackColor="yellow" />
</asp:EditCommandColumn>
</columns>
</asp:DataGrid>
</form>
</body>
</html>
The code is written in C#; let me know if you need help to show an example
in VB.NET.
Sincerely,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
- Follow-Ups:
- RE: File Upload control not working in Datagrid
- From: Walter Wang [MSFT]
- RE: File Upload control not working in Datagrid
- References:
- RE: File Upload control not working in Datagrid
- From: Walter Wang [MSFT]
- RE: File Upload control not working in Datagrid
- Prev by Date: RE: File Upload control not working in Datagrid
- Next by Date: RE: File Upload control not working in Datagrid
- Previous by thread: RE: File Upload control not working in Datagrid
- Next by thread: RE: File Upload control not working in Datagrid
- Index(es):
Loading