Help Me~~My DropDownList Can not get the"BigClassList.SelectedItem.Text" there is an error: System.N

H

huobazi

I have many dropdownlist controls in my ascx (and use LoadControl in a aspx fiel) file,so i write a method "InitList(DropDownList list,string strsql,string TextField,string ValueField)"
but when i want to get the BigClassList.SelectedItem.Text and BigClassList.SelectedItem.Value in a button onclick method,btnSmallClassEdit_Click(....),there post an error " System.NullReferenceException: 佫¶ÔÏóÒýÓÃÉèÖõ½¶ÔÏóµÄʵÀý¡£",

I Don't know the wrong is where in my code, pls help me thanks!!!
I guess the BigClassList.SelectedItem.Text and BigClassList.SelectedItem.Value may be null


///////a part of my codes here//////////////////////////////////////////////////////
///My connection defined in the parent page which is inherits from the class "Page";
// And i make sure the type, and ID matches to the HTML, and HTML declaration of BigClassList has a
// runat=server attribute and in a "<form runat=server>" tags .
////////////////////////////////////////////////////////////////////////////////////
public void Page_Load(Object sender,EventArgs e)

{

if(!IsPostBack)

{
BigClassDropDownListInit();
}
}


public void BigClassDropDownListInit()
{
InitList(BigClassList,"select ID,BigClassName from RootClass order by BigClassName","BigClassName","ID");
}


public void InitList(DropDownList list,string strsql,string TextField,string ValueField)
{
Conn.Open();
Cmd =new OleDbCommand(strsql,Conn);
OleDbDataReader Rd = Cmd.ExecuteReader();
list.DataSource=Rd;
list.DataTextField=TextField;
list.DataValueField=ValueField;
list.DataBind();
Rd.Close();
Conn.Close();
}
public void btnSmallClassAdd_Click(Object sender,EventArgs e)
{
Conn.Open();
OleDbCommand commTest=new OleDbCommand("select * from Type where TypeName='"+txtSmallClassAdd.Text+"'",Conn);
int i=commTest.ExecuteNonQuery();
Conn.Close();
if(i>0)
Response.Write("<script>alert('ÒÑÓÐͬÃû·ÖÀà´æÔÚ!');</script>");
else
{
string strInsert="insert into Type(TypeName,BigClassName,BigID) Values ('"+txtSmallClassAdd.Text+"','"+BigClassList.SelectedItem.Text+"','"+BigClassList.SelectedItem.Value+"')";

Conn.Open();
OleDbCommand commAdd=new OleDbCommand(strInsert,Conn);
commAdd.ExecuteNonQuery();
Conn.Close();
Response.Write("<script>alert('¶þ¼¶·ÖÀàÌí¼Ó³É¹¦!');</script>");
}
BigClassDropDownListInit();
SmallClassDropDownListInit();
}
////////////////about the ascx fiels code here//
<td width="80%" valign="middle" bgcolor="#FFFFFF"><font color="#000000">
¡¡ÔÚ:</font> <asp:dropdownlist ID="BigClassList" Width="100" runat="server" ></asp:dropdownlist> <font color="#000000">ÖÐÔö¼Ó¶þ¼¶À¸Ä¿ </font> <asp:textbox ID="txtSmallClassAdd" Width="100" runat="server" ></asp:textbox> <asp:button ID="btnSmallClassAdd" OnClick="btnSmallClassAdd_Click" Text="Ìí¼Ó" runat="server" ></asp:button> </td>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top