我创建了一个绑定的 DetailView。我想为 ProposalStatus 字段而不是 TextBox 设置一个 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>
测试.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();
}
}