How to add datasource to textbox in visual studio 2015 - c#

In my form I have drop down list that displays products
and a textbox that should display the price for the selected product
My question is how can I add datasource into the textbox ?

You can add a Event-Handler to the drop-down list that gets called, when the user selects a different value in the drop-down list.
Then simply change the value of the textbox in the event-handler method.
Hope this helps, please comment if you need more information.

I solved the problem !
I used datalist insted of textbox and I configured its datasource
to retrive the price for any chosen product from the drop down list
<ItemTemplate>
Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>

Related

Set Focus for ItemTemplate textbox in grid view for asp.net c#

I have one query which is related to get focus in itemfield of Textbox in gridview. Based on checkbox selection populating different number of textBoxes and after entered data in last textbox and making webservice call based on response populating gridview with itemField(textboxes).
The scenario is explaining below.
CheckBox(Unchecked): False
Number of text boxes(ID): 1
CheckBox(Checked): True
Number of text boxes(ID, AccNo, Price): 3
After entered values in last textboxes, make webservice call and filling gridview with data but it's not get focus of first textbox in GridView.
<asp:TemplateField>
<HeaderTemplate>
<p>Paid Amount</p>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAmnt" runat="server" TabIndex="1"
onkeypress="return isNumberKey(event,this)" onblur="calAmount(event,this)" Text='<%# Eval("PaidAmount") %>'>
</asp:TextBox>
</ItemTemplate>
I have not any event related gridview making like, rowBound, CommandView. Could you please show me a way related this.
Thank you so much in advance.

How to Bind related Dropdownlist in gridview

I have a Grid view, whenever I select 1st dropdownlist i.e. subject i want to bind related teachers name on second dropdownlist only one subject and teacher will updated dropdownlist is inside itemtemplate.
eg
<asp:TemplateField>
<HeaderTemplate>
Friday
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlSubjectFr" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlTeacherFr" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
If u want u change the Drop down list value of second based on first Drop down list selected value.. that should write in selected Index changed event with auto post back = "true"

Data not displaying from my database

It should be so freakin' easy, but why is my data not displaying? I even used the AccessDataSource wizard to configure the datasource and I still can't get it to show what is there. Here is my markup:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/TravelJoansDB.mdb"
SelectCommand="SELECT ID, Destinations FROM [Destinations]">
</asp:AccessDataSource>
<asp:Label ID="CurrentDestLabel" runat="server" Text="What is currently displayed:" />
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Destinations") %>'/>
I don't get any errors or anything, just nothing displays for my "Label1". Any thoughts?
I am not quite sure, but I think you try to show a whole List in a single label...
perhaps your command need something like "Select top 1 Destinations FROM [Destinations]"
So you make sure, that you have only one Element selected...
Make sure to include any databound controls in a DetailsView, FormView, Gridview, etc...

Select an item in a dropdownlist in a listview

I have a listview which is databound by a list of objects.
In the listview, i have a dropdownlist on each item. Which is filled in the .._itemcreated event.
<asp:ListView ID="ListList" runat="server">
<ItemTemplate>
<asp:TextBox ID="ListItem" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:DropDownList ID="ddlAccountManagers" AutoPostBack="True" runat="server" />
<br />
</ItemTemplate>
</asp:ListView>
Depending on which item, i have to set the selectedvalue of the dropdown. But how do I do this?
How do I access the current items values in the itemcreated event?
Since you are able to fill the dropdownlist, I assume you already have access to it.
ddlAccountManagers.Items.FindByText("TextToSelect").Selected = True
or
ddlAccountManagers.Items.FindByValue("ValueToSelect").Selected = True
You could try this one:
ddlAccountManagers.SelectedValue="value you want to be selected"
In the list of objects you have, I suppose that each object will be associated with an AccountManager. An AccoutManager logically would have an id, that will distinguish him/her from the rest of account managers. Then you have to put this value as the selected value.

Error when displaying data in a datalist from sqldatasource in asp project

I am building a website for a class and have come across an error that I can not find the solution to. I have a dropdownlist on the page that displays the cutomer name and sets the selected value to the customer id selected from a sqldatasource (data tables from access database - fig1) (this works fine as I have tested it before I added the second sqldatasource). I add a datalist control to the page and a second sqldatasource for the datalist to bind to. I configured the datasource using the configure data source wizard as shown in fig2. I then use the wizard to test the data returned by the query and see that it works, the wizard shows that the data that I wanted is returned. So I bind the datalist to the data source and now the itemtemplate (in the html source view) contains data bound labels that will show the values:
Products.Name:
<asp:Label ID="Products_NameLabel" runat="server" Text='<%# Eval("Products.Name") %>' />
<br />
Technicians.Name:
<asp:Label ID="Technicians_NameLabel" runat="server" Text='<%# Eval("Technicians.Name") %>' />
<br />
Incidents.DateOpened:
<asp:Label ID="Incidents_DateOpenedLabel" runat="server" Text='<%# Eval("Incidents.DateOpened") %>' />
<br />
Incidents.DateClosed:
<asp:Label ID="Incidents_DateClosedLabel" runat="server" Text='<%# Eval("Incidents.DateClosed") %>' />
<br />
Incidents.Description:
<asp:Label ID="Incidents_DescriptionLabel" runat="server" Text='<%# Eval("Incidents.Description") %>' />
The labels are now bound to the data returned from the second source....or so it would seem. When I run it and select a customer that should return the data that is being selected in the sqldatasource select statement, my program crashes and gives this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Products'.
I do not understand why it is saying that the datarowview does not contain a column with that name when the sqldatasource clearly returns it when I test it in the wizard. Can anyone please help?
P.S. Sorry for the links to imgur...apparently you have to have a higher rep to post pictures
I tried this and the generated HTML was NOT prefixed with the table name like in your code.
I hand-added the table name and reproduced your error.
So, if you have duplicate column names like you do, you need to modify the SQL statement to use an alias:
Products.Name as Products_Name

Categories