わかりましたので、データベースから最初に入力されたドロップダウンリストがあります。データテーブルに基づいて、選択した値をデータベースにあるテキストと等しくしたい。私が何をしても、「---Select One---」のみが表示されます。これは、ドロップダウンリスト項目リストに手動で追加した唯一の項目であり、プルしている値が null の場合 (またはそれが必要な場合)、デフォルト値を表示します。する)
protected void Page_Load(object sender, EventArgs e)
{
Master.TopLabel = "Survey Creation";
if (!IsPostBack)
{
SqlConnection Connection = DatabaseConnection.GetSurveySystemConnection();
string sqlquery = "SELECT S.[Survey_Desc], S.[Start_Date], C.[Category_Name] ,S.[End_Date], S.[Audience] FROM [Survey] S Inner Join Category C On S.Category_ID = C.ID Where S.[ID] =" + Session["Survey_ID"];
SqlCommand cmd = new SqlCommand(sqlquery, Connection);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable DT = new DataTable();
da.Fill(DT);
if (DT != null)
{
DescriptionMemo.Text = DT.Rows[0]["Survey_Desc"].ToString();
CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items.IndexOf(CategoryDropDownList.Items.FindByText(DT.Rows[0]["Category_Name"].ToString()));
StartDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["Start_Date"].ToString());
EndDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["End_Date"].ToString());
string Audience = DT.Rows[0]["Audience"].ToString();
if (Audience == "Students Only")
{
AudienceRadioGroup.Items[0].Selected = true;
}
else if (Audience == "Staff Only")
{
AudienceRadioGroup.Items[1].Selected = true;
}
else
{
AudienceRadioGroup.Items[2].Selected = true;
}
}
Connection.Close();
}
}
aspx ページの DropDownList。
<asp:DropDownList ID="CategoryDropDownList" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Category_Name"
DataValueField="Category_Name" AppendDataBoundItems="true" Height="16px"
Width="200px">
<asp:ListItem>---Select One---</asp:ListItem>
</asp:DropDownList>
SQL データソース選択コマンド。
SELECT [Category_Name] FROM [Category]
編集:これは完全なコードですが、関連性があるかどうかはわかりませんでした。申し訳ありません。