Populate ASP.NET textbox from combobox selected index change - asp.net

I am very new to ASP.NET.
I have a an ASP.NET page with an AJAX Combobox and a TextBox. The combobox populates from a database and has value of ID and displays Name. The textbox should display Address. All i want to do is change the value in the Address textbox when the index on the Name combo box changes.
And then be able to change the address and save it back to the database.
How do I do this (simple?) task?
Code so far...
<form id="form1" runat="server">
<div>
<asp:ComboBox ID="ComboBox1" runat="server" DataSourceID="AccessDataSource1" DataTextField="CompositeName"
DataValueField="Id" MaxLength="0" Style="display: inline;"
AutoCompleteMode="SuggestAppend"
onselectedindexchanged="ComboBox1_SelectedIndexChanged">
</asp:ComboBox>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/StudentDB.accdb"
SelectCommand="SELECT [Id], [Name], [Address] FROM [tblStudents]">
</asp:AccessDataSource>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>

Try with the below markup. I've used DropDownList, it could be replaced with AJAX ComboBox.
The DetailsView could be further enhanced with CSS and ItemTemplate.
You could put more fields into the ItemTemplate like City, Country and so on.
<asp:AccessDataSource DataFile="App_Data/Students.accdb" ID="AccessDataSource1"
DataSourceMode='DataSet' SelectCommand='Select ID, [First Name], [Last Name] from Students'
runat='server' >
</asp:AccessDataSource>
<asp:AccessDataSource DataFile="App_Data/Students.accdb" ID="AccessDataSource2"
SelectCommandType="Text" SelectCommand='Select [ID], [Address] from [Students] where ID=#id'
runat='server'>
<SelectParameters>
<asp:ControlParameter ControlID='DropDownList1' Name='id'/>
</SelectParameters>
</asp:AccessDataSource>
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' DataSourceID='AccessDataSource1'
DataTextField="First Name" DataValueField='ID'>
</asp:DropDownList>
<asp:DetailsView ID='DetailsView' runat="server" DataSourceID='AccessDataSource2' DataKeyNames='ID'>
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID='AddressTextBox' runat='server' Text='<%# Bind("Address") %>'></asp:TextBox>
<asp:Button ID='AddressSaveButton' runat='server' Text='Save' UseSubmitBehavior='true' />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

In your ComboBox1_SelectedIndexChanged event. Set TextBox1.Text = CurrentAddress variable.
In reality, I am not a big fan of bindings commands directly in the asp.
I would create a Sub that is called LoadMyComboBox() which would fire in the Page_Load() event.
I would assume your going to have a button or some type of event that would be fired to perform your update? TextChanged would be overkill here. Adding an update button and then building your update command in the code behind would handle your update. Once that update is complete you can simply call LoadMyComboBox() again to refresh your combobox to refresh any necessary changes and do whatever you want with your textbox at this time as well.

u can do this by using a dropdownlist itself. In the DropDownList's SelectedIndexChanged event, bind the address to the TextBox control according the dropdownlist selection and pass this as a parameter and save it in database.

Related

Datapager and ListView inside a repeater

I have a Repeater with a ListView and its Datapager inside.
The information is populated correctly but, the datapager does not refresh the listview when clicking the page numer.
How do I refresh he list view based on datapager selection
<asp:Repeater runat="server" ID="Rptr" DataSourceID="Categories" >
<asp:HiddenField runat="server" id="Hidden" Value='<%# Eval("Category") %.' />
<ItemTemplate>
<asp:DataPager runat="server" ID="DtPgr_Top" PagedControlID="List" PageSize="40">
...
</asp:DataPager>
<asp:ListView runat="server" ID="List" DataSourceID="Items" >
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="Items" ConnectionString="<%$ ConnectionStrings:SAP_B1 %>"
SelectCommand=" SELECT Item FROM Items WHERE Category = #Category " >
<SelectParameters>
<asp:ControlParameter ControlID="Hidden" Name="Category" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
" SelectCommand=" SELECT Category FROM Categories " />
enter code here
Solution 1 /
You have to put the listview outside the repeater.
Solution 2 /
Do not determine the datasource of repeater inline. Populate it from codebehind only once and only on load with - if not ispostback - directive.

