ASP.NET数据绑定/从代码隐藏获取变量 [英] ASP.NET databinding / getting variables from codebehind

查看:31
本文介绍了ASP.NET数据绑定/从代码隐藏获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经三年左右没有使用ASP.NET了,对此我感到非常生锈。我的旧代码无法在旧公司中查看。这个问题应该很基本,但是我在这个问题上找不到任何好的,可靠的或不是很陈旧的资源,所以我在这里问。

So I haven't used ASP.NET for three years or so and I'm really rusty on it. My old code isn't available to me for review (at an old company). This question should be pretty basic, but I can't find any good or reliable or not-super-old resources on the issue, so I'm asking here.

是否可以再次获得有关数据绑定的一般概述?我记得它对于选择框等非常有用,但是我真的不记得它是如何工作的。总体上来说,这可能是一部不错的ASP.NET教程,因为我不记得它是如何处理POST请求或类似的东西的。我应该只尝试使用ASP.NET MVC吗?

Can I get a general overview of databinding again? I remember it being really useful for select boxes, etc., but I don't really remember how it works. Maybe a good ASP.NET tutorial in general, because I don't remember how it handles POST requests or anything like that really either. Should I just try ASP.NET MVC?

相关地,假设我的代码隐藏页中有一个公共变量。现在,我通过在页面加载函数末尾说 Page.DataBind()来访问它,然后运行<%#变量%> ; 在ASPX中,但这不是我以前记得做的方式,我认为这不是很好的做法。从代码隐藏中显示变量的最佳方法是什么?

Relatedly, suppose I have a public variable in my codebehind page. Right now I am accessing it by saying Page.DataBind() at the end of the page load function and then running <%# variable %> in the ASPX, but that's not how I remember doing it before and I reckon that it's not very good practice. What's the best way to display variables from codebehind?

推荐答案

通常,数据绑定(至少在WebForms模型中)是大多数情况分配要显示的字段,将DataSource属性设置为包含这些字段的合适对象,例如一个DataReader,DataTable,一个Collection,并调用DataBind方法。因此,对于您的 select 案例,您要放置< asp:dropdownlist runat = server id = MyDropDownList> 在页面的标记中,然后在代码中

Databinding in general (at least in the WebForms model) is mostly a case of assigning fields to be displayed, setting the DataSource property to a suitable object that contains those fields e.g. a DataReader, DataTable, a Collection, and calling the DataBind method. So for your select case, you'd put an <asp:dropdownlist runat="server" id="MyDropDownList"> in the markup for the page, and then in the code

DataSet myDataSet;

myDataSet = someDataMethod();

MyDropDownList.DataTextField = fieldname;
MyDropDownList.DataValueField = fieldname;
MyDropDownList.DataSource = myDataSet;
MyDropDownList.DataBind();

或者,如果您使用DataSource控件,则可以避免编写此类代码并在标记中执行例如< asp:SqlDataSource> < asp:ObjectDataSource>

Or you can avoid writing that kind of code and do it in the markup if you use a DataSource control e.g. <asp:SqlDataSource>, <asp:ObjectDataSource>

<asp:SqlDataSource runat="server" id="MySqlDataSource" ConnectionString="aConnectionString" SelectCommand="MyStoredProcName" SelectCommandType="StoredProcedure"  />
<asp:dropdownlist runat="server" id="MyDropDownList" DataSourceId="MySqlDataSource" DataTextField="fieldname" DataValueField="fieldname">

要在页面上放置变量,您以前可能要做的就是使用标签或页面上的文本框,即在代码背后,将变量分配给Text属性,例如

For putting your variable on a page, the way you might have done it before is to have a label or textbox on the page, that in your code-behind you assign your variable to the Text property e.g.

<asp:label runat="server" id="MyLabel" />

MyLabel.Text = myVariable.ToString();

回发:您可以测试 IsPostback 属性代码隐藏页面的大小,以确定它是否是回发。在Page_Load方法之后,如果您定义了其他方法,则将触发其他方法,例如一个DropDownList的SelectedIndexChanged。

Postbacks: you can test the IsPostback property of a page in code-behind to determine if it's a postback or not. After the Page_Load method, other methods will fire if you've defined them e.g. SelectedIndexChanged for a DropDownList.

这篇关于ASP.NET数据绑定/从代码隐藏获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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