GridView Template field to browse explorer/directory

M

Morris Neuman

Hi,

I have a gridview on the contents page of my web form.

The GridView is bound to a datasource. I would like one of the fields when
in edit mode to display the contents of the bound field with the ability to
have a Browse button that the user can navigate, select a different file path
and name. When the update button clicked, I want the file path and name
selected to be save to the bound field I don't want the file to be uploaded
but just the selected file name saved.

I have tried the FileUpload control with the Enable and Visible properties
bound to the field. However when I click edit, I get error "Specified cast
is not valid".

I tried the HTMLInputFile control, however I don't see how to bind the field.

Is there another control that I should be using?

Appreciate any help.
 
A

Allen Chen [MSFT]

Hi Morris,

From your description you want to add a browse button in the EditTemplate
of the GridView, which acts like the <input type="file"/>. In addition, you
don't want to upload the file, correct?

If so it's not that easy. To not allow the file upload we have to use
another form. And to put the <input type="file"/> in the <table> we have to
put the input on the same form. So obviously it's a contradiction and
cannot be achieved.

A workaround I can suggest is to put the <input type="file"/> in another
form and set the absolute position to attach it on the <table>. Here's a
working sample (IE only. As far as I know it's impossible to create a cross
browser workaround. Maybe you can use Silverlight or Flash to achieve your
requirement.):

Aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

Inherits="_Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page</title>
<script type="text/javascript">
function AssignValueToInput()
{
var id=document.getElementById("Hidden2").value;

if(id!="")
{
var obj=document.getElementById(id);
if(obj)
{
obj.value=document.getElementById("File1").value + "";
}
}

}


function ShowAndPlaceHtmlInputFile()
{

var id=document.getElementById("Hidden1").value;
if(id!="")
{

var obj=document.getElementById(id);
if(obj){
var offsettop=getY(obj);
var offsetleft=getX(obj);
var div=document.getElementById("mydiv");
div.style.left=offsetleft;
div.style.top=offsettop;
div.style.display="inline";
}
}
}

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}
function getX( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

</script>

</head>

<body onload="ShowAndPlaceHtmlInputFile()">


<form id="form1" runat="server">
<input id="Hidden1" runat="server" type="hidden" value=""/>
<input id="Hidden2" runat="server" type="hidden" value=""/>
<div>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Test"
AutoGenerateColumns="false" DataSourceID="SqlDataSource1"
AutoGenerateEditButton="true" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%#Eval("Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("Name") %>'
style="z-index:2;" ></asp:TextBox>
<asp:Button ID="Button1" style=" visibility:hidden;" runat="server"
Text="Browse..." OnLoad="Button1_Load" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
DeleteCommand="DELETE FROM [Table1] WHERE [Test] = @test"
InsertCommand="INSERT INTO [Table1] ([Test], [Name]) VALUES (@test,
@Name)"
SelectCommand="SELECT * FROM [Table1]"
UpdateCommand="UPDATE [Table1] SET [Name] = @Name WHERE [Test] =
@test">
</asp:SqlDataSource>

</div>



</form>

<form id="form2" action="Default.aspx" style="z-index:1;">
<div id="mydiv" style="z-index:2; display:none; position:absolute;">
<input id="File1"type="file" onchange="AssignValueToInput()"
style="width:1px" />
</div>

</form>


</body>

</html>

Aspx.cs:

public partial class _Default : System.Web.UI.Page

{




protected void Button1_Load(object sender, EventArgs e)
{
Button b = (Button)sender;
this.Hidden1.Value = b.ClientID;
TextBox t = (TextBox)b.FindControl("TextBox1");
this.Hidden2.Value = t.ClientID;
}



}

As far as I can tell, you probably have to change the requirement (at
least, allow the file upload). Or else please use some RIA techniques such
as Silverlight or Flash.

You can get started to learn Silverlight here (Currently Silverlight is
still in RC0 but it will be released soon):
http://silverlight.net/

If you have further questions, just feel free to ask.

Regards,
Allen Chen
Microsoft Online Community Support


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

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://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Thread-Topic: GridView Template field to browse explorer/directory
| thread-index: AckpelLJxUTnQXZrQzuZDUFfLyOvaQ==
| From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| Subject: GridView Template field to browse explorer/directory
| Date: Wed, 8 Oct 2008 12:16:10 -0700
| Lines: 23
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:4057
| NNTP-Posting-Host: tk2msftsbfm01.phx.gbl 10.40.244.148
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| I have a gridview on the contents page of my web form.
|
| The GridView is bound to a datasource. I would like one of the fields
when
| in edit mode to display the contents of the bound field with the ability
to
| have a Browse button that the user can navigate, select a different file
path
| and name. When the update button clicked, I want the file path and name
| selected to be save to the bound field I don't want the file to be
uploaded
| but just the selected file name saved.
|
| I have tried the FileUpload control with the Enable and Visible
properties
| bound to the field. However when I click edit, I get error "Specified
cast
| is not valid".
|
| I tried the HTMLInputFile control, however I don't see how to bind the
field.
|
| Is there another control that I should be using?
|
| Appreciate any help.
| --
| Thanks
| Morris
|
 
