上述錯誤發生在列表框上的SelectedIndexChanged click事件上。ListBox返回SelectedIndexChanged事件中的空字符串
在調試中返回的值是「」,但是當您查看網頁源代碼時,確實存在值。
這裏是我的列表框:
<asp:ListBox ID="lstBxMyList" runat="server" CssClass="binorderlst1"
DataTextField="myName"
DataValueField="myID"
OnSelectedIndexChanged ="lstBxMyList_SelectedIndexChanged"
AutoPostBack="true" Rows="10">
</asp:ListBox>
這裏的事件:
protected void lstBxMyList_SelectedIndexChanged(object sender, EventArgs e)
{
myID = Convert.ToInt32(lstBxSiteList.SelectedValue.ToString());
... rest of code
}
爲了完整這裏的數據綁定:
private void BindLstBxMyList(int myOtherID)
{
DataTable dt = new DataTable();
SqlConnection conn;
SqlCommand comm;
using (conn = new SqlConnection(aSHconns.aconn))
{
comm = new SqlCommand("myStoredProc", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@myOtherID", SqlDbType.Int));
comm.Parameters["@myOtherID"].Value = myOtherID;
SqlDataAdapter sqlDa = new SqlDataAdapter(comm);
try
{
conn.Open();
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
lstBxMyList.DataSource = dt;
lstBxMyList.DataTextField = "myName";
lstBxMyList.DataValueField = "myID";
lstBxMyList.DataBind();
}
}
finally
{
conn.Close();
}
}
}
如果我恢復到一個SqlDataSource列表框的回報值。 Bt我需要重新填充列表,所以我需要數據綁定背後的代碼(除非有更好的方法)
那麼,爲什麼listbox的選擇值返回一個空字符串呢?任何幫助將受到感謝。
你可以顯示你從哪裏調用BindLstBxMyList的代碼? –
您是否在每次回發時調用BindLstBxMyList或檢查Page.IsPostBack。 –
我在每次回發的Page_Load事件中調用它,因爲我希望它每次都刷新。 – ComfortablyNumb