我有一个asp.net c#页面,它显示了一个组织列表,并通过文本字段提供了“组织类型”及其地址。我最初使代码工作,选择组织将触发SelectedIndexChanged事件并填充额外的文本字段。
现在我已经添加了额外的功能来从记录中选择此信息,DropDownList始终保留在第一个选项。
我希望发生以下情况:
您会注意到我已为该组织硬编码ID 43。我也很难将其传递给组织DDL。我可能错误地做了那个部分。 我提供了asp.net和c#代码隐藏:
<%@ 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();
}
}
*/
}
由于
萨姆