我创建了一个绑定的DetailView。我想为ProposalStatus字段而不是TextBox提供DropDownList。我可以动态添加DropDownList,但在单击Insert后无法找到控件。
有没有办法在后面的代码中找到DropDownList?或者我必须使用ItemTemplate吗?
谢谢
test.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();
}
}
答案 0 :(得分:0)
感谢Garrison Neely的建议。我想使用TemplateField是现在的快捷方式。所以,我修改了代码。
Test.aspx文件
<form id="Form1" runat="server">
<asp:DetailsView ID="dvProposal" runat="server" Height="50px" Width="100%"
OnModeChanging="dvProposal_OnModeChanging" OnItemInserting="dvProposal_OnItemInserting"
OnItemDeleting="dvProposal_OnItemDeleting" OnDataBound="dvProposal_OnDataBound" AutoGenerateRows="false">
<Fields>
<asp:BoundField DataField="ProposalNo" HeaderText="ProposalNo"/>
<asp:BoundField DataField ="ProposalCode" HeaderText ="ProposalCode" />
<asp:BoundField DataField ="ProposalTitle" HeaderText ="ProposalTitle" />
<asp:BoundField DataField ="YearSubmitted" HeaderText ="YearSubmitted" />
<asp:TemplateField HeaderText ="ProposalStatus">
<InsertItemTemplate>
<asp:DropDownList runat="server" DataTextField ="ProposalStatus" DataValueField ="ProposalStatus"
ID="ddlProposalStatus" ></asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" DataTextField ="ProposalStatus" DataValueField ="ProposalStatus"
ID="ddlProposalStatus" ></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("ProposalStatus") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="ProposalLink" HeaderText ="ProposalLink" />
</Fields>
</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 void LoadProposalStatusDropDownList()
{
DropDownList ProposalStatusList = (DropDownList)this.dvProposal.FindControl("ddlProposalStatus");
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.DataBind();
if (dvProposal.CurrentMode == DetailsViewMode.Edit)
{
DataRowView drv=(DataRowView)this.dvProposal.DataItem;
ProposalStatusList.Items.FindByValue(drv.Row[4].ToString()).Selected = true;
}
}
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();
}
protected void dvProposal_OnDataBound(Object sender, EventArgs e)
{
if (this.dvProposal.CurrentMode != DetailsViewMode.ReadOnly)
{
LoadProposalStatusDropDownList();
((TextBox)dvProposal.Rows[0].Cells[1].Controls[0]).Enabled = false;
}
}
protected void dvProposal_OnModeChanging(Object sender, DetailsViewModeEventArgs e)
{
dvProposal.ChangeMode(e.NewMode);
dvProposal_DataRebind();
}
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("ddlProposalStatus")).SelectedValue;
string proposalLink = e.Values[4].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();
}
}