数据表列不可访问 C# ASP.NET

发布于 2024-09-13 13:30:35 字数 1143 浏览 11 评论 0原文

我有这个数据表

public partial class class1
{
  private DataTable dt;
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           dt.Columns.Add("Col1", System.Type.GetType("System.String"));
           dt.Columns.Add("Col2", System.Type.GetType("System.String"));
           bind();
        }
    }
}

private void bind()
{
    //database call
    //loop 
    dt.Rows.Add(col1_value.ToString(), col2_value.ToString(), col3.ToString());
    // populate the dropdown list with the database entries
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DataRow[] datarow;
   variable1=  DropDownList1.SelectedValue;
   datarow = dt.Select( dt.Columns["col1"] + DropDownList1.SelectedValue);
   variable2 = datarow.GetValue(1).ToString();
}

当调用 selectedindexchange 事件时,它在行中显示错误 datarow = dt.Select( dt.Columns["col1"] + DropDownList1.SelectedValue);,表示该列不存在。当执行 selectedIndexChanged 时,数据表中的所有行和列都会从数据表中丢失。我在这里缺少什么?

我想避免将数据表存储在会话或视图状态中。另外,问题是为什么数据表会变空。有没有办法避免重新填充数据表中的所有内容? 如果我有一个字符串类变量,它在回发后不会变空,或者会变空。

I have this datatable

public partial class class1
{
  private DataTable dt;
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           dt.Columns.Add("Col1", System.Type.GetType("System.String"));
           dt.Columns.Add("Col2", System.Type.GetType("System.String"));
           bind();
        }
    }
}

private void bind()
{
    //database call
    //loop 
    dt.Rows.Add(col1_value.ToString(), col2_value.ToString(), col3.ToString());
    // populate the dropdown list with the database entries
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DataRow[] datarow;
   variable1=  DropDownList1.SelectedValue;
   datarow = dt.Select( dt.Columns["col1"] + DropDownList1.SelectedValue);
   variable2 = datarow.GetValue(1).ToString();
}

When the selectedindexchange event is called, it displays an error in the line
datarow = dt.Select( dt.Columns["col1"] + DropDownList1.SelectedValue);, saying the column does not exist. All the rows and columns from the datatable are lost from the datatable when the selectedIndexChanged is executed. What am I missing here?

I want to avoid storing the datatable in session or viewstate. Also, the question is why does the datatable becomes empty. Is there anyway to avoid repopulating the datatable everything there is a post back?
If I have a class variable of string that doesnt become empty after a postback or does it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-09-20 13:30:35

删除 if (!IsPostBack) ....您每次回发时都必须填充数据表 dt,因为您没有将其存储在视图状态或会话中的任何位置...

Remove the if (!IsPostBack) .... You have to populate the datatable dt everytime you postback since you aren't storing it anywhere in viewstate or session....

不语却知心 2024-09-20 13:30:35

您正在 Page_Load 事件中以编程方式添加列。 SelectedIndexChanged 事件会导致回发,并且 Page_Load 中的逻辑仅在请求不是回发时添加列。这意味着这些列不会在每次回发时添加回来,如果您想以编程方式添加它们,则必须添加这些列。

You are adding the columns programmatically in your Page_Load event. The SelectedIndexChanged event causes a postback, and your logic in the Page_Load only adds the columns when the request is NOT a postback. This means that the columns are not added back on each postback, which they must be if you want to add them programmatically.

戈亓 2024-09-20 13:30:35

这可能是因为您的条件(if (!IsPostBack))、indexchanged事件导致回发,因此列永远不会添加到数据表中。
您应该摆脱这种情况或将数据表填充到另一个位置(甚至可能是索引更改事件的事件处理程序)。

It may be because of your condition (if (!IsPostBack)), indexchanged event causes postback, so columns are never added to the datatable.
You should get rid of this condition or populate your datatable in another place (maby even your eventhandler for indexchanged event).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文