A

Allen Chen [MSFT]

Hi Morris,

Have you tried my suggestions?

Regards,
Allen Chen
Microsoft Online Support
--------------------
| Thread-Topic: GridView Template field to browse explorer/directory
| thread-index: AckpelLJxUTnQXZrQzuZDUFfLyOvaQ==
| From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| Subject: GridView Template field to browse explorer/directory
| Date: Wed, 8 Oct 2008 12:16:10 -0700
| Lines: 23
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:4057
| NNTP-Posting-Host: tk2msftsbfm01.phx.gbl 10.40.244.148
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| I have a gridview on the contents page of my web form.
|
| The GridView is bound to a datasource. I would like one of the fields
when
| in edit mode to display the contents of the bound field with the ability
to
| have a Browse button that the user can navigate, select a different file
path
| and name. When the update button clicked, I want the file path and name
| selected to be save to the bound field I don't want the file to be
uploaded
| but just the selected file name saved.
|
| I have tried the FileUpload control with the Enable and Visible
properties
| bound to the field. However when I click edit, I get error "Specified
cast
| is not valid".
|
| I tried the HTMLInputFile control, however I don't see how to bind the
field.
|
| Is there another control that I should be using?
|
| Appreciate any help.
| --
| Thanks
| Morris
|
 
M

Morris Neuman

Hi,

My colleague tried your suggestion, unfortunately as neither one of us are
experts, we were not able to make it work.

As we have a master page and a content page, we were not too sure where to
place some of your code.

My colleague will email you as attachments the master page and the test
content page where we wish to show/use this browse button with additional
details.

If you can insert the required code in these pages and email back I could
test the code here and let you know the results.

Your help is very much appreciated.
 
A

Allen Chen [MSFT]

Hi,

Thanks for the code. I've sent a demo project to your colleague. Please
have a try and let me know if it works.

Regards,
Allen Chen
Microsoft Online Support
--------------------
| Thread-Topic: GridView Template field to browse explorer/directory
| thread-index: Acku42j14+Lp3GGxTtSkiSlPna+JTw==
| X-WBNR-Posting-Host: 65.55.12.11
| From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: GridView Template field to browse explorer/directory
| Date: Wed, 15 Oct 2008 09:31:01 -0700
| Lines: 87
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:4079
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| My colleague tried your suggestion, unfortunately as neither one of us
are
| experts, we were not able to make it work.
|
| As we have a master page and a content page, we were not too sure where
to
| place some of your code.
|
| My colleague will email you as attachments the master page and the test
| content page where we wish to show/use this browse button with additional
| details.
|
| If you can insert the required code in these pages and email back I could
| test the code here and let you know the results.
|
| Your help is very much appreciated.
| --
| Thanks
| Morris
|
|
| "Allen Chen [MSFT]" wrote:
|
| > Hi Morris,
| >
| > Have you tried my suggestions?
| >
| > Regards,
| > Allen Chen
| > Microsoft Online Support
| > --------------------
| > | Thread-Topic: GridView Template field to browse explorer/directory
| > | thread-index: AckpelLJxUTnQXZrQzuZDUFfLyOvaQ==
| > | From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| > | Subject: GridView Template field to browse explorer/directory
| > | Date: Wed, 8 Oct 2008 12:16:10 -0700
| > | Lines: 23
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | Path: TK2MSFTNGHUB02.phx.gbl
| > | Xref: TK2MSFTNGHUB02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:4057
| > | NNTP-Posting-Host: tk2msftsbfm01.phx.gbl 10.40.244.148
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi,
| > |
| > | I have a gridview on the contents page of my web form.
| > |
| > | The GridView is bound to a datasource. I would like one of the
fields
| > when
| > | in edit mode to display the contents of the bound field with the
ability
| > to
| > | have a Browse button that the user can navigate, select a different
file
| > path
| > | and name. When the update button clicked, I want the file path and
name
| > | selected to be save to the bound field I don't want the file to be
| > uploaded
| > | but just the selected file name saved.
| > |
| > | I have tried the FileUpload control with the Enable and Visible
| > properties
| > | bound to the field. However when I click edit, I get error
"Specified
| > cast
| > | is not valid".
| > |
| > | I tried the HTMLInputFile control, however I don't see how to bind
the
| > field.
| > |
| > | Is there another control that I should be using?
| > |
| > | Appreciate any help.
| > | --
| > | Thanks
| > | Morris
| > |
| >
| >
|
 
A

Allen Chen [MSFT]

Hi,

Do you have any progress on this issue?

Regards,
Allen Chen
Microsoft Online Support