Issue with linkbutton in radlistview

I create a radlistview and bind it with objectdatasource and define a linkbutton in radlistview
and linkbutton text bind to multi column in radlistview to display multi line in my radlisview.
Now I want when a user click on linkbutton, one row of radlistview that user clicked on is set to a session so I could use it in other pages.
I define selected items in radlistview itemdatabound event or in radlistview itemcommand event, but these events not fire .please help me.
my code is:
<asp:LinkButton ID="lbl1" runat="server"
OnClick="linqbutton1_Click"
CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>'
Text='<%#" ยป"+"buy"+" "+Eval("MelkType")+" "+Eval("MelkSize")+" meter"+" "+Eval("Melkregion") %>'
style="margin-right:0px;direction:rtl;margin-top:20px; ">
</asp:LinkButton>
<br />
<br />
</ItemTemplate>
</telerik:RadListView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
SelectMethod="RentalApartmentGet" TypeName="mymelkclass">
</asp:ObjectDataSource>
You should just extract the values server side after the selection is made, i think this is demo is close to what you need :
http://demos.telerik.com/aspnet-ajax/listview/examples/selecting/defaultcs.aspx

asp.net DetailsView - AutoGenerate Columns

I have a asp DetailsView control that I auto generate the fields.
There is one column that has an ID that is mapped to another table (foreign key). It shows up in a textbox. I want that column to be displayed as a dropdownlist as indicated in my code example below. This works fine, but the other column still shows the textbox with the ID in it.
My question is:
is it possible to use the auto generate and still hide columns you don't need or want to modify?
I hate to have to write code for every single column just because one column needs to use a TemplateField.
DetailsView
<asp:DetailsView ID="DetailsView1" runat="server"
DefaultMode="Edit" DataSourceID="EntityDataSource1"
AutoGenerateEditButton="True" AutoGenerateInsertButton="True">
<Fields>
<asp:TemplateField HeaderText="Authorization">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" Runat="server" DataSourceID="EntityDataSource2" CssClass="DropDown"
DataTextField="Name" DataValueField="AuthenticationId" SelectedValue='<%# bind("AuthenticationId") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp
</Fields>
</asp:DetailsView>
DetailsView DataSource:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ContextTypeName="EntityNamespace.MyEntity" EnableFlattening="False"
EntitySetName="Routes" Include="Authentication" Where="it.RouteId = #RouteId">
<WhereParameters>
<asp:RouteParameter Type="Int32" RouteKey="RouteId" Name="RouteId" />
</WhereParameters>
</asp:EntityDataSource>
Dropdownlist DataSource
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ContextTypeName="EntityNamespace.MyEntity" EnableFlattening="False"
EntitySetName="Authentications">
</asp:EntityDataSource>
I think you can. Take a look at AutoGenerateRows. It says like this on msdn:
Explicitly declared row fields can be used in combination with
automatically generated row fields. When both are used, explicitly
declared row fields are rendered first, followed by the automatically
generated row fields.
But you also have to consider that the rows are not in the field collection
Automatically generated bound row fields are not added to the Fields
collection.
Reference here

DropDownList TextValue is 1st in List

