0

我创建了一个绑定的 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();
    }

}
4

1 回答 1

0

感谢加里森尼利的建议。我猜现在使用 TemplateField 是最快的方法。所以,我修改了代码。

测试.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>

测试.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();
    }

}
于 2013-07-17T16:58:31.550 回答