Showing posts with label bound. Show all posts
Showing posts with label bound. Show all posts

Saturday, March 31, 2012

set a file upload control to a data column within a formview edit item template

I have a formview with an edit template which contains a textbox which has "ProductImageUrl" data column bound to it. I want to bind this to a fileupload control instead so that an image can be selected. There is no text field for a fileupload control.

Any ideas?

Thanks

Andrew

Hi, Andrew

Do you want to get the full path of the image? If so, you can use <input id="file1" type="file" ...> then you can get the full path using fil1.value.

Hope this helps.


Is there a way of just getting the file name and not the full path. Can you give me an example of the code behind for using the fil1.value bit.

Thanks

Andrew


Hello

If you want filename, not the full path, you can useFileUpload control and FileUpload1.FileName is that you want.

As to the code, it's very easy.

<%@. Page Language="C#" AutoEventWireup="True" %>

<script runat="server">

void Button1_Click(object Source, EventArgs e)
{

if (Text1.Value == "")
{
Span1.InnerHtml = "Error: You must enter a file name." + File1.Value;
return;
}

if (File1.PostedFile.ContentLength > 0)
{
try
{
File1.PostedFile.SaveAs("c:\\temp\\" + Text1.Value);
Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
Text1.Value + "</b> on the Web server.";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
Text1.Value + "</b><br>" + exc.ToString() + ".";


}
}

}
void Click(Object src, EventArgs s)
{
Span1.InnerHtml = File1.Value;
}
</script>

<html>
<head>
<title>HtmlInputFile Example</title>
</head>
<body>

<h3>HtmlInputFile Example</h3>

<form id="Form1" enctype="multipart/form-data"
runat="server">

Select File to Upload:
<input id="File1"
type="file" onselect="Click"
runat="server">

<p>
Save as file name (no path):
<input id="Text1"
type="text"
runat="server">

</p>
<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />

</p>
<p>
<input type=button
id="Button1"
value="Upload"
onserverclick="Button1_Click"
runat="server">

</p>

</form>

</body>
</html>

Just click "browser" and then click "upload", the "span" will show the File1.Value.


You can get the file name with the following code

<input type="File" runat="server" ID="InputFile" />

To get the file name use the below line

InputFile.PostedFile.FileName


to get only the filename in a fullpath use

System.IO.Path.GetFileName(fullpath)

HC


The difficult part is that the textbox3 and fileupload controls are within a loginview and a detailsview so doing the findcontrol bit is twisting my melon considerably at the moment.

Any help gratefully received.

Thanks

Andrew


Here is my code

<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate></LoggedInTemplate>
<AnonymousTemplate>
You need to be logged in and be an Administrator to see this page.
</AnonymousTemplate>
<RoleGroups>
<asp:RoleGroup roles="Administrator">
<ContentTemplate>
<h3>Click Edit to view and edit product details and description</h3>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataSourceID="ObjectDataSource1" Height="50px" Width="100%" DataKeyNames="ProductItemID" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" AutoGenerateDeleteButton="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" SortExpression="ProductDescription" Visible="False" />
<asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" SortExpression="ProductPrice" />
<asp:TemplateField HeaderText="ProductImageUrl" SortExpression="ProductImageUrl">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProductImageUrl") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProductImageUrl") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductImageUrl") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductCategoryID" SortExpression="ProductCategoryID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ProductCategoryID") %>'></asp:TextBox>
cat id<br /> </EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ProductCategoryID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductCategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<FTB:FreeTextBox id="ProductDescriptionTextBox" runat="server" Text='<%# Bind("ProductDescription") %>'>
</FTB:FreeTextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>

</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductItemID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<img src="http://pics.10026.com/?src=images/<%# Eval("ProductImageUrl") %>" style="width:100px;" />

</ItemTemplate>
</asp:TemplateField>
</Fields>
<EmptyDataTemplate>
<strong>Product successfully deleted</strong>
</EmptyDataTemplate>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:DetailsView>
<br />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/editproducts.aspx">Go back to edit products</asp:HyperLink><br />
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="{0}"
SelectMethod="GetDataByProductItemID" TypeName="DataSet3TableAdapters.DataTable1TableAdapter" UpdateMethod="UpdateQuery" InsertMethod="InsertQuery" DeleteMethod="DeleteQuery">
<SelectParameters>
<asp:QueryStringParameter Name="ProductItemID" QueryStringField="ProductItemID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="ProductDescription" Type="String" />
<asp:Parameter Name="ProductPrice" Type="Int32" />
<asp:Parameter Name="ProductImageUrl" Type="String" />
<asp:Parameter Name="WebsiteCategoryID" Type="Int32" />
<asp:Parameter Name="ProductCategoryID" Type="Int32" />
<asp:Parameter Name="ProductItemID" Type="Int32" />

</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductCategoryID" Type="Int32" />
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="ProductDescription" Type="String" />
<asp:Parameter Name="ProductImageUrl" Type="String" />
<asp:Parameter Name="WebsiteCategoryID" Type="Int32" />
<asp:Parameter Name="ProductPrice" Type="Int32" />
</InsertParameters>
<DeleteParameters>
<asp:Parameter Name="ProductItemID" Type="Int32" />

</DeleteParameters>
</asp:ObjectDataSource>
<br />
<br />
<br />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>


</asp:LoginView>

Monday, March 26, 2012

Set default text on databound object

