ANAT2403
asked on
databind in formview
In ASP.NET 2.0 C# I have a form with a Formview, Dropdownlist and a TextBox.
I change a value in the DropdownList and I nees the Textbox to refreshed.
In the DropdownList_SelectedIndex Change I do my checkings .
If there I doe Formview1.DataBind() I get the refresh I want in the TextBox but the dropdownlist
didn't get the new value but remain with the old one.
So I want to do a databind just to the TextBox but I get an error message :
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Where can I do the Databind or another command that will refresh the textbox?
I change a value in the DropdownList and I nees the Textbox to refreshed.
In the DropdownList_SelectedIndex
If there I doe Formview1.DataBind() I get the refresh I want in the TextBox but the dropdownlist
didn't get the new value but remain with the old one.
So I want to do a databind just to the TextBox but I get an error message :
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Where can I do the Databind or another command that will refresh the textbox?
ASKER
YOu didn't understand the problem:
Formview1.Databind does exactly what I want in the synchronizing the Textbox value but it lose the new value
of the dropdownlist. So I thought I will do the databind only for the textbox but this command is not allowed as I mentioned before in the event I am which is dropdownlist_selectedindex .
Have any other idea?
Formview1.Databind does exactly what I want in the synchronizing the Textbox value but it lose the new value
of the dropdownlist. So I thought I will do the databind only for the textbox but this command is not allowed as I mentioned before in the event I am which is dropdownlist_selectedindex
Have any other idea?
not sure i follow your problem 100%.
<quote>I get the refresh I want in the TextBox but the dropdownlist
didn't get the new value but remain with the old one.</quote>
Are you trying to change the value of the textbox when you change the selectedindex on the dropdown.....
onselectedindexchanged(etc etc)
textbox1.text = dropdownlist1.text;
or....
are you trying to type something in the textbox and it be added to the dropdown.
dropdown1.items.add(textbo x1.text); ??
or something different completly ??
o
<quote>I get the refresh I want in the TextBox but the dropdownlist
didn't get the new value but remain with the old one.</quote>
Are you trying to change the value of the textbox when you change the selectedindex on the dropdown.....
onselectedindexchanged(etc
textbox1.text = dropdownlist1.text;
or....
are you trying to type something in the textbox and it be added to the dropdown.
dropdown1.items.add(textbo
or something different completly ??
o
ASKER
OK. I'll explain it all and also give the kod.
I have a page with a formview with a textbox for price and a dropdownlist for currencies.
The price has a format for currency {0:c}.
The user can change the currency and the currency symbol next to the price should be change accordingly.
My dropdownlist consists of different currencies that are bind to a culture name.
When the user choose from the dropdownlist a new value (a new currency) for example he choose UK pound I know it is culture en-GB and then I change the culture of the page with the command:
myCultInfo = new CultureInfo("en-GB", false);
Thread.CurrentThread.Curre ntCulture = myCultInfo;
and then I will have the appropriate currency next to the price.
(This I discovered and decided to do after a lot of researching).
So after I get the new culture for the dropdownlist and change the new culture of the page I need the textbox of the price to be bind or refreshed. The textbox is in a formview. If I do formview.bind() it changes to the new currency symbol but I do it in the event of dropdownlist_SelectedIndex Changed and if I do it I loose the new value of the dropdownlist.
The question is where and how to make the textbox show the new currency symbol:
****************
The code behind:
**************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Web.UI.HtmlControls ;
// special namespaces for globalizations
using System.Globalization;
using System.Text;
using System.Threading;
using System.Collections.Special ized;
using System.Data.SqlClient;
public partial class testcurrencyBind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ // get the currency from input according to culture name
Session["Page_Culture_glbl "] = getCulture();
set_Culture();
}
set_Culture();
}
/// <summary>
/// set the current culture according to input or changed dropdownlist
/// </summary>
private void set_Culture()
{
string Page_Culture;
CultureInfo myCultInfo;
if ((string)Session["Page_Cul ture_glbl" ] == "")
{
myCultInfo = Thread.CurrentThread.Curre ntCulture;
Session["Page_Culture_glbl "] = myCultInfo.Name;
}
Page_Culture = (string)Session["Page_Cult ure_glbl"] ;
myCultInfo = new CultureInfo(Page_Culture, false);
Thread.CurrentThread.Curre ntCulture = myCultInfo;
}
/// <summary>
/// this function removes the currency symbol before updating the datbase
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UpdateButtonFV_Command(obj ect sender, CommandEventArgs e)
{
TextBox temp22;
temp22 = (TextBox)FormView1.FindCon trol("Subs cruption_A mountTBFVE dit");
Decimal tempdec = Decimal.Parse(temp22.Text, System.Globalization.Numbe rStyles.Cu rrency);
temp22.Text = tempdec.ToString();
}
protected void DDLFVEdit_SelectedIndexCha nged(objec t sender, EventArgs e)
{
string Page_Culture;
string Page_Culture_Item;
DropDownList tempddl;
tempddl = (DropDownList)FormView1.Fi ndControl( "DDLFVEdit ");
Page_Culture = tempddl.SelectedValue.ToSt ring();
Page_Culture_Item = tempddl.SelectedItem.ToStr ing();
CultureInfo myCIintl1 = new CultureInfo(Page_Culture, false);
Thread.CurrentThread.Curre ntCulture = myCIintl1;
Session["Page_Culture_glbl "] = Page_Culture;
// this command deletes the new value of this DDLFVEdit dropdownlist
FormView1.DataBind();
}
/// <summary>
/// this function get the Currency_for_payment for the current record which is actuallt a culture name
/// </summary>
/// <returns></returns>
private string getCulture()
{
// Define the connection and the command, and the command type
SqlConnection conn = new SqlConnection("Data Source=ANAT2005;Initial Catalog=IIB_Data;Integrate d Security=True");
SqlCommand cmd = new SqlCommand("select Currency_for_payment from candidate WHERE (Candidate_id = @Candidate_id", conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@Candi date_id", SqlDbType.Int);
cmd.Connection.Open();
cmd.Parameters["@Candidate _id"].Valu e = 1005;
string result = cmd.ExecuteScalar().ToStri ng();
// return the culture name
cmd.Connection.Close();
return result;
}
}
**********
The page
**********
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcurrencyBind .aspx.cs" Inherits="testcurrencyBind " %>
<!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>exampgridview2</tit le>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Anat_Tes tConnectio nString %>"
SelectCommand="SELECT Candidate_id, Subscruption_Amount, Currency_for_payment FROM Candidate WHERE (Candidate_id = @Candidate_id)"
UpdateCommand="UPDATE Candidate SET Candidate_id = @Candidate_id, Subscruption_Amount = CONVERT(Money,@Subscruptio n_Amount) , Currency_for_pay ment = @Currency_for_payment WHERE (Candidate_id = @Candidate_id) ">
<SelectParameters>
<asp:Parameter DefaultValue="1005" Name="Candidate_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Candidate_id" Type="Int32" />
<asp:Parameter Name="Subscruption_Amount" Type="Decimal"/>
<asp:Parameter Name="Currency_for_payment " />
</UpdateParameters>
</asp:SqlDataSource>
</div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="Candidate_id " DataSourceID="SqlDataSourc e1"
Style="z-index: 102; left: 379px; position: absolute; top: 2px" Height="186px" Width="299px" >
<EditItemTemplate>
Candidate_id:
<asp:Label ID="Candidate_idLabel1" runat="server" Text='<%# Eval("Candidate_id") %>'>
</asp:Label><br />
Subscruption_Amount:
<asp:TextBox ID="Subscruption_AmountTBF VEdit" runat="server" Text='<%# Bind("Subscruption_Amount" ,"{0:C}") %>' ></asp:TextBox><br />
currency:<br />
<asp:DropDownList ID="DDLFVEdit" runat="server" AutoPostBack="True" DataSourceID="SDSCurrency"
DataTextField="CurrencyNam e" DataValueField="CultureNam e" SelectedValue='<%# Bind("Currency_for_payment ") %>'
Width="195px" OnSelectedIndexChanged="DD LFVEdit_Se lectedInde xChanged" >
</asp:DropDownList>
<br />
<br />
<asp:LinkButton ID="UpdateButtonFV" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" OnCommand="UpdateButtonFV_ Command" >
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Candidate_id:
<asp:TextBox ID="Candidate_idTextBox" runat="server" Text='<%# Bind("Candidate_id") %>'>
</asp:TextBox><br />
Subscruption_Amount:
<asp:TextBox ID="Subscruption_AmountTBF VIns" runat="server" Text='<%# Bind("Subscruption_Amount" ,"{0:C}") %>'>
</asp:TextBox><br />
Currency:<br />
<asp:DropDownList ID="DDLFVIns" runat="server" AutoPostBack="True" DataSourceID="SDSCurrency"
DataTextField="CurrencyNam e" DataValueField="CultureNam e" SelectedValue='<%# Bind("Currency_for_payment ") %>'
Width="156px">
</asp:DropDownList>
<br />
<asp:LinkButton ID="InsertButtonFV" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
Candidate_id:
<asp:Label ID="Candidate_idLabel" runat="server" Text='<%# Eval("Candidate_id") %>'>
</asp:Label><br />
Subscruption_Amount:
<asp:Label ID="Subscruption_AmountLab elFV" runat="server" Text='<%# Bind("Subscruption_Amount" , "{0:C}") %>'></asp:Label><br />
currency:
<asp:DropDownList ID="DDLFVSel" runat="server" DataSourceID="SDSCurrency" DataTextField="CurrencyNam e"
DataValueField="CultureNam e" Enabled="False" SelectedValue='<%# Bind("Currency_for_payment ") %>'
Width="120px" AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:LinkButton ID="EditButtonFV" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<br />
<br />
<asp:SqlDataSource ID="SDSCurrency" runat="server" ConnectionString="<%$ ConnectionStrings:Anat_Tes tConnectio nString %>"
SelectCommand="SELECT [CultureName], [CurrencyName] FROM [Currency]"></asp:SqlDataS ource>
</form>
</body>
</html>
ThankYou very much
Anat
I have a page with a formview with a textbox for price and a dropdownlist for currencies.
The price has a format for currency {0:c}.
The user can change the currency and the currency symbol next to the price should be change accordingly.
My dropdownlist consists of different currencies that are bind to a culture name.
When the user choose from the dropdownlist a new value (a new currency) for example he choose UK pound I know it is culture en-GB and then I change the culture of the page with the command:
myCultInfo = new CultureInfo("en-GB", false);
Thread.CurrentThread.Curre
and then I will have the appropriate currency next to the price.
(This I discovered and decided to do after a lot of researching).
So after I get the new culture for the dropdownlist and change the new culture of the page I need the textbox of the price to be bind or refreshed. The textbox is in a formview. If I do formview.bind() it changes to the new currency symbol but I do it in the event of dropdownlist_SelectedIndex
The question is where and how to make the textbox show the new currency symbol:
****************
The code behind:
**************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls
// special namespaces for globalizations
using System.Globalization;
using System.Text;
using System.Threading;
using System.Collections.Special
using System.Data.SqlClient;
public partial class testcurrencyBind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ // get the currency from input according to culture name
Session["Page_Culture_glbl
set_Culture();
}
set_Culture();
}
/// <summary>
/// set the current culture according to input or changed dropdownlist
/// </summary>
private void set_Culture()
{
string Page_Culture;
CultureInfo myCultInfo;
if ((string)Session["Page_Cul
{
myCultInfo = Thread.CurrentThread.Curre
Session["Page_Culture_glbl
}
Page_Culture = (string)Session["Page_Cult
myCultInfo = new CultureInfo(Page_Culture, false);
Thread.CurrentThread.Curre
}
/// <summary>
/// this function removes the currency symbol before updating the datbase
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UpdateButtonFV_Command(obj
{
TextBox temp22;
temp22 = (TextBox)FormView1.FindCon
Decimal tempdec = Decimal.Parse(temp22.Text,
temp22.Text = tempdec.ToString();
}
protected void DDLFVEdit_SelectedIndexCha
{
string Page_Culture;
string Page_Culture_Item;
DropDownList tempddl;
tempddl = (DropDownList)FormView1.Fi
Page_Culture = tempddl.SelectedValue.ToSt
Page_Culture_Item = tempddl.SelectedItem.ToStr
CultureInfo myCIintl1 = new CultureInfo(Page_Culture, false);
Thread.CurrentThread.Curre
Session["Page_Culture_glbl
// this command deletes the new value of this DDLFVEdit dropdownlist
FormView1.DataBind();
}
/// <summary>
/// this function get the Currency_for_payment for the current record which is actuallt a culture name
/// </summary>
/// <returns></returns>
private string getCulture()
{
// Define the connection and the command, and the command type
SqlConnection conn = new SqlConnection("Data Source=ANAT2005;Initial Catalog=IIB_Data;Integrate
SqlCommand cmd = new SqlCommand("select Currency_for_payment from candidate WHERE (Candidate_id = @Candidate_id", conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@Candi
cmd.Connection.Open();
cmd.Parameters["@Candidate
string result = cmd.ExecuteScalar().ToStri
// return the culture name
cmd.Connection.Close();
return result;
}
}
**********
The page
**********
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcurrencyBind
<!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>exampgridview2</tit
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Anat_Tes
SelectCommand="SELECT Candidate_id, Subscruption_Amount, Currency_for_payment FROM Candidate WHERE (Candidate_id = @Candidate_id)"
UpdateCommand="UPDATE Candidate SET Candidate_id = @Candidate_id, Subscruption_Amount = CONVERT(Money,@Subscruptio
<SelectParameters>
<asp:Parameter DefaultValue="1005" Name="Candidate_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Candidate_id" Type="Int32" />
<asp:Parameter Name="Subscruption_Amount"
<asp:Parameter Name="Currency_for_payment
</UpdateParameters>
</asp:SqlDataSource>
</div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="Candidate_id
Style="z-index: 102; left: 379px; position: absolute; top: 2px" Height="186px" Width="299px" >
<EditItemTemplate>
Candidate_id:
<asp:Label ID="Candidate_idLabel1" runat="server" Text='<%# Eval("Candidate_id") %>'>
</asp:Label><br />
Subscruption_Amount:
<asp:TextBox ID="Subscruption_AmountTBF
currency:<br />
<asp:DropDownList ID="DDLFVEdit" runat="server" AutoPostBack="True" DataSourceID="SDSCurrency"
DataTextField="CurrencyNam
Width="195px" OnSelectedIndexChanged="DD
</asp:DropDownList>
<br />
<br />
<asp:LinkButton ID="UpdateButtonFV" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" OnCommand="UpdateButtonFV_
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Candidate_id:
<asp:TextBox ID="Candidate_idTextBox" runat="server" Text='<%# Bind("Candidate_id") %>'>
</asp:TextBox><br />
Subscruption_Amount:
<asp:TextBox ID="Subscruption_AmountTBF
</asp:TextBox><br />
Currency:<br />
<asp:DropDownList ID="DDLFVIns" runat="server" AutoPostBack="True" DataSourceID="SDSCurrency"
DataTextField="CurrencyNam
Width="156px">
</asp:DropDownList>
<br />
<asp:LinkButton ID="InsertButtonFV" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
Candidate_id:
<asp:Label ID="Candidate_idLabel" runat="server" Text='<%# Eval("Candidate_id") %>'>
</asp:Label><br />
Subscruption_Amount:
<asp:Label ID="Subscruption_AmountLab
currency:
<asp:DropDownList ID="DDLFVSel" runat="server" DataSourceID="SDSCurrency"
DataValueField="CultureNam
Width="120px" AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:LinkButton ID="EditButtonFV" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<br />
<br />
<asp:SqlDataSource ID="SDSCurrency" runat="server" ConnectionString="<%$ ConnectionStrings:Anat_Tes
SelectCommand="SELECT [CultureName], [CurrencyName] FROM [Currency]"></asp:SqlDataS
</form>
</body>
</html>
ThankYou very much
Anat
do you have viewstate enabled tor dropdownlist?
ASKER
I don't know. What I am suppose to do in order to have it enabled?
never mind that...using FormView1.DataBind method resolves all data-binding expressions in the active template of the control. So, the only way I can think of is to keep the dropdownlist.selectedindex same is to remmember it before you call FormView1.DataBind and the line after that assignt it back.
something like this:
protected void DDLFVEdit_SelectedIndexCha nged(objec t sender, EventArgs e)
{
string Page_Culture;
string Page_Culture_Item;
DropDownList tempddl;
tempddl = (DropDownList)FormView1.Fi ndControl( "DDLFVEdit ");
Page_Culture = tempddl.SelectedValue.ToSt ring();
Page_Culture_Item = tempddl.SelectedItem.ToStr ing();
CultureInfo myCIintl1 = new CultureInfo(Page_Culture, false);
Thread.CurrentThread.Curre ntCulture = myCIintl1;
Session["Page_Culture_glbl "] = Page_Culture;
// this command deletes the new value of this DDLFVEdit dropdownlist
int selIndex = tempddl.SelectedIndex;
FormView1.DataBind();
tempddl.SelectedIndex = selIndex;
}
something like this:
protected void DDLFVEdit_SelectedIndexCha
{
string Page_Culture;
string Page_Culture_Item;
DropDownList tempddl;
tempddl = (DropDownList)FormView1.Fi
Page_Culture = tempddl.SelectedValue.ToSt
Page_Culture_Item = tempddl.SelectedItem.ToStr
CultureInfo myCIintl1 = new CultureInfo(Page_Culture, false);
Thread.CurrentThread.Curre
Session["Page_Culture_glbl
// this command deletes the new value of this DDLFVEdit dropdownlist
int selIndex = tempddl.SelectedIndex;
FormView1.DataBind();
tempddl.SelectedIndex = selIndex;
}
ASKER
OK this is exactly the problem!!! the value of the tempddl does not change in the slectedchanged. I remain with the new value to the end of the function. but in another place afterwords it changes the value. maybe I put the tempddl.SelectedIndex = selIndex; also in another place? but where
Thanks
Anat
Thanks
Anat
this will work:
int selIndex = tempddl .SelectedIndex;
FormView1.DataBind();
ClientScript.RegisterStart upScript(t ypeof(Page ), "MyKey", "<script language=javascript>docume nt.getElem entById('" + tempddl .ClientID + "').selectedIndex = " + selIndex.ToString() + "</script>");
int selIndex = tempddl .SelectedIndex;
FormView1.DataBind();
ClientScript.RegisterStart
ASKER
I found some interesting things:
first, your new suggestion realy keeps the new value of the dropdownlist after the databind but ....when I do the
update, the old value comes back and again I loose the new one. but listen what I discovered:
Its not a problem of dropdownlist at all!!! If I have in my page more fields of textbox for example and I gave
them new values - then when I do the databind in the event of the dropdownlist_selectedindex changed, I also loose
their values!!! So lets think: To make the formview.databind() in other place - its impossible , I tried and in many places I am not allowed and I get error. The question is why I need the formview.databind? I need it in order
that after I changed the culture I want the new currency to take effect on the amount field textbox and to have the new currency symbol. I need to do something just for this, just for my amount field textbox. How and where can I do it.
Can you help?
Thankyou.
first, your new suggestion realy keeps the new value of the dropdownlist after the databind but ....when I do the
update, the old value comes back and again I loose the new one. but listen what I discovered:
Its not a problem of dropdownlist at all!!! If I have in my page more fields of textbox for example and I gave
them new values - then when I do the databind in the event of the dropdownlist_selectedindex
their values!!! So lets think: To make the formview.databind() in other place - its impossible , I tried and in many places I am not allowed and I get error. The question is why I need the formview.databind? I need it in order
that after I changed the culture I want the new currency to take effect on the amount field textbox and to have the new currency symbol. I need to do something just for this, just for my amount field textbox. How and where can I do it.
Can you help?
Thankyou.
ASKER
I decided to solve the problem without using the databind.
I will change the values and only when I press the update button I expect the currency to be changed.
but this is not enough because during the update I have to remove the currency format and only after I do that
I can set the new culture. It works ok.
I will change the values and only when I press the update button I expect the currency to be changed.
but this is not enough because during the update I have to remove the currency format and only after I do that
I can set the new culture. It works ok.
I'm glad you have it working.
;)
;)
I resolved the problem, but then ANAT2403 asked about another problem which should be treated as new question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if not page.ispostback then
Formview1.Databind
end if