对象引用未设置为对象DropDownList的实例

时间:2015-07-30 06:41:12

标签: c# asp.net drop-down-menu

<asp:Content ID="Content1" ContentPlaceHolderID="LeftContent" runat="Server">

     <script type="text/javascript">
         function setHash(obj) {
             var deger = $(obj).attr('links');

             $(".active").removeClass("active");
             $('.pagemenudu > li[id="' + deger + '"]').addClass("active");
             $(".highlight").removeClass("highlight");
             $(".iletisimdiv div#" + deger).addClass("highlight");
         }

         $(document).ready(function () {


         });

    </script>
    <ul class="pagemenuDepo">

        <li id="conform" class="active"><a onclick='setHash(this);' links="conform"><%= GetLocalResourceObject("header").ToString().ToUpper() %><span></span></a></li>
    </ul>
</asp:Content>

<asp:Content ID="Content15" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="RightContent" runat="server">


    <script type="text/javascript">
        function formcontrol(objs) {
            var form = $("form:first");
            $('#' + $(form).attr("id")).validate({
                ignore: ":hidden:not(select)",
                rules: {

                    '<%= dtTarih.UniqueID %>': {
                        minlength: 2,
                        required: true
                    },
                    '<%= ddlDepoId.UniqueID %>': {
                        required: {
                            depends: function (element) {
                                return $("#MainContent_RightContent_ddlDepoId").val() == '';
                            }
                        }
                    },

                    '<%= txtDepoAlani.UniqueID %>': {
                        minlength: 2,
                        required: true
                    },
                    '<%= txtGunlukPaletAdedi.UniqueID %>': {
                        minlength: 2,
                        companynumber: true,
                        required: true
                    },
                    '<%= txtGunlukDepoisisi.UniqueID %>': {
                        minlength: 2,
                        required: true
                    },
                    '<%= txtElektrikIndeksi.UniqueID %>': {
                        required: true,
                        selectcheck: true

                    }
                },
                highlight: function (element) {
                    $(element).closest('.required').removeClass('success').addClass('error');
                },
                success: function (element) {
                    element
                            .text('OK!').addClass('valid')
                            .closest('.required').removeClass('error').addClass('success');
                }
            });
            jQuery.validator.addMethod('selectcheck', function (value) {
                return (value != '0');
            }, "year required");
            jQuery.validator.addMethod("companynumber", function (value, element) {
                var regexp = /^[0-9 ]+$/;
                return this.optional(element) || regexp.test(value);
            }, "Enter a valid Company Number.");

            jQuery.validator.addMethod(     //adding a method to validate select box//
                    "chosen",
                    function (value, element) {
                        return (value == null ? false : (value.length == 0 ? false : true))
                    },
                    "MainContent_RightContent_dtTarihplease select an option"//custom message
            );
        }

        $(function () {
            $("#MainContent_RightContent_dtTarih").datepicker();

            $("#<%= ddlDepoId.UniqueID %>").onchange(function (){
                alert($(this).val());
            })
        });
    </script>




    <style>
        /*.chosen-container {
            width: 250px !important;
        }

        .chosen-single {
            width: 250px !important;
        }*/
    </style>

    <div class="iletisimdiv" id="divIletisim" runat="server">
        <div id="conform" data-id="conform" class="highlight">
            <div class="contactarea divids">
                <div>
                    <div class="formrows innovaForms" style="margin-top: 50px;">

                        <div class="column leftcolumn required">
                            <asp:DropDownList ID="ddlDepoId" DataTextField="DepoAdi" DataValueField="DepoId" CssClass="chosen-select" runat="server" ValidationGroup="form" >
                            </asp:DropDownList>
                        </div>
                        <div class="column rightcolumn required">
                            <input type="text" id="dtTarih" runat="server" class="pkinnovaTextArea">
                        </div>

                    </div>

                    <div class="formrows innovaForms ">
                        <div class="column leftcolumn required">
                            <asp:TextBox ID="txtDepoAlani" CssClass="pkinnovaTextArea numeric-input" runat="server" ValidationGroup="form"></asp:TextBox>
                        </div>
                        <div class="column rightcolumn required">
                            <asp:TextBox ID="txtGunlukPaletAdedi" CssClass="pkinnovaTextArea numeric-input" runat="server" ValidationGroup="form"></asp:TextBox>
                        </div>
                    </div>

                    <div class="formrows innovaForms">
                        <div class="column leftcolumn required">
                            <asp:TextBox ID="txtGunlukDepoisisi" CssClass="pkinnovaTextArea numeric-input" runat="server" ValidationGroup="form"></asp:TextBox>
                        </div>
                        <div class="column rightcolumn required">
                            <asp:TextBox ID="txtElektrikIndeksi" CssClass="pkinnovaTextArea numeric-input" runat="server" ValidationGroup="form"></asp:TextBox>
                           <asp:HiddenField runat="server" Value="szdfg" ID="HiddenField" />
                        </div>
                    </div>


                    <asp:Button ID="BtnGonder" OnClientClick=' return formcontrol();' CssClass="contactsubmit" runat="server" Text="Kaydet" OnClick="BtnGonder_Click" ValidationGroup="form" />
                </div>
            </div>
        </div>
    </div>

    <div runat="server" id="message" visible="false"></div>