I have an insert template textbox that is bound to a field in SQL.
Since it is the insert template it really doesn't have anything in it.
How do I set the default text? Like for example if I have a insert
template field that is databound to a date sql column how can I set the
text to be a default like todays date?Use the ItemDataBound event. This event fires after the databinding occurs
for each Item, so it occurs the same number of times as the number of Items.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"MasterChief" <constants@.mix-net.net> wrote in message
news:1138744610.650024.116910@.o13g2000cwo.googlegroups.com...
>I have an insert template textbox that is bound to a field in SQL.
> Since it is the insert template it really doesn't have anything in it.
> How do I set the default text? Like for example if I have a insert
> template field that is databound to a date sql column how can I set the
> text to be a default like todays date?
>
I can't seem to find the ItemDataBound event. I can find it for the
gridview but not the formview which is where the insert template I am
using is. I am trying to use the databinding event is that the same
thing as ItemDataBound? Also how do I reference the textbox in the form
view? Like I can't see to go plan_startdateTextBox.text =
DateTime.Today.ToString()
Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)
End Sub
I searched for hours and finally came upon this posting if anybody is
interested.
http://groups.google.com/group/micr...ae822433a4ab209
Here is an example from code that I wrote:
Private Sub datResults_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datResults.ItemDataBound
If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblRegion1"), Label).Text = "*NONE*" Then
CType(e.Item.FindControl("lblRegion1"), Label).Text = " "
End If
If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblRegion2"), Label).Text = "*NONE*" Then
CType(e.Item.FindControl("lblRegion2"), Label).Text = " "
End If
If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblPhone1"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone1"), Label).Text = " "
If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblPhone2"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone2"), Label).Text = " "
End Sub
Notice that at the end of the first line it refers to the event as
datResults.ItemDataBound (this is the standard objectID.eventname format).
As for your question about referencing a textbox (or any other Control used
in your Template), you must use
CType(e.Item.FindControl(controlID),ControlType) to reference the Control.
This is necessary because since there are many Items in a DataList (for
example, my example will include many controls with the ID lblPhone1), you
cannot simply use just the ID. All my sample code is in VB.NET, if you have
any questions let me know.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"MasterChief" <constants@.mix-net.net> wrote in message
news:1138911157.928679.158000@.g47g2000cwa.googlegroups.com...
>I can't seem to find the ItemDataBound event. I can find it for the
> gridview but not the formview which is where the insert template I am
> using is. I am trying to use the databinding event is that the same
> thing as ItemDataBound? Also how do I reference the textbox in the form
> view? Like I can't see to go plan_startdateTextBox.text =
> DateTime.Today.ToString()
> Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
> Object, ByVal e As System.EventArgs)
> End Sub
>

Set default text on databound object

I have an insert template textbox that is bound to a field in SQL.
Since it is the insert template it really doesn't have anything in it.
How do I set the default text? Like for example if I have a insert
template field that is databound to a date sql column how can I set the
text to be a default like todays date?Use the ItemDataBound event. This event fires after the databinding occurs
for each Item, so it occurs the same number of times as the number of Items.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"MasterChief" <constants@.mix-net.net> wrote in message
news:1138744610.650024.116910@.o13g2000cwo.googlegr oups.com...
>I have an insert template textbox that is bound to a field in SQL.
> Since it is the insert template it really doesn't have anything in it.
> How do I set the default text? Like for example if I have a insert
> template field that is databound to a date sql column how can I set the
> text to be a default like todays date?
I can't seem to find the ItemDataBound event. I can find it for the
gridview but not the formview which is where the insert template I am
using is. I am trying to use the databinding event is that the same
thing as ItemDataBound? Also how do I reference the textbox in the form
view? Like I can't see to go plan_startdateTextBox.text =
DateTime.Today.ToString()

Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)

End Sub
I searched for hours and finally came upon this posting if anybody is
interested.
http://groups.google.com/group/micr...ae822433a4ab209
Here is an example from code that I wrote:

Private Sub datResults_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datResults.ItemDataBound

If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblRegion1"), Label).Text = "*NONE*" Then

CType(e.Item.FindControl("lblRegion1"), Label).Text = " "

End If

If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblRegion2"), Label).Text = "*NONE*" Then

CType(e.Item.FindControl("lblRegion2"), Label).Text = " "

End If

If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblPhone1"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone1"), Label).Text = " "

If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblPhone2"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone2"), Label).Text = " "

End Sub

Notice that at the end of the first line it refers to the event as
datResults.ItemDataBound (this is the standard objectID.eventname format).
As for your question about referencing a textbox (or any other Control used
in your Template), you must use
CType(e.Item.FindControl(controlID),ControlType) to reference the Control.
This is necessary because since there are many Items in a DataList (for
example, my example will include many controls with the ID lblPhone1), you
cannot simply use just the ID. All my sample code is in VB.NET, if you have
any questions let me know.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"MasterChief" <constants@.mix-net.net> wrote in message
news:1138911157.928679.158000@.g47g2000cwa.googlegr oups.com...
>I can't seem to find the ItemDataBound event. I can find it for the
> gridview but not the formview which is where the insert template I am
> using is. I am trying to use the databinding event is that the same
> thing as ItemDataBound? Also how do I reference the textbox in the form
> view? Like I can't see to go plan_startdateTextBox.text =
> DateTime.Today.ToString()
> Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
> Object, ByVal e As System.EventArgs)
> End Sub

Set DropDownList Items Alignment

Hi

How can I set my dropdownlist Itmes alignement right rether than left?

I bound them from a database??

my code

<asp:DropDownList width="100%" Runat="Server" ID="ddlCollege" DataTextField="CollegeName" Style="text-align: center;" DataValueField="CollegeID" Autopostback="True" OnSelectedIndexChanged="ShowDepartmentsEvents"/>

on the behind code I select values from the database??

Can Someone help??

thanks in advance

Setting style="text-align:right" on the dropdown list will work in Firefox, but I don't think there is any way to do this on Internet Explorer 6.

regards


I'm afraid you cannot do that unless you create you own dropdownlist :(

Hope my suggestion helps.