0

バインドされた 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();
    }

}
4

1 に答える 1

0

Garrison Neely の提案に感謝します。今のところ、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>

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

}
于 2013-07-17T16:58:31.550 に答える