バインドされた DetailView を作成しました。TextBox ではなく、ProposalStatus フィールドに DropDownList を使用したいと考えています。DropDownList を動的に追加できますが、挿入をクリックした後にコントロールが見つかりません。
背後のコードで DropDownList を見つける方法はありますか? または、ItemTemplate を使用する必要がありますか?
ありがとうございました
テスト.aspx
<form runat="server">
<asp:DetailsView ID="dvProposal" runat="server" Height="50px" Width="100%"
OnModeChanging="dvProposal_OnModeChanging" OnItemInserting="dvProposal_OnItemInserting"
OnItemDeleting="dvProposal_OnItemDeleting">
</asp:DetailsView>
</form>
test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
public partial class test : System.Web.UI.Page
{
string connectionString = WebConfigurationManager.ConnectionStrings["WildfireOperationsResearch"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
dvProposal_DataRebind();
}
}
protected DropDownList LoadProposalStatusDropDownList()
{
DropDownList ProposalStatusList = new DropDownList();
string SelectString = "select ProposalStatus from ProposalStatus order by ProposalStatus";
OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter(SelectString, MyConnection);
DataSet MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet);
ProposalStatusList.DataSource = MyDataSet;
ProposalStatusList.DataValueField = "ProposalStatus";
ProposalStatusList.DataTextField = "ProposalStatus";
ProposalStatusList.ID = "ddProposalStatus";
ProposalStatusList.DataBind();
return ProposalStatusList;
}
protected void dvProposal_DataRebind()
{
string SelectString = "select ProposalNo,ProposalCode, ProposalTitle, YearSubmitted, ProposalStatus, ProposalLink " +
"from Proposal where ProposalNo=64" ;
OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter(SelectString, MyConnection);
DataSet MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet);
//load into detail view
dvProposal.DataSource = MyDataSet;
dvProposal.AutoGenerateEditButton = true;
dvProposal.AutoGenerateDeleteButton = true;
dvProposal.AutoGenerateInsertButton = true;
dvProposal.DataBind();
if (dvProposal.CurrentMode != DetailsViewMode.ReadOnly)
{
// add the DropDownList to the DetailView
DropDownList ProjectProposalList = LoadProposalStatusDropDownList();
dvProposal.Rows[4].Cells[1].Controls.Add(ProjectProposalList);
dvProposal.Rows[4].Cells[1].Controls[0].Visible=false;
((TextBox)dvProposal.Rows[0].Cells[1].Controls[0]).Enabled = false;
}
}
protected void dvProposal_OnModeChanging(Object sender, DetailsViewModeEventArgs e)
{
dvProposal.ChangeMode(e.NewMode);
dvProposal_DataRebind();
}
// NullReferenceException を返してください。DetailView で DropDownList コントロールが見つかりません。
protected void dvProposal_OnItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string proposalCode = e.Values[1].ToString();
string proposalTitle = e.Values[2].ToString();
string YearSubmitted = e.Values[3].ToString();
**//give me a NullReferenceException. Cannot find the DropDownList control in the DetailView.**
string proposalStatus = ((DropDownList)dvProposal.Rows[4].Cells[1].FindControl("ddProposalStatus")).SelectedValue;
string ProposalLink = e.Values[5].ToString();
string insertString = " insert into Proposal (ProposalCode, ProposalTitle, YearSubmitted, ProposalStatus, ProposalLink) " +
"values('" + proposalCode + "','" + proposalTitle + "'," + YearSubmitted + ",'" + proposalStatus + "','" + ProposalLink + "')";
OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbCommand MyCommand = new OleDbCommand(insertString, MyConnection);
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
dvProposal_DataRebind();
}
protected void dvProposal_OnItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
string deleteString = "delete from Proposal where ProposalNo=" + e.Values[0];
OleDbConnection MyConnection = new OleDbConnection(connectionString);
OleDbCommand MyCommand = new OleDbCommand(deleteString, MyConnection);
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
dvProposal_DataRebind();
}
}