|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to bind a textbox to an a data sourceHi,
I want to bind a textbox (not in a gridview or detailsview or formview), just a regular textbox to a field in an AccessDataSource or SQLDataSource. How can I do that? -- Thanks Morris Morris Neuman schreef:
> Hi, Exactly what is an AccessDataSource?> > I want to bind a textbox (not in a gridview or detailsview or formview), > just a regular textbox to a field in an AccessDataSource or SQLDataSource. Under the assumption that you have class (eg: class A and an instance a) with a property (eg: B) you can bind to the Text property of a Textbox (eg: c) as following: c.DataBindings.Add("Text", a, "B"); If you create a datasource in the designer, you can also set this via the properties window -> DataBindings -> Text Morris Neuman wrote:
> Hi, The easiest way is still to put that textbox in the ItemTemplate of a > > I want to bind a textbox (not in a gridview or detailsview or > formview), just a regular textbox to a field in an AccessDataSource > or SQLDataSource. > > How can I do that? FormView, and binding the FormView to the datasource. -- Riki Thanks. Can the Textbox in the itemtemplate be bound to a datasource
differnt from the one bound to the FornView? I have done that with Dropdownlist control in a itemtemplate of a formview or gridview but can't seem to do that with a textbox control. Any suggestions? -- Show quoteThanks Morris "Riki" wrote: > Morris Neuman wrote: > > Hi, > > > > I want to bind a textbox (not in a gridview or detailsview or > > formview), just a regular textbox to a field in an AccessDataSource > > or SQLDataSource. > > > > How can I do that? > > The easiest way is still to put that textbox in the ItemTemplate of a > FormView, > and binding the FormView to the datasource. > > -- > > Riki > > > Morris Neuman wrote:
> Thanks. Can the Textbox in the itemtemplate be bound to a datasource That's because a dropdownlist is a list control.> differnt from the one bound to the FornView? I have done that with > Dropdownlist control in a itemtemplate of a formview or gridview but > can't seem to do that with a textbox control. Why do you want the textbox to have its own datasourcecontrol? Just bind it inside the itemtemplate to the data of the formview, and you're done. -- Riki Hi Morris,
For TextBox control, it is a simple databound webcontrol, you can simply bind its property with an object/value or function e.g. <asp:TextBox .... Text=<%# function or object %> /> For DataSource control, it is designed to support template based databound control or list databound control rather, so those simple controls like Label , Textbox doesn't directly support binding with it. Would you provide some further information about the scenario in your page? So far according to your description, I think it may be better that we use code to programmatically retrieve value and assign it to textbox(if it is already in a template control which bound to another datasource). Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. OK. I have 2 different scenario.
Scenario 1. I have a formview that I use to insert records. It adds the records to mailbox table (Access or SQL). So I have the formview bound to either AccessDataSource or SQLDataSource controls. I also have a table (mailboxdefault) that has default values for some of the mailbox fields. These can be changed by the system administrator. What I am trying to do is the following: When the formview control is displayed in insert mode, I want the fields to display the values from the mailboxdefault, allowing the user to change them if desired or leave the defaults as is, and then add the record to the mailbox table. Can I bind the textbox control in the inserttemplate to a different data source than the one use to bind the formview? Scenario 2. Can I bind a regular textbox control (not in a formview or gridview, etc) to a datasource so I can display a value from a table? Thanks to everyone for your help. -- Show quoteThanks Morris "Steven Cheng[MSFT]" wrote: > Hi Morris, > > For TextBox control, it is a simple databound webcontrol, you can simply > bind its property with an object/value or function e.g. > > <asp:TextBox .... Text=<%# function or object %> /> > > For DataSource control, it is designed to support template based databound > control or list databound control rather, so those simple controls like > Label , Textbox doesn't directly support binding with it. > > Would you provide some further information about the scenario in your page? > So far according to your description, I think it may be better that we use > code to programmatically retrieve value and assign it to textbox(if it is > already in a template control which bound to another datasource). > > Sincerely, > > Steven Cheng > > Microsoft MSDN Online Support Lead > > > This posting is provided "AS IS" with no warranties, and confers no rights. > > Morris Neuman wrote:
Show quote > OK. I have 2 different scenario. Since you're using the formview only for inserting, you can configure> > Scenario 1. > I have a formview that I use to insert records. It adds the records > to mailbox table (Access or SQL). So I have the formview bound to > either AccessDataSource or SQLDataSource controls. > > I also have a table (mailboxdefault) that has default values for some > of the mailbox fields. These can be changed by the system > administrator. > > What I am trying to do is the following: > When the formview control is displayed in insert mode, I want the > fields to display the values from the mailboxdefault, allowing the > user to change them if desired or leave the defaults as is, and then > add the record to the mailbox table. > > Can I bind the textbox control in the inserttemplate to a different > data source than the one use to bind the formview? the datasourcecontrol as follows: 1) create the datasource with the wizard in Visual Studio. Use the mailbox table. This will create the right InsertCommand value for what you want, but the SelectCommand will still point to the mailbox table, which is not what you want. 2) modify the SelectCommand in order to select the default values from the mailboxdefault table. This will produce what you want. > Scenario 2. Yes, but not to a datasourcecontrol (as far as I know).> Can I bind a regular textbox control (not in a formview or gridview, > etc) to a datasource so I can display a value from a table? You can write some code to databind your textbox when the page loads. -- Riki Thanks for your reply Morris and thanks for Riki's further suggestion.
For scenario1, I agree with Riki that if the FormView is used for inserting item only, you can consider manually change the "SelectStatement" of the datasource control so as to let it return values from the "DefaultMailBox" table. However, if your FormView will be used to both insert and update/display items, I'm afraid you have to manually use code to programmatically retrieve the default values from database and bind it to the textboxes in Inserttemplate(in PreRender event or any other place that can access the loaded controls in InsertTemplate). For scenario2, since TextBox can not directly bind to datasource control, I suggest you consider define some codebehind helper functions that retrieve the proper value(from database or ...) and use those helper function in your databinding expression. e.g. <asp:TextBox ..... Text="<%# GetValueFromHelper() %>" />.... Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. Thanks to everyone for all the help. I tried as suggested by Riki and
retrieved the default fields in the select statement used to bind the formview. Then I changed the field in the formview that I want to display the default value, to a template field. I bound the item template to the default value but the value does not show on the screen. I also bound the insert template to the default value but that did not work either. Question: If I want to display on the screen the mailboxdefault value but insert to the mailbox record, which template should I use for the display and which template should I use for the insert? I am only displaying the formview in insert mode when the user clicks on the add mailbox link button. -- Show quoteThanks Morris "Steven Cheng[MSFT]" wrote: > Thanks for your reply Morris and thanks for Riki's further suggestion. > > For scenario1, I agree with Riki that if the FormView is used for inserting > item only, you can consider manually change the "SelectStatement" of the > datasource control so as to let it return values from the "DefaultMailBox" > table. However, if your FormView will be used to both insert and > update/display items, I'm afraid you have to manually use code to > programmatically retrieve the default values from database and bind it to > the textboxes in Inserttemplate(in PreRender event or any other place that > can access the loaded controls in InsertTemplate). > > For scenario2, since TextBox can not directly bind to datasource control, I > suggest you consider define some codebehind helper functions that retrieve > the proper value(from database or ...) and use those helper function in > your databinding expression. e.g. > > <asp:TextBox ..... Text="<%# GetValueFromHelper() %>" />.... > > Sincerely, > > Steven Cheng > > Microsoft MSDN Online Support Lead > > > This posting is provided "AS IS" with no warranties, and confers no rights. > > The problem is that, in VS2003 using web forms, the text control had a
property - "Databinding" that simplified connecting the control to a datasource. It could be done visually during design and was very simple to modify. In VS2005, I want to do the same thing: I want to add a textbox to a form and then bind it to a datasource, and I want to do it visually, without having to go to the HTML source and add something like, Text='<%# DataBinder.Eval(HelpDeskDV, "[0].ContactName", "{0}") %>'. So the question is: how do I assign a datasource to an existing textbox on a web form? Regards, Fred *** Sent via Developersdex http://www.developersdex.com *** Hi,
Any ideas or thoughts on my 5/1 posting? Any help is most appreciated. -- Show quoteThanks Morris "Steven Cheng[MSFT]" wrote: > Thanks for your reply Morris and thanks for Riki's further suggestion. > > For scenario1, I agree with Riki that if the FormView is used for inserting > item only, you can consider manually change the "SelectStatement" of the > datasource control so as to let it return values from the "DefaultMailBox" > table. However, if your FormView will be used to both insert and > update/display items, I'm afraid you have to manually use code to > programmatically retrieve the default values from database and bind it to > the textboxes in Inserttemplate(in PreRender event or any other place that > can access the loaded controls in InsertTemplate). > > For scenario2, since TextBox can not directly bind to datasource control, I > suggest you consider define some codebehind helper functions that retrieve > the proper value(from database or ...) and use those helper function in > your databinding expression. e.g. > > <asp:TextBox ..... Text="<%# GetValueFromHelper() %>" />.... > > Sincerely, > > Steven Cheng > > Microsoft MSDN Online Support Lead > > > This posting is provided "AS IS" with no warranties, and confers no rights. > > Hi Morris,
I've investigated the FormView databinding model further and find that using the built-in "SelectCommand" does not help for this scenario. Since FormView will need to use two way databinding, the InsertCommand need to be set to the actual database table(you want to insert record). For the default table's values, I'm afraid you need to maually assign them to the TextBoxes in the "Inserttemplate". e.g. ================= protected void FormView1_PreRender(object sender, EventArgs e) { if (FormView1.CurrentMode == FormViewMode.Insert) { TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox; TextBox txt2 = FormView1.FindControl("descriptionTextBox") as TextBox; //replace default value with the one you retrieve from database txt1.Text = "default name.."; txt2.Text = "default description.."; } } ==================== Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. Hi Steven,
Thanks for your response and your help. The default value from is a field from a datasource control defined as a variable in my web.config AppSettings as MyDataSource. This control can point to either an AccessDataSource or SqlDataSource based on web.config. I also have the default value as a boundfield in my gridview control, however I have iit set to invisible. So the question is - in your comment you have txt1.Text = "default name.."; but in that case "default name is a literal. How do I set txt1.Text set to the value in either the database source or the boundfield in the gridview. Thanks again for your help. -- Show quoteThanks Morris "Steven Cheng[MSFT]" wrote: > Hi Morris, > > I've investigated the FormView databinding model further and find that > using the built-in "SelectCommand" does not help for this scenario. Since > FormView will need to use two way databinding, the InsertCommand need to be > set to the actual database table(you want to insert record). For the > default table's values, I'm afraid you need to maually assign them to the > TextBoxes in the "Inserttemplate". e.g. > > ================= > protected void FormView1_PreRender(object sender, EventArgs e) > { > if (FormView1.CurrentMode == FormViewMode.Insert) > { > TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox; > TextBox txt2 = FormView1.FindControl("descriptionTextBox") as > TextBox; > > //replace default value with the one you retrieve from database > txt1.Text = "default name.."; > txt2.Text = "default description.."; > > } > } > ==================== > > Sincerely, > > Steven Cheng > > Microsoft MSDN Online Support Lead > > > This posting is provided "AS IS" with no warranties, and confers no rights. > > > Thanks for your reply Morris,
If you need to get the data field value from DataSource control, you can also do it programmatically. ASP.NET 2.0 DataSource control provide method to let you explicitly perform the query and get the resultset. e.g. ========================= protected void FormView1_PreRender(object sender, EventArgs e) { if (FormView1.CurrentMode == FormViewMode.Insert) { TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox; TextBox txt2 = FormView1.FindControl("descriptionTextBox") as TextBox; IEnumerable result = SqlDataSource2.Select(DataSourceSelectArguments.Empty); DataView dv = result as DataView; string defaultname = (string)dv[0]["name"]; string defaultdesc = (string)dv[0]["description"]; txt1.Text = defaultname; txt2.Text = defaultdesc; //............ } } =============================== Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. Hi Steven,
Tried the code per your suggestion, however I get the following error: The type or namespace name 'DataView' could not be found (are you missing a using directive or an assembly reference? What do I need to change? Also in your post you specify SqlDataSource1. I use either SqlDataSource1 or AccessDataSource1 based on a variable MyDataSource from the AppSetting section of the web.config. How would I modify your statement to base selection on the MyDataSource app setting? Thanks as always for your help. -- Show quoteThanks Morris "Steven Cheng[MSFT]" wrote: > Thanks for your reply Morris, > > If you need to get the data field value from DataSource control, you can > also do it programmatically. ASP.NET 2.0 DataSource control provide method > to let you explicitly perform the query and get the resultset. e.g. > > > ========================= > protected void FormView1_PreRender(object sender, EventArgs e) > { > if (FormView1.CurrentMode == FormViewMode.Insert) > { > TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox; > TextBox txt2 = FormView1.FindControl("descriptionTextBox") as > TextBox; > > IEnumerable result = > SqlDataSource2.Select(DataSourceSelectArguments.Empty); > DataView dv = result as DataView; > > string defaultname = (string)dv[0]["name"]; > string defaultdesc = (string)dv[0]["description"]; > > txt1.Text = defaultname; > txt2.Text = defaultdesc; > > > //............ > > } > } > =============================== > > Sincerely, > > Steven Cheng > > Microsoft MSDN Online Support Lead > > > This posting is provided "AS IS" with no warranties, and confers no rights. > > > > Hi Morris,
As for the DataView class, it is under the System.Data namespace, have you imported the namespace in your code behind file? For SqlDataSouce and AccessDataSource, they've both implemented the "Select" method that can directly perform query in manual code and return the queried resultset. Make sure you set "DataSourceMode" of them to "DataSet"(the default value) so that the returned object from Selecct method would be a DataView object. For detecting whether it is SqlDataSource or AccessDataSource, I suggest you typeof() directive (in C#) to detect the type (use if...else block) to separate your code logic of the datasource control's converting. How do you think? Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. |
|||||||||||||||||||||||