I have many dropdownlist controls in my ascx (and use LoadControl in a aspx fiel) file,so i write a method "InitList(DropD ownList list,string strsql,string TextField,strin g ValueField)"
but when i want to get the BigClassList.Se lectedItem.Text and BigClassList.Se lectedItem.Valu e in a button onclick method,btnSmall ClassEdit_Click (....),there post an error " System.NullRefe renceException: 佫¶ÔÏóÒýÓÃÉèÖ Ãµ½¶ÔÏóµÄʵÀý¡£ ",
I Don't know the wrong is where in my code, pls help me thanks!!!
I guess the BigClassList.Se lectedItem.Text and BigClassList.Se lectedItem.Valu e 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(Objec t sender,EventArg s e)
{
if(!IsPostBack)
{
BigClassDropDow nListInit();
}
}
public void BigClassDropDow nListInit()
{
InitList(BigCla ssList,"select ID,BigClassName from RootClass order by BigClassName"," BigClassName"," ID");
}
public void InitList(DropDo wnList list,string strsql,string TextField,strin g ValueField)
{
Conn.Open();
Cmd =new OleDbCommand(st rsql,Conn);
OleDbDataReader Rd = Cmd.ExecuteRead er();
list.DataSource =Rd;
list.DataTextFi eld=TextField;
list.DataValueF ield=ValueField ;
list.DataBind() ;
Rd.Close();
Conn.Close();
}
public void btnSmallClassAd d_Click(Object sender,EventArg s e)
{
Conn.Open();
OleDbCommand commTest=new OleDbCommand("s elect * from Type where TypeName='"+txt SmallClassAdd.T ext+"'",Conn);
int i=commTest.Exec uteNonQuery();
Conn.Close();
if(i>0)
Response.Write( "<script>alert( 'ÒÑÓÐͬÃû·ÖÀà´æ ÔÚ!');</script>");
else
{
string strInsert="inse rt into Type(TypeName,B igClassName,Big ID) Values ('"+txtSmallCla ssAdd.Text+"',' "+BigClassList. SelectedItem.Te xt+"','"+BigCla ssList.Selected Item.Value+"')" ;
Conn.Open();
OleDbCommand commAdd=new OleDbCommand(st rInsert,Conn);
commAdd.Execute NonQuery();
Conn.Close();
Response.Write( "<script>alert( '¶þ¼¶·ÖÀàÌí¼Ó³É ¹¦!');</script>");
}
BigClassDropDow nListInit();
SmallClassDropD ownListInit();
}
////////////////about the ascx fiels code here//
<td width="80%" valign="middle" bgcolor="#FFFFF F"><font color="#000000" >
¡¡ÔÚ:</font> <asp:dropdownli st ID="BigClassLis t" Width="100" runat="server" ></asp:dropdownlis t> <font color="#000000" >ÖÐÔö¼Ó¶þ¼¶À¸ Ä¿ </font> <asp:textbox ID="txtSmallCla ssAdd" Width="100" runat="server" ></asp:textbox> <asp:button ID="btnSmallCla ssAdd" OnClick="btnSma llClassAdd_Clic k" Text="Ìí¼Ó" runat="server" ></asp:button> </td>