1

我已经创建了一个全局数据表,并且在页面加载事件中向它添加了列。现在我想在按钮单击事件中向它添加数据。当我按如下方式执行时,我收到一条错误消息......

列“catID”不属于表

解决方案是什么...我需要使用会话...?代码如下

    public partial class Default2 : System.Web.UI.Page
{
    DataTable dtSelectedSeats = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dtSelectedSeats.Columns.Add("catID", typeof(string));
            dtSelectedSeats.Columns.Add("seatID", typeof(string));
        }

    }
    protected void seat_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;

        if (button.BackColor == Color.Cyan)
        {
            button.BackColor = Color.Lime;

            addSeat(button.Text);
        }

    }

    private void addSeat(string seatNo)
    {
        DataRow dr;
        dr = dtSelectedSeats.NewRow();
        dr["catID"] = ddlCategory.SelectedItem.Value.ToString();
        dr["seatID"] = seatNo;
        dtSelectedSeats.Rows.Add(dr);
    }
}
4

4 回答 4

1

ASPX:

<asp:DropDownList ID="ddlCategory" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="seat" runat="server" BackColor="Cyan" onclick="seat_Click" Text="1" />
<asp:Button ID="Button1" runat="server" BackColor="Cyan" onclick="Button1_Click" Text="2" />
<asp:Button ID="Button2" runat="server" BackColor="Cyan" onclick="Button2_Click" Text="3" /><br />
<asp:GridView ID="GridView1" runat="server"/>

后面的代码:

protected void seat_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    if (button.BackColor == Color.Cyan)
    {
        button.BackColor = Color.Lime;
        addSeat(button.Text);
    }
}
private void addSeat(string seatNo)
{
    if (Session["dt"] == null)
    {
        Response.Write("DataTable not exist!");
        return;
    }
    DataTable dtSelectedSeats = (DataTable)Session["dt"];
    DataRow dr = dtSelectedSeats.NewRow();
    dr["catID"] = ddlCategory.SelectedItem.Value.ToString();
    dr["seatID"] = seatNo;
    dtSelectedSeats.Rows.Add(dr);
    GridView1.DataSource = dtSelectedSeats;
    GridView1.DataBind();
    Session["dt"] = dtSelectedSeats;
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dtSelectedSeats = new DataTable();
        dtSelectedSeats.Columns.Add("catID", typeof(string));
        dtSelectedSeats.Columns.Add("seatID", typeof(string));
        Session["dt"] = dtSelectedSeats;
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    seat_Click(sender, e);
}
protected void Button2_Click(object sender, EventArgs e)
{
    seat_Click(sender, e);
}
于 2013-09-08T06:08:41.107 回答
0

就在我的脑海中,看起来好像您在错误的事件中添加了行。我不确定您的 DataTable 在您向其添加列时是否已初始化(从而丢弃您的更改)。尝试将您的 PageLoad 代码放入 PagePrerender 中,看看这是否会给您带来更好的结果。

于 2013-09-08T04:59:01.563 回答
0

您的代码对于 DataTable 是完全正确的,您得到的错误可能是针对某些不同的问题。请正确说明您的错误

于 2013-09-08T03:59:09.973 回答
0

if (!IsPostBack) 只需在单击按钮时删除coz,页面就会返回。

于 2013-09-08T03:52:54.073 回答