我是一名新的ASP.NET开发人员,我现在正在努力解决以下问题;我有一个具有多个GridView的面板控件。根据DropDownList中的选择,只显示一个GridView。
让我们假设我们有三个GridView,我们有一个带有三个选项的DropDownList; A,B和C.如果选择A,将显示第一个GridView。之后,如果我选择B,除了第一个显示的GridView之外,还将显示第二个GridView。我想要的是从下拉列表中选择任何选项后,应删除GridView和Label控件并替换为新结果。
那我怎么能这样做?
我的ASP.NET代码:
<asp:Panel ID="Panel1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:GridView ID="GridView1" runat="server" Visible="true"></asp:GridView>
<br />
<asp:GridView ID="GridView2" runat="server" Visible="true"></asp:GridView>
<br />
<asp:GridView ID="GridView3" runat="server" Visible="true"></asp:GridView>
<br /><br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</asp:Panel>
C#代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex != null)
{
int option = DropDownList1.SelectedIndex;
var myList2 = new List<string>();
myList2.Add("Test");
myList2.Add("Test");
var myList3 = new List<string>();
switch (option)
{
case 1:
if (myList2.Count > 0)
{
GridView1.DataSource = myList2;
GridView1.DataBind();
GridView1.Visible = true;
}
else
{
lblMessage.BackColor = System.Drawing.Color.Yellow;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "No Data!";
}
break;
case 2:
if (myList3.Count > 0)
{
GridView1.Visible = false;
GridView2.DataSource = myList3;
GridView2.DataBind();
GridView2.Visible = true;
}
else
{
lblMessage.BackColor = System.Drawing.Color.Yellow;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "No Data!";
}
break;
case 3:
if (myList2.Count > 0)
{
GridView1.Visible = false;
GridView2.Visible = false;
GridView3.DataSource = myList2;
GridView3.DataBind();
GridView3.Visible = true;
}
else
{
lblMessage.BackColor = System.Drawing.Color.Yellow;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "No Data!";
}
break;
default:
break;
}
}
}
注意:正如你在上面的C#中看到的那样,在switch语句的每种情况下,我都删除了前面案例的控件,但这不是正确的方法,因为我需要在选择后删除GridView或Label控件下拉列表中的任何项目。那么有什么方法可以解决这个问题。
答案 0 :(得分:1)
按照此处所述使用MultiView控件:
像这样更改页面:
<asp:Panel ID="Panel1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:MultiView ID="MainMultiview" runat="server">
<asp:View ID="View1" runat="server">
GridView 1
<asp:GridView ID="GridView1" runat="server" Visible="true"></asp:GridView>
<br />
</asp:View>
<asp:View ID="View2" runat="server">
GridView 2
<asp:GridView ID="GridView2" runat="server" Visible="true"></asp:GridView>
<br />
</asp:View>
<asp:View ID="View3" runat="server">
GridView 3
<asp:GridView ID="GridView3" runat="server" Visible="true"></asp:GridView>
<br />
</asp:View>
</asp:MultiView>
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</asp:Panel>
并将C#类后面的代码更改为:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex != null)
{
int option = DropDownList1.SelectedIndex;
var myList2 = new List<string>();
myList2.Add("Test");
myList2.Add("Test");
var myList3 = new List<string>();
switch (option)
{
case 1:
if (myList2.Count > 0)
{
//GridView1.DataSource = myList2;
//GridView1.DataBind();
//GridView1.Visible = true;
MainMultiview.SetActiveView(View1);
}
else
{
lblMessage.BackColor = System.Drawing.Color.Yellow;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "No Data!";
}
break;
case 2:
if (myList2.Count > 0)
{
//GridView1.Visible = false;
//GridView2.DataSource = myList3;
//GridView2.DataBind();
//GridView2.Visible = true;
MainMultiview.SetActiveView(View2);
}
else
{
lblMessage.BackColor = System.Drawing.Color.Yellow;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "No Data!";
}
break;
case 3:
if (myList2.Count > 0)
{
//GridView1.Visible = false;
//GridView2.Visible = false;
//GridView3.DataSource = myList2;
//GridView3.DataBind();
//GridView3.Visible = true;
MainMultiview.SetActiveView(View3);
}
else
{
lblMessage.BackColor = System.Drawing.Color.Yellow;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "No Data!";
}
break;
default:
break;
}
}
}
注意gridview无法绑定到字符串列表。但多视图的工作很有意义。