I have an asp.net c# page that shows a list of organisations and also provides via text fields the 'type of organisation' and its address. I originally had the code working whereby selecting an organisation would fire the SelectedIndexChanged event and populate the extra textfields.
Now that I have added extra functionality to select this information from a record, the DropDownList always stays at the first option.
I would like the following to happen:
- DropDownList to be filled with the list of organisations
- The records oganisationID is used to select the appropriate item in the DropDownList
- Populate the text fields accordingly
- If the user select a different organisation from the list, the SelectedIndexChanged event populates the relevant fields again.
You will notice that I have hardcoded the ID 43 for the organisation. I was also having trouble passing that to the Organisation DDL. I may have been doing that part wrongly. I have provided the asp.net and c# code-behind:
<%@ Page Title="Incident Record" Language="C#" MasterPageFile="~/common/master/incident.master" AutoEventWireup="true" CodeFile="recordstackoverflowexample.aspx.cs" Inherits="incident_incidentdetails" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cp" runat="Server">
<!-- Forms
================================================== -->
<div class="row">
<div class="col-lg-12">
<div class="well">
<form class="bs-example form-horizontal" runat="server">
<fieldset>
<legend>Incident Record</legend>
<asp:SqlDataSource ID="SQLDSRecordPage" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" DataSourceMode="DataSet" SelectCommand="uspRecordPage" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="RecordID" QueryStringField="recordid" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<div class="form-group">
<label class="col-lg-3 control-label">Organisation's contact details</label>
</div>
<div class="form-group">
<div class="col-lg-6">
<asp:SqlDataSource ID="SQLDSOrganisations" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT [OrganisationID], [OrganisationName] FROM [tblOrganisation] WHERE ([IsCurrent] = @IsCurrent) ORDER BY [OrganisationName]">
<SelectParameters>
<asp:Parameter DefaultValue="TRUE" Name="IsCurrent" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList class="form-control" ID="DDLOrganisation" name="SelOrganisation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDLOrganisation_SelectedIndexChanged"></asp:DropDownList>
</div>
</div>
<asp:SqlDataSource ID="SqlDSOrganisationType" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT [OrganisationID], [OrganisationName], [OrganisationType] FROM [tblOrganisation], [pickOrganisationType] WHERE [tblOrganisation].[OrganisationTypeID] = [pickOrganisationType].[OrganisationTypeID] AND ([IsCurrent] = @IsCurrent) AND ([OrganisationID] = @OrganisationID)">
<SelectParameters>
<asp:Parameter DefaultValue="TRUE" Name="IsCurrent" Type="Boolean" />
<asp:ControlParameter ControlID="DDLOrganisation" Name="OrganisationID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<div class="form-group">
<div class="col-lg-2">
<label class="control-label">Organisation type:</label>
</div>
<div class="col-lg-4">
<asp:TextBox ID="TxtOrganisationType" class="form-control" runat="server" ReadOnly="true"></asp:TextBox>
</div>
</div>
<asp:SqlDataSource ID="SqlDSOrganisationAddress" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>" SelectCommand="SELECT [AddressLine1], [AddressLine2], [AddressLine3], [Town], [County], [Postcode], [Telephone], [Fax], [Email] FROM [tblOrganisationAddress] WHERE ([OrganisationAddressID] = @OrganisationAddressID)">
<SelectParameters>
<asp:ControlParameter ControlID="DDLOrganisation" Name="OrganisationAddressID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<div class="form-group">
<div class="col-lg-2">
<label class="control-label">Address line 1:</label>
</div>
<div class="col-lg-4">
<asp:TextBox ID="TxtOrganisationAddressLine1" class="form-control" runat="server" ReadOnly="true"></asp:TextBox>
</div>
</div>
<div class="form-group">
<div class="col-lg-2">
<label class="control-label">Address line 2: </label>
</div>
<div class="col-lg-4">
<asp:TextBox ID="TxtOrganisationAddressLine2" class="form-control" runat="server" ReadOnly="true"></asp:TextBox>
</div>
</div>
<div class="form-group">
<div class="col-lg-2">
<label class="control-label">Address line 3:</label>
</div>
<div class="col-lg-4">
<asp:TextBox ID="TxtOrganisationAddressLine3" class="form-control" runat="server" ReadOnly="true"></asp:TextBox>
</div>
</div>
<div class="form-group">
<div class="col-lg-4 col-md-offset-2">
<a href="/incident/record.aspx">
<asp:Button class="form-control btn btn-success" ID="BtnUpdateRecord" PostBackUrl="~/incident/record.aspx" runat="server" Text="Update Record" /></a>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class incident_incidentdetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadOptions();
}
}
protected void LoadOptions()
{
DataTable organisations = new DataTable();
SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
using (connection)
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [OrganisationID], [OrganisationName], [OrganisationType] FROM [tblOrganisation], [pickOrganisationType] WHERE [tblOrganisation].[OrganisationTypeID] = [pickOrganisationType].[OrganisationTypeID] AND ([IsCurrent] = 1)", connection);
adapter.Fill(organisations);
DDLOrganisation.DataSource = organisations;
DDLOrganisation.DataTextField = "OrganisationName";
DDLOrganisation.DataValueField = "OrganisationID";
DDLOrganisation.DataBind();
//DDLOrganisation.SelectedIndex = DDLOrganisation.Items.IndexOf(DDLOrganisation.Items.FindByText("Name of Organisation"));
DDLOrganisation.SelectedIndex = DDLOrganisation.Items.IndexOf(DDLOrganisation.Items.FindByValue("43"));
DataView dv2 = (DataView)SqlDSOrganisationType.Select(DataSourceSelectArguments.Empty);
DataRowView drv2 = dv2[0];
TxtOrganisationType.Text = drv2["OrganisationType"].ToString();
DataView dv = (DataView)SqlDSOrganisationAddress.Select(DataSourceSelectArguments.Empty);
DataRowView drv = dv[0];
TxtOrganisationAddressLine1.Text = drv["AddressLine1"].ToString();
TxtOrganisationAddressLine2.Text = drv["AddressLine2"].ToString();
TxtOrganisationAddressLine3.Text = drv["AddressLine3"].ToString();
}
}
protected void DDLOrganisation_SelectedIndexChanged(object sender, EventArgs e)
{
DataView dv2 = (DataView)SqlDSOrganisationType.Select(DataSourceSelectArguments.Empty);
DataRowView drv2 = dv2[0];
TxtOrganisationType.Text = drv2["OrganisationType"].ToString();
DataView dv = (DataView)SqlDSOrganisationAddress.Select(DataSourceSelectArguments.Empty);
DataRowView drv = dv[0];
TxtOrganisationAddressLine1.Text = drv["AddressLine1"].ToString();
TxtOrganisationAddressLine2.Text = drv["AddressLine2"].ToString();
TxtOrganisationAddressLine3.Text = drv["AddressLine3"].ToString();
}
protected void SQLDSRecordPage_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@RecordID"].Value = Request.QueryString["recordid"];
}
/*
SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
string selectedID = DDLOrganisation1.SelectedItem.Value;
SqlCommand theCommand = new SqlCommand ("SELECT [AddressLine1], [AddressLine2], [AddressLine3], [Town], [County], [Postcode], [Telephone], [Fax], [Email] FROM [tblOrganisationAddress] WHERE ([OrganisationAddressID] = @OrganisationAddressID)", connection);
theCommand.Parameters.AddWithValue("@OrganisationAddressID", selectedID);
theCommand.CommandType = CommandType.Text;
using (SqlDataReader theReader = theCommand.ExecuteReader())
{
if (theReader.HasRows)
{
// Get the first row
theReader.Read();
// Set the text box values
//CustomerName.Text = theReader.GetString(0);
TxtOrganisationAddressLine1.Text = theReader["AddressLine1"].ToString();
}
}
DataView dv2 = (DataView)SqlDSOrganisationType.Select(DataSourceSelectArguments.Empty);
DataRowView drv2 = dv2[0];
TxtOrganisationType.Text = drv2["OrganisationType"].ToString();
DataView dv = (DataView)SqlDSOrganisationAddress.Select(DataSourceSelectArguments.Empty);
DataRowView drv = dv[0];
// TxtOrganisationAddressLine1.Text = drv["AddressLine1"].ToString();
TxtOrganisationAddressLine2.Text = drv["AddressLine2"].ToString();
TxtOrganisationAddressLine3.Text = drv["AddressLine3"].ToString();
TxtOrganisationTown.Text = drv["Town"].ToString();
TxtOrganisationCounty.Text = drv["County"].ToString();
TxtOrganisationPostcode.Text = drv["Postcode"].ToString();
TxtOrganisationTelephone.Text = drv["Telephone"].ToString();
TxtOrganisationFax.Text = drv["Fax"].ToString();
TxtOrganisationEmail.Text = drv["Email"].ToString();
}
}
*/
}
Thanks
Sam