--------------------
| Thread-Topic: GridView Template field to browse explorer/directory
| thread-index: Acku42j14+Lp3GGxTtSkiSlPna+JTw==
| X-WBNR-Posting-Host: 65.55.12.11
| From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: GridView Template field to browse explorer/directory
| Date: Wed, 15 Oct 2008 09:31:01 -0700
| Lines: 87
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:4079
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| My colleague tried your suggestion, unfortunately as neither one of us
are
| experts, we were not able to make it work.
|
| As we have a master page and a content page, we were not too sure where
to
| place some of your code.
|
| My colleague will email you as attachments the master page and the test
| content page where we wish to show/use this browse button with additional
| details.
|
| If you can insert the required code in these pages and email back I could
| test the code here and let you know the results.
|
| Your help is very much appreciated.
| --
| Thanks
| Morris
|
|
| "Allen Chen [MSFT]" wrote:
|
| > Hi Morris,
| >
| > Have you tried my suggestions?
| >
| > Regards,
| > Allen Chen
| > Microsoft Online Support
| > --------------------
| > | Thread-Topic: GridView Template field to browse explorer/directory
| > | thread-index: AckpelLJxUTnQXZrQzuZDUFfLyOvaQ==
| > | From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| > | Subject: GridView Template field to browse explorer/directory
| > | Date: Wed, 8 Oct 2008 12:16:10 -0700
| > | Lines: 23
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | Path: TK2MSFTNGHUB02.phx.gbl
| > | Xref: TK2MSFTNGHUB02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:4057
| > | NNTP-Posting-Host: tk2msftsbfm01.phx.gbl 10.40.244.148
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi,
| > |
| > | I have a gridview on the contents page of my web form.
| > |
| > | The GridView is bound to a datasource. I would like one of the
fields
| > when
| > | in edit mode to display the contents of the bound field with the
ability
| > to
| > | have a Browse button that the user can navigate, select a different
file
| > path
| > | and name. When the update button clicked, I want the file path and
name
| > | selected to be save to the bound field I don't want the file to be
| > uploaded
| > | but just the selected file name saved.
| > |
| > | I have tried the FileUpload control with the Enable and Visible
| > properties
| > | bound to the field. However when I click edit, I get error
"Specified
| > cast
| > | is not valid".
| > |
| > | I tried the HTMLInputFile control, however I don't see how to bind
the
| > field.
| > |
| > | Is there another control that I should be using?
| > |
| > | Appreciate any help.
| > | --
| > | Thanks
| > | Morris
| > |
| >
| >
|
 
M

Morris Neuman

Hi,

Sorry, we have not had a chance to try yet.

I will let you know if we have any additional questions and thank you for
your help.
--
Thanks
Morris


Allen Chen said:
Hi,

Do you have any progress on this issue?

Regards,
Allen Chen
Microsoft Online Support

--------------------
| Thread-Topic: GridView Template field to browse explorer/directory
| thread-index: Acku42j14+Lp3GGxTtSkiSlPna+JTw==
| X-WBNR-Posting-Host: 65.55.12.11
| From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: GridView Template field to browse explorer/directory
| Date: Wed, 15 Oct 2008 09:31:01 -0700
| Lines: 87
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:4079
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| My colleague tried your suggestion, unfortunately as neither one of us
are
| experts, we were not able to make it work.
|
| As we have a master page and a content page, we were not too sure where
to
| place some of your code.
|
| My colleague will email you as attachments the master page and the test
| content page where we wish to show/use this browse button with additional
| details.
|
| If you can insert the required code in these pages and email back I could
| test the code here and let you know the results.
|
| Your help is very much appreciated.
| --
| Thanks
| Morris
|
|
| "Allen Chen [MSFT]" wrote:
|
| > Hi Morris,
| >
| > Have you tried my suggestions?
| >
| > Regards,
| > Allen Chen
| > Microsoft Online Support
| > --------------------
| > | Thread-Topic: GridView Template field to browse explorer/directory
| > | thread-index: AckpelLJxUTnQXZrQzuZDUFfLyOvaQ==
| > | From: =?Utf-8?B?TW9ycmlzIE5ldW1hbg==?= <[email protected]>
| > | Subject: GridView Template field to browse explorer/directory
| > | Date: Wed, 8 Oct 2008 12:16:10 -0700
| > | Lines: 23
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | Path: TK2MSFTNGHUB02.phx.gbl
| > | Xref: TK2MSFTNGHUB02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:4057
| > | NNTP-Posting-Host: tk2msftsbfm01.phx.gbl 10.40.244.148
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi,
| > |
| > | I have a gridview on the contents page of my web form.
| > |
| > | The GridView is bound to a datasource. I would like one of the
fields
| > when
| > | in edit mode to display the contents of the bound field with the
ability
| > to
| > | have a Browse button that the user can navigate, select a different
file
| > path
| > | and name. When the update button clicked, I want the file path and
name
| > | selected to be save to the bound field I don't want the file to be
| > uploaded
| > | but just the selected file name saved.
| > |
| > | I have tried the FileUpload control with the Enable and Visible
| > properties
| > | bound to the field. However when I click edit, I get error
"Specified
| > cast
| > | is not valid".
| > |
| > | I tried the HTMLInputFile control, however I don't see how to bind
the
| > field.
| > |
| > | Is there another control that I should be using?
| > |
| > | Appreciate any help.
| > | --
| > | Thanks
| > | Morris
| > |
| >
| >
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top