【问题标题】:ASP.NET Gridview textbox binding to a variable at runtimeASP.NET Gridview 文本框在运行时绑定到变量
【发布时间】:2014-11-05 21:18:01
【问题描述】:

我是 ASP.NET 的新手,我想知道以下是否可行。 我有一个使用 OldDbDataAadpter 绑定到数据源(MS Access)的应用程序,并且工作正常但是,我希望能够根据用户输入(从下拉列表中)显示多个表字段中的一个。这个想法是用户在运行时选择一种(可能的 10 种)语言,并让 Gridview 仅显示该字段。
如果您暂时可以忽略数据库的结构(这是一个遗留问题),该表有许多字段(索引、名称等),然后是 10 种语言之一的字段(例如 en-我们,de,es,fr,等等..)。
以下部分工作正常(变量“lang”在运行时从下拉列表中填充)

       string qryStr = "select PIndex, TName, Component, " + lang  " from MyTbles";


        GlobalClass.adap = new OleDbDataAdapter(qryStr, con);

        OleDbCommandBuilder bui = new OleDbCommandBuilder(GlobalClass.adap);

        GlobalClass.dt = new DataTable();

        GlobalClass.adap.Fill(GlobalClass.dt);

        GridView1.DataSource = GlobalClass.dt;

        GridView1.DataBind();

GlobalClass.cs如下: 公共课 GlobalClass {

    public static OleDbDataAdapter adap;

    public static DataTable dt;

    // Stored image path before updating the record

}

问题是 ASP 代码在绑定时似乎需要 EXACT 字段名。这是 ASP 文件的一部分:

 Text='<%#Bind("Component") %>'

这显示了“组件”字段如何绑定到 Gridview 文本框。我的问题是: 是否可以在上面的“绑定”部分中使用变量名?我希望这个 ASP 代码能够从后面部分的 .CS 代码中找到并解析一个变量。
我不希望任何人向我展示如何,但我想知道这是否可能(以及我应该查看的相关课程..) 非常感谢..

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc gridview


    【解决方案1】:

    为什么不这样做

    string qryStr = "select PIndex, TName, Component, " + lang " as SelectedLang from MyTbles";
    

    然后

    Text='&lt;%#Bind("SelectedLang") %&gt;'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多