English 中文(简体)
列表框返回选中的 Index 更改事件上的空字符串
原标题:Listbox Returns Empty String on SelectedIndexChanged Event

以上错误发生在列表框中选中的 Index 已更改的点击事件 。

在调试返回的值时,返回的值是“,”但当查看网页源代码时,肯定有数值。

在这里,我的列表框 :

<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();
            }
        }

    }

如果我回到 SqlData 数据库, 来源列表框返回值。 Bt 我需要重新配置列表, 所以我需要数据集背后的代码( 除非有更好的方法) 。

那么为什么列表框选中的值返回一个空字符串? 有任何帮助会收到吗?

最佳回答

您有属性 AutoPostBack = “ true” = “ true”, 这将对每个被选中的索引进行修改后进行回溯 。 因此, 在它转到选中的索引更改事件之前, 它必须按页面负载键, 您将列表框绑在一起。 因此, 当它点击页面负载时, 选中的索引将被修改为默认项目 。 因此, 请尝试以下代码 。

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             //Bind your listbox here
            }
         }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签