如何使用C#在ASP.NET中创建查找 [英] How to create a lookup in ASP.NET using C#

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

问题描述







我想创建一个像这样的查找函数示例。但我不太了解vb,我也没有看到有关该Lookup工作流程的更多信息。所以我很难理解。现在我的项目需要在C#上进行这种查找。请帮忙 。我尝试过自己的逻辑。请参阅下面的代码,它显示错误为

Hi ,


I would like to create a lookup function just like this Example . But i dont know much about vb and i dont see much information about the workflow of that Lookup . so its hard for me to understand .Now My project needs this kind of Look up to be done on C#. Please Help . I have tried with my own logic . Please see my code Below and it shows error as "

object is not set to an instance of an object







我的尝试:




"

What I have tried:

public static void fill(string property)
        {
            Page page = (Page)HttpContext.Current.Handler;
            TextBox TenancyAddress = (TextBox)page.FindControl("TenancyAddress");
            TenancyAddress.Text = property;
        }

        [WebMethod]
        public static void LookupUpdate(string id)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [godaddy].[dbo].[Property] where [Prop_id] = " + id, con);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                Page page = (Page)HttpContext.Current.Handler;
                TextBox TenancyAddress = (TextBox)page.FindControl("TenancyAddress");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                 string property = ds.Tables[0].Rows[i]["PropertyName"].ToString();
                    fill(property);
                }
            }
            con.Close();
            



        }

推荐答案

CHill60是正确的。 FindControl无法在页面上找到控件。这意味着它不存在。



为什么你不能用Id直接访问控件?如果你在控件上声明了runat = server,那么你可以直接在后面的代码上访问它。
CHill60 is correct. The FindControl could not able to find the control on page. That means it does not exist.

Why can't you directly access the control with Id? If you have runat=server declared on the control, then you can access it directly on code behind.


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

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