I'm a newbie when it comes to asp.net, so grateful for any help.
I have a simple data bound drop down list, with a details view control. I can select a value from the list, hit update and the correct value gets written to the database. Problem is, the control the automatically "resets" to display the 1st value in the list. This would confuse the user and make them think they'd selected the 1st value prior to the update.
The only code-behind code in relation to this drop down list can be found in the ItemUpdating method of the details view control, as follows:
DropDownList ddlLoc = (DropDownList)dvYourProfile.FindControl("ddlLocation");
e.NewValues["PreferredLocation"] = ddlLoc.SelectedValue;
And here's the code form the aspx page
<asp:TemplateField HeaderText="Preferred Location"
SortExpression="PreferredLocation">
<ItemTemplate>
<asp:DropDownList Text='<%# Bind("PreferredLocation") %>' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="dsStaticDate" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT * FROM StaticData" />
You need to bind form the code behind. The out-of-the-box behavior of binding on the aspx page with the SqlDataSource does not allow you to stop the rebinding of the DropDownList after an event occurs.

Binding DropDownList SelectedValue to DataValueField for GridView Update

Ok guys, so I read this:
How to set SelectedValue of DropDownList in GridView EditTemplate
and I'm having the same issue. However, I don't want to bind the selected value to the displayed text, but instead the values. They are different attributes selected from a SQLDataSource. Here is my DDL code with its SQLDataSource:
<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
DataSourceID="Plant_Carton_Food_List"
DataTextField="Food_Description"
DataValueField="PlantMaterial_FoodID"
SelectedValue='<%# Bind("PlantMaterial_FoodID") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="Plant_Carton_Food_List" runat="server"
ConnectionString="<%$ ConnectionStrings:OMSConnectionString %>"
SelectCommand="SELECT P.PlantMaterial_FoodID,
M.Material_SAP_Value + ' - ' + MD.SAP_Long_Description AS Food_Description
FROM Plant_Carton_Food AS P
LEFT OUTER JOIN Material_Descriptions AS MD
ON P.PlantMaterial_FoodID = MD.Material_ID
LEFT OUTER JOIN Materials AS M
ON P.PlantMaterial_FoodID = M.Material_ID">
</asp:SqlDataSource>
Here is the (abridged) SQLDataSource of the GridView:
<asp:SqlDataSource ID="Plant_Carton_Table" runat="server"
OldValuesParameterFormatString="old_{0}"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
OnInserting="Plant_Carton_Food_Table_Inserting"
OnInserted="Plant_Carton_Food_Table_Inserted"
InsertCommand="spPlantCartonInsert" InsertCommandType="StoredProcedure"
SelectCommand="spPlantCartonSelect" SelectCommandType="StoredProcedure"
UpdateCommand="spPlantCartonUpdate" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="Active_Case" Type="Boolean" />
<asp:Parameter Name="PlantMaterial_FoodID" Type="String" />
<asp:Parameter Name="PlantMaterial_CaseID" Type="String" />
...
</UpdateParameters>
...
</asp:SqlDataSource>
And, finally, my exception:
DataBinding: 'System.Data.DataRowView'
does not contain a property with the
name 'PlantMaterial_FoodID'.
I really don't have much knowledge on how databinding through the GridView Edit templates work, but I was able to see that the correct values pass through the OnRowCommand event handler for updates. How do I propagate these values to the SQLDataSource without getting NULL?
Right now, I guess any explanation on how databinding with GridView templates work in general would be beneficial as well. Thanks!
This isn't really an answer to the question, but instead a workaround... but it works for me.
I added PlantMaterial_FoodID as a hidden column into the GridView and changed the SelectedValue binding on the DropDownList to reference this new column, as shown below.
New Column
<asp:TemplateField HeaderText="Food ID" SortExpression="Food_ID">
<EditItemTemplate>
<asp:Label ID="FoodIDLabel" runat="server" Text='<%# Eval("Food_ID") %>'</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="FoodIDLabel" runat="server" Text='<%# Bind("Food_ID") %>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
...and here is the new binding
<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
DataSourceID="Plant_Carton_Food_List" DataTextField="Food_Description"
DataValueField="PlantMaterial_FoodID" SelectedValue='<%# Bind("Food_ID") %>'
</asp:DropDownList>
This effectively sets the selected dropdownlist index to the correct value.
Have you checked that PlantMaterial_FoodID is spelled exactly as in the SqlDataSource SelectCommand?

Resources