如何使用C#将Label与ASP.net中的数据库绑定 [英] How to bind Label with Database in ASP.net with C#

查看:388
本文介绍了如何使用C#将Label与ASP.net中的数据库绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想用C#在ASP.net应用程序中将textBox1或Label与SQL Server绑定。

我想写这样的代码:



Hi every body,

I want to bind textBox1 or Label with SQL Server in ASP.net application with C#.
I want to write code like this:

textBox1.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");





但这个属性不适合我:( DataBindings)

只出现:( DataBind),(DataBinding)所以(Add方法)没有相应出现。



任何人都可以告诉我为什么这个属性不适合我(DataBindings)



请帮忙我

谢谢。



But this property not appear for me: (DataBindings )
appear that only: (DataBind) , (DataBinding) so (Add method) not appear accordingly.

Any one can tell me why this property not appear for me (DataBindings )

please help me
Thanks.

推荐答案

阅读 ASP.NET中的两个数据绑定 [ ^ ]



**更新**

您试图将WinForm数据绑定与WebForm一起使用,但这不起作用。

如果你读过这篇文章,你应该阅读标题为简单数据绑定 - 一个(简要)复习的部分,并看到 DataBinder.Eval()方法 [ ^ ]这是用于在WebForms中数据绑定控件的内容。



在CodeBehind中使用 DataBinder.Eval()方法;

Have a read of Two-Way Data Binding in ASP.NET[^]

** UPDATE **
You are trying to use WinForm data binding with a WebForm this does not work.
If you read the article you should have read the section entitled "Simple DataBinding - a (brief) refresher" and seen the DataBinder.Eval() Method[^] which is what is used to databind controls in WebForms.

To use the DataBinder.Eval() method in CodeBehind;
TextBox1.Text = DataBinder.Eval(ds.Tables["Products"].DefaultView[0], "ProductName").ToString();



此方法也有选项指定格式字符串;


This method also has the option to specify a format string;

TextBox2.Text = DataBinder.Eval(ds.Tables["Products"].DefaultView[0], "ProductPrice", "{0:c}"); //Display using currency format





但是,如果您想要做的只是根据数据库字段更改标签文本可以只使用 string.Format()喜欢;



If however all you want is to do is change the text of a label based on a database field you could just use string.Format() like;

Label1.Text = string.Format("{0}", ds.Tables["Products"].DefaultView[0]["Description"]);





请记住WebForm数据绑定与WinForm数据绑定的工作方式不同,并尝试理解其工作原理。



Keep in mind that WebForm databinding does not work the same way as WinForm databinding and attempt to comprehend how things work.


textbox1.text ='<%#Eval(columnname)%>'



标签或链接按钮文本相同
textbox1.text='<%#Eval("columnname")%>'

Same for label or linkbutton text


这篇关于如何使用C#将Label与ASP.net中的数据库绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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