Showing posts with label newbie. Show all posts
Showing posts with label newbie. Show all posts

Saturday, March 24, 2012

Populate textbox from item selected in a drop down list

I am a newbie to .net so bear with me here.

I have a page that has a dropdown list of customers. I want to select a customer from that dropdown and populate the appropriate textboxes.

So when I select 'John Smith' from the dropdown list I have a form with textboxes for first name, last name, accout id, etc that needs to populate accordingly. I am using VB.net with MS SQL. I have been successful populating the dropdown list from the DB. Where I am lost is populating the appropriate textboxes for the customer selected in the dropdown list. Any help is appreciated.Hi,

If you are having the dropdownlist and the textbox in the same form, then you need to do as below:-

Set the autopostback property for the dropdownlist to true. This will make the dropdownlist post back to the server, once an element is selected. Then, you can put the following code in your codebehind file to get the selected item.

If Not IsPostBack Then

textbox1.text = dropdownlist1.SelectedItem.Text

End If

If you are having the textbox in a different form, then you need to pass the selected value by querystring to that form.

If Not IsPostBack Then

Response.Redirect("NewForm.aspx?name=" & dropdownlist1.SelectedItem.Text)

End If

Then in the new form, you can get the value using the following code:-

textbox1.text = Request.Params("name")

Hope it helps.
Something to keep in mind:

The name of the user (or whatever) may not be unique. You can have two people with the name "John Smith." You propably have a column in your DB that is unique for each person like UserID or whatever.

So when you update the dropdownlist from the database, you need to retrieve the unique id for each person as well, so that when the index of the selected index in the drop down list changes, you can use the corresponding unique ID to retrieve the other info from the database.

I cant remember exactly, but you should take a loot at the dropdown control itself. Many such controls have a 'Value' for each item in them. You van set the 'Value' to the unique ID somehow.

Youll have to take a look...

Wednesday, March 21, 2012

Populating a DataGrid with a Stored Procedure based on ddl values

Hello All -

First off, I am a complete newbie.

I have created a page with 5 dropdownlists that I populated using stored procedures.

Question one:

How Do I display a default value for these Dropdowns?

I have configured a DataGrid using another Stored Procedure.

Question 2:

How Do I populate the DG with the values specified within the 5 dropdownlists using a stored procedure?

Here is a copy of the current aspx page.

<%@dotnet.itags.org.PageLanguage="VB"MasterPageFile="~/MasterPage.master"Title="CTM PreRunbook" %>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">

<table><tr><thalign="center"colspan="3"style="vertical-align: middle; text-align: center"valign="middle">

<asp:LabelID="Label1"runat="server"Font-Size="Large"Text="Data Feed Query"></asp:Label> </th></tr>

<tr>

<th>

Application Name:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlAppName"runat="server"AutoPostBack="True"DataSourceID="SqlDataSource1"DataTextField="AppName"DataValueField="AppName">

<asp:ListItem>- Select -</asp:ListItem>

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spGet_AppName"SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Data Feed Name:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px; text-align: left;">

<asp:DropDownListID="ddlDataFeedName"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceDependantFeeds"DataTextField="FeedName"DataValueField="FeedName">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceDependantFeeds"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGet_FeedNames"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Account:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlAccount"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceServiceAccount"DataTextField="ServiceAccounts"DataValueField="ServiceAccounts">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceServiceAccount"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGetDistinctServiceAccounts"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Platform:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px; text-align: left;">

<asp:DropDownListID="ddlPlatform"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourcePlatform"DataTextField="Platform"DataValueField="Platform">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourcePlatform"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spGet_Platform"SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Scheduled Run Days:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlSchedRunDays"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceSchedRunDays"DataTextField="ScheduledRunDays"DataValueField="ScheduledRunDays">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceSchedRunDays"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGet_ScheduledRunDays"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

</Table>

<table>

<tr>

<tdstyle="width: 100px"align="center">

<asp:ButtonID="btnFilter"runat="server"Text="Filter"/></td>

<tdstyle="width: 100px">

<asp:ButtonID="btnReset"runat="server"Text="Reset"/></td>

</tr>

<tr>

<tdcolspan="2"align="center">

<asp:ButtonID="btnShowAllDataFeeds"runat="server"Text="Show All Data Feeds"/></td>

</tr>

</table>

<br/>

<asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True"

AutoGenerateColumns="False"DataSourceID="SqlDataSourceGridView">

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSourceGridView"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spFilterByServAcct"SelectCommandType="StoredProcedure">

<SelectParameters>

<asp:FormParameterFormField="ServiceAccounts"Name="ServAcct"

Type="String"/>

</SelectParameters>

</asp:SqlDataSource>

<br>

</asp:Content>

Thanks ,

Pat

here is the best example that describes your needs

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/sqldatasource.aspx