帮助使用ASP.NET C#DataBinding [英] Help with ASP.NET C# DataBinding

查看:55
本文介绍了帮助使用ASP.NET C#DataBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在ASP.NET网页中有一个下拉控件,我用从数据库中检索到的数据填充了该下拉控件,在每次页面加载事件之后,数据再次重新绑定,因此增加了该下拉控件中的ListItems,这不是我想要,我只想将它绑定一次.

这是代码吗?

Hello guys,

I have a dropdown control in my ASP.NET web page, i populated the dropdown control with data retrieved from the database, after every page load event the data is re-binded again, thus increasing the ListItems in the dropdown control which is not what i want, i just want it binded once.

Here is the code?

protected void GetSVNumber(string vNumber)
   {
       SqlDataReader vreader = null;
       string vString = "SELECT ResponseDateTime FROM GpsResponseData WHERE SendersPhone = @senderphone";
       using (SqlConnection vconnection = new SqlConnection(GetConnectionString()))
       {
           SqlCommand vcommand = new SqlCommand(vString, vconnection);
           SqlParameter vParam = new SqlParameter("@senderphone", vNumber);
           vcommand.Parameters.Add(vParam);
           vcommand.CommandType = CommandType.Text;
           try
           {
               if (vconnection.State == ConnectionState.Open)
               {
                   vconnection.Close();
               }
               else
               {
                   vconnection.Open();
                   vreader = vcommand.ExecuteReader();
                   while (vreader.Read())
                   {
                       if (vreader.HasRows)
                       {
                           DateTime vResult = vreader.GetDateTime(0);
                           ddVehicleGpsTime.Items.Add(vResult.ToString());
                       }
                       else
                       {
                       lblError.Text = "The selected vehicle does not have any GPS data retreived for today";
                       }
                   }
               }
           }
           catch (SqlException vsqlex)
           {
               lblError.Text = vsqlex.Message;
           }
       }
   }


问题在于,当读者返回false时,不会显示lblError.Text中的文本.

谢谢大家


The problem is that, the text in the lblError.Text does not display when the reader return false.

Thanks guys

推荐答案

确保您检查Page.IsPostback只能在此方法为false时调用此方法

Make sure you checking Page.IsPostback can only calling this method when it is false

If(!IsPostback)
{
  GetSVNumber
}


这篇关于帮助使用ASP.NET C#DataBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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