在dropdownlist中编辑时发生错误..for发送dropdownlistvalue IAM获取对象未设置为对象的实例 [英] error while editing in dropdownlist..for send dropdownlistvalue iam getting object not set to instance of an object

查看:35
本文介绍了在dropdownlist中编辑时发生错误..for发送dropdownlistvalue IAM获取对象未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<table cellpadding="8" cellspacing="3"  >

<tr>
    <td>
        Category Name:
    </td>
    <td>
        <asp:DropDownList ID="DropDownList1" runat="server"

            onselectedindexchanged="DropDownList1_SelectedIndexChanged"

            AutoPostBack="true" Width="146px" ontextchanged="DropDownList1_TextChanged">
        </asp:DropDownList>

    </td>
</tr>
<tr>
<td>
    SubCategory Name:
</td>
<td>
    <asp:DropDownList ID="DropDownList2" runat="server"  Width="146px"

        AutoPostBack="true" onprerender="DropDownList2_PreRender"

        onselectedindexchanged="DropDownList2_SelectedIndexChanged"

        ontextchanged="DropDownList2_TextChanged">
    </asp:DropDownList>
</td>
</tr>
<tr>
<td>
    Level Name: /td>
<td>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>









我的C#代码是:










my c# code is:


 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            filldropdown();
           
            fillgrid();

            if (Request.QueryString["id"] != null)
            {
                Button1.Visible = false;
                Button2.Visible = true;
                edits();
            }
            else
            {
                Button2.Visible = false;
                Button1.Visible = true;
            }

           
        }


    }

    public void fillgrid()
    {
        onlineexam oe = new onlineexam();
        //DataTable dt = oe.getcasestudy();
        DataTable dt = oe.getlevel();
        if (dt.Rows.Count == 0)
        {

        }
        else
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
    public void edits()
    {
        onlineexam oe = new onlineexam();
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
     
        DataTable dt = oe.getlevelbyid(id);
       
           string fn= dt.Rows[0]["CategoryId"].ToString();
        string sn = dt.Rows[0]["CategoryName"].ToString(); 
        DropDownList1.SelectedItem.Text = fn;
/*error here* Object reference not set to an instance of an object*/ DropDownList2.SelectedItem.Text= sn;

       // for (int z = 0; z < DropDownList1.Items.Count; z++)
       // {
       // if (DropDownList1.Items[z].Text == fn)
       // {
       //     DropDownList1.Items[z].Selected = true;
       // }
       //}
        //for (int x = 0; x < DropDownList2.Items.Count; x++)
        //{
        //    if (DropDownList2.Items[x].Text == sn)
        //    {
        //        DropDownList2.Items[x].Selected = true;
        //    }
        //}
            TextBox1.Text = dt.Rows[0]["LevelName"].ToString();
    }


    public void filldropdown()
    {
        onlineexam oe = new onlineexam();
        DataTable dt = oe.getcategoryid();
        if (dt.Rows.Count == 0)
        {
        }
        else
        {
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = dt.Columns["CategoryName"].ToString();
            DropDownList1.DataValueField = dt.Columns["Id"].ToString();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "Select");
        }
    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CategoryId = DropDownList1.SelectedItem.Text.ToString();
        string CategoryName = DropDownList2.SelectedItem.Text.ToString();
      //  string CategoryName = DropDownList3.SelectedItem.Text.ToString();
        string LevelName = TextBox1.Text;
        onlineexam oe = new onlineexam();
        oe.addlevel(CategoryId, CategoryName, LevelName);
        MessageBox msg = new MessageBox();
        msg.Show("Level Added Successfully");
        fillgrid();
        DropDownList2.Items.Clear();
        TextBox1.Text = "";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        MessageBox msg = new MessageBox();
        onlineexam oe = new onlineexam();
        // oe.deletecasestudy(id);
        oe.deletelevel(id);


        msg.Show("Deleting Successfully");
        fillgrid();
        DropDownList1.SelectedItem.Text = "Select";
        DropDownList2.Items.Clear();
        TextBox1.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        string CategoryId = DropDownList1.SelectedItem.Text.ToString();
        string CategoryName = DropDownList2.SelectedItem.Text.ToString(); ;
        //string CategoryName = DropDownList3.SelectedItem.Text.ToString();
        string LevelName = TextBox1.Text;
        onlineexam oe = new onlineexam();
        oe.updatelevel(id, CategoryId, CategoryName, LevelName);
        MessageBox msg = new MessageBox();
        msg.Show("Level Added Successfully");
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    public void filldropdown2(int CategoryId)
    {
        onlineexam oe = new onlineexam();
        DataTable dt = oe.getsubcategoryid(CategoryId);
        if (dt.Rows.Count == 0)
        {

            //DropDownList2.Items.Insert(0, "Select");
            DropDownList2.Items.Insert(0, "NULL");
        }
        else
        {
            DropDownList2.DataSource = dt;
           // DropDownList2.DataTextField = dt.Columns["CategoryName"].ToString();
            //string d = dt.Columns["CategoryName"].ToString();
            //DropDownList2.DataTextField = d;
            DropDownList2.DataValueField = dt.Columns["CategoryName"].ToString();
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "Select");
        }
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_TextChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        TextBox1.Text = "";
        int CategoryId = Convert.ToInt32(DropDownList1.SelectedItem.Value);
        filldropdown2(CategoryId);
    }
    protected void DropDownList2_TextChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList2_PreRender(object sender, EventArgs e)
    {
        
    }
}

推荐答案


代替:
Hi,
Instead of :
DropDownList1.SelectedItem.Text = fn;
/*error here* Object reference not set to an instance of an object*/ DropDownList2.SelectedItem.Text= sn;


用途:


Use:

DropDownList1.Text = fn;
/*error here* Object reference not set to an instance of an object*/ DropDownList2.Text= sn;


因为要输入自定义文本,所以所选项目的属性在那时将为空,直到并且除非您从列表中选择任何项目.


--Amit


Because you are entering custom text so selected item property will be null at that time until and unless you select any item from the list.


--Amit


DropDownList1.SelectedValue= fn;


/*错误在这里*对象引用未设置为对象的实例*/

不需要这个


/*error here* Object reference not set to an instance of an object*/

No need of this

 string fn= dt.Rows[0]["CategoryId"].ToString();
 string sn = dt.Rows[0]["CategoryName"].ToString(); 
 DropDownList1.SelectedItem.Text = fn;

filldropdown2(fn);
DropDownList2.SelectedItem.Text= sn;



dropdown selectedText 将被自动选择,因为您要绑定DropdownList 之前要进行edit().


谢谢
灰烬



The selectedText of dropdown will be automatically get selected, because you are binding DropdownList before going to edit().


Thanks
Ashish


这篇关于在dropdownlist中编辑时发生错误..for发送dropdownlistvalue IAM获取对象未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