</asp:Content>

这是我的C#代码

public partial class DepoBilgiFormu : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["clicked"] = null;
            BindDepoAdi();
            dtTarih.Attributes.Add("placeholder", GetLocalResourceObject("datetime").ToString());
            txtDepoAlani.Attributes.Add("placeholder", GetLocalResourceObject("storeArea").ToString());
            txtGunlukPaletAdedi.Attributes.Add("placeholder", GetLocalResourceObject("DailyTrackNumber").ToString());
            txtGunlukDepoisisi.Attributes.Add("placeholder", GetLocalResourceObject("DailyStoreHeat").ToString());
            txtElektrikIndeksi.Attributes.Add("placeholder", GetLocalResourceObject("ElectricityMeterIndex").ToString());

        }
    }
    protected void BindDepoAdi()
    {
        using (UlkerEntity entity = new UlkerEntity())
        {
            ddlDepoId.DataSource = entity.tblDepoes.ToList();
            ddlDepoId.Items.Insert(0,
                new ListItem
                {
                    Enabled = true,
                    Text = GetLocalResourceObject("whichideas").ToString(),
                    Value = "0"
                });
            ddlDepoId.DataBind();

        }
    }

    protected void BtnGonder_Click(object sender, EventArgs e)
    {
        if (Session["clicked"] == null)
        {
            UlkerEntity entity = new UlkerEntity();
            tblDepoBilgiFormu bilgi = new tblDepoBilgiFormu();

            bilgi.DepoAlani = Convert.ToInt32(txtDepoAlani.Text);
            bilgi.GunlukPaletAdeti = Convert.ToInt32(txtGunlukPaletAdedi.Text);
            bilgi.GunlukDepoisisi = Convert.ToInt32(txtGunlukDepoisisi.Text);
            bilgi.ElektrikIndeksi = Convert.ToInt32(txtElektrikIndeksi.Text);
            bilgi.LanguageId = ProClass.Culture.CurrentLanguageId;

            DateTime YeniTarih = Convert.ToDateTime( dtTarih.Value.Substring(3, 2) + "." + dtTarih.Value.Substring(0, 2) + "." + dtTarih.Value.Substring(6, 4));

            bilgi.Date = YeniTarih;

            if (ddlDepoId.SelectedIndex > -1)
            {
                bilgi.DepoId = Convert.ToInt32(ddlDepoId.SelectedItem.Value); (PROBLEM İS HERE)
            }

            entity.AddTotblDepoBilgiFormus(bilgi);
            entity.SaveChanges();
            entity.Dispose();


            divIletisim.Visible = false;
            message.Visible = true;
            message.InnerText = "Bilgiler alınmıştır";

            this.txtDepoAlani.Text = "";
            this.txtElektrikIndeksi.Text = "";
            this.txtGunlukDepoisisi.Text = "";
            this.txtGunlukPaletAdedi.Text = "";
            this.ddlDepoId.SelectedIndex = 0;

            Session["clicked"] = true;
        }
    }

}

下拉列表中存在问题。我无法将数据库下拉列表的下拉列表清空。 任何人都可以帮我解决这个问题吗?谢谢你。

1 个答案:

答案 0 :(得分:0)

问题不是很清楚,因为这是一个错字。但是我注意到你的代码中可能有无意识的东西:你将一个项目插入到dropdownlist然后调用Databind,这实际上是覆盖了Items集合。

如果您的问题没有重新插入的项目,请切换插入位置和数据绑定调用,例如:

ddlDepoId.DataSource = entity.tblDepoes.ToList();
ddlDepoId.DataBind();
ddlDepoId.Items.Insert(0,
    new ListItem
    {
        Enabled = true,
        Text = GetLocalResourceObject("whichideas").ToString(),
        Value = "0"
    });