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_SelectedIndexChange 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?
ANAT2403Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Darth_helgeCommented:
Do the databind only if the page is not postback

if not page.ispostback then
Formview1.Databind
end if
0
 
ANAT2403Author Commented:
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?
0
 
osiris247Commented:
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(textbox1.text); ??

or something different completly ??

o
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ANAT2403Author Commented:
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.CurrentCulture = 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_SelectedIndexChanged 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.Specialized;
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_Culture_glbl"] == "")
        {
            myCultInfo = Thread.CurrentThread.CurrentCulture;
            Session["Page_Culture_glbl"] = myCultInfo.Name;
        }
        Page_Culture = (string)Session["Page_Culture_glbl"];
        myCultInfo = new CultureInfo(Page_Culture, false);
        Thread.CurrentThread.CurrentCulture = 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(object sender, CommandEventArgs e)
    {
        TextBox temp22;
        temp22 = (TextBox)FormView1.FindControl("Subscruption_AmountTBFVEdit");
        Decimal tempdec = Decimal.Parse(temp22.Text, System.Globalization.NumberStyles.Currency);
        temp22.Text = tempdec.ToString();
    }

   
    protected void DDLFVEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Page_Culture;
        string Page_Culture_Item;
        DropDownList tempddl;
        tempddl = (DropDownList)FormView1.FindControl("DDLFVEdit");
        Page_Culture = tempddl.SelectedValue.ToString();
        Page_Culture_Item = tempddl.SelectedItem.ToString();
        CultureInfo myCIintl1 = new CultureInfo(Page_Culture, false);
        Thread.CurrentThread.CurrentCulture = 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;Integrated 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("@Candidate_id", SqlDbType.Int);
        cmd.Connection.Open();
        cmd.Parameters["@Candidate_id"].Value = 1005;
        string result = cmd.ExecuteScalar().ToString();
        // 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</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Anat_TestConnectionString %>"
            SelectCommand="SELECT Candidate_id, Subscruption_Amount, Currency_for_payment FROM Candidate WHERE (Candidate_id = @Candidate_id)"
            UpdateCommand="UPDATE Candidate &#13;&#10;SET&#13;&#10; Candidate_id = @Candidate_id,&#13;&#10; Subscruption_Amount = CONVERT(Money,@Subscruption_Amount) , &#13;&#10;Currency_for_payment = @Currency_for_payment&#13;&#10;WHERE (Candidate_id = @Candidate_id)&#13;&#10;">
            <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>
        &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="Candidate_id" DataSourceID="SqlDataSource1"
            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_AmountTBFVEdit" runat="server" Text='<%# Bind("Subscruption_Amount","{0:C}") %>' ></asp:TextBox><br />
                currency:<br />
                <asp:DropDownList ID="DDLFVEdit" runat="server" AutoPostBack="True" DataSourceID="SDSCurrency"
                    DataTextField="CurrencyName" DataValueField="CultureName" SelectedValue='<%# Bind("Currency_for_payment") %>'
                     Width="195px" OnSelectedIndexChanged="DDLFVEdit_SelectedIndexChanged"   >
                </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_AmountTBFVIns" runat="server" Text='<%# Bind("Subscruption_Amount","{0:C}") %>'>
                </asp:TextBox><br />
                Currency:<br />
                <asp:DropDownList ID="DDLFVIns" runat="server" AutoPostBack="True" DataSourceID="SDSCurrency"
                    DataTextField="CurrencyName" DataValueField="CultureName" 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_AmountLabelFV" runat="server" Text='<%# Bind("Subscruption_Amount", "{0:C}") %>'></asp:Label><br />
                currency:
                <asp:DropDownList ID="DDLFVSel" runat="server" DataSourceID="SDSCurrency" DataTextField="CurrencyName"
                    DataValueField="CultureName" 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>
        &nbsp;&nbsp;<br />

        <br />
        <asp:SqlDataSource ID="SDSCurrency" runat="server" ConnectionString="<%$ ConnectionStrings:Anat_TestConnectionString %>"
            SelectCommand="SELECT [CultureName], [CurrencyName] FROM [Currency]"></asp:SqlDataSource>
        &nbsp; &nbsp;
    </form>
</body>
</html>

ThankYou very much
Anat





0
 
aki4uCommented:
do you have viewstate enabled tor dropdownlist?
0
 
ANAT2403Author Commented:
I don't know. What I am suppose to do in order to have it enabled?
0
 
aki4uCommented:
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_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Page_Culture;
        string Page_Culture_Item;
        DropDownList tempddl;
        tempddl = (DropDownList)FormView1.FindControl("DDLFVEdit");
        Page_Culture = tempddl.SelectedValue.ToString();
        Page_Culture_Item = tempddl.SelectedItem.ToString();
        CultureInfo myCIintl1 = new CultureInfo(Page_Culture, false);
        Thread.CurrentThread.CurrentCulture = 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;
    }



0
 
ANAT2403Author Commented:
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

0
 
aki4uCommented:
this will work:

        int selIndex = tempddl .SelectedIndex;
        FormView1.DataBind();
        ClientScript.RegisterStartupScript(typeof(Page), "MyKey", "<script language=javascript>document.getElementById('" + tempddl .ClientID + "').selectedIndex = " + selIndex.ToString() + "</script>");
0
 
ANAT2403Author Commented:
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_selectedindexchanged, 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.
0
 
ANAT2403Author Commented:
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.
0
 
aki4uCommented:
I'm glad you have it working.
;)
0
 
aki4uCommented:
I resolved the problem, but then ANAT2403 asked about another problem which should be treated as new question.
0
 
GranModCommented:
Closed, no 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.