Tuesday, March 13, 2012

TextBox = database call. How?

I am reasonably competent at creating SqlDataSources, DropDownLists, GridViews etc. but I am drawing a blank as to how a TextBox would make a call to a SqlDataSource. Can a TextBox have a data source? How do you construct the code behind to execute a call to a database and then load a field value into the TextBox. Something like below?

protectedvoid DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
lblCCG_ID.Text =Convert.ToString(DropDownList1.SelectedValue);
- - - run call to database - - - Select Field3 Where Field6 = @dotnet.itags.org.CCG_ID;
TextBox1.Text = Eval("Field3");

- - -

}

Yes you can databind a TextBox but not like GridView or DataList for e.g. here is one way to do it.

//Declaratively - assume LoadValueFromDB() is a code behind method to retrieve data returns a string
//Instead of LoadValueFromDB method you could databind using a Datasource e.g. a custom object e.g. Order.OrderTotal.ToString()

<asp:TextBox id="Text1"Text="<%# LoadValueFromDB()%>"Width="200px"runat="server"/>
Also see thishttp://www.developerfusion.co.uk/show/4410/5/
Well you can call the db value at the text box text as:

text='<%# DataBinder.Eval(Container.DataItem, "commentID") %>' Well you can call the db value at the text box text as:text='<%# DataBinder.Eval(Container.DataItem, "commentID") %>'

0 comments:

Post a Comment