0

我有一个 GridView,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="LastName" HeaderText="LastName" 
                    SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="ProgramName" HeaderText="ProgramName" 
                    SortExpression="ProgramName" />
            </Columns>
 </asp:GridView>

这是我用来刷新 GridView 的 ListBox 方法,

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {        
        if (ListBox1.SelectedValue == "Computer Programmer")
        {
          DataSet test =  GetStudentByProgramID(1);
          if (test.Tables.Count > 0)
          {
              GridView1.Columns.Clear();
              GridView1.DataSource = test;
              GridView1.Attributes.Add("style", "table-layout:fixed");
              //ISBNColumn
              BoundField LastName = new BoundField();
              //LastName.DataField = test.Tables.r.ColumnName;
              LastName.HeaderText = "ISBN";
              LastName.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
              GridView1.Columns.Add(LastName);
              //CopyNumberColumn
              BoundField FirstName = new BoundField();
              //FirstName.DataField = myitems.Items.CopyNumberColumn.ColumnName;
              FirstName.HeaderText = "Copy Number";
              FirstName.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
              GridView1.Columns.Add(FirstName);
              GridView1.DataBind();
          }

        }
    }

但是当我从 ListBox 中选择值时,GridView 不会刷新。我在这里犯了什么错误?我的列表框也像这样被填满,

DataSet ab = GetProgramList();
        ListBox1.Items.Clear();


        if (ab.Tables[0].Rows.Count > 0)
        {
        ListBox1.DataSource = ab;
        ListBox1.DataTextField ="ProgramName";
        ListBox1.DataValueField ="ProgramName";
        ListBox1.DataBind();
        }
4

1 回答 1

2

确保您的列表框有

AutoPostBack="True"

<asp:ListBox id="ListBox1" 
               Rows="4"
               AutoPostBack="True" 
               SelectionMode="Single"  
               runat="server">

if(!IsPostBack)
{
        DataSet ab = GetProgramList();
        ListBox1.Items.Clear();


        if (ab.Tables[0].Rows.Count > 0)
        {
        ListBox1.DataSource = ab;
        ListBox1.DataTextField ="ProgramName";
        ListBox1.DataValueField ="ProgramName";
        ListBox1.DataBind();
        }
}
于 2012-07-24T03:43:22.843 回答