我有一个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();
}
答案 0 :(得分: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();
}
}