我目前有两个下拉列表,并使用数据库中的名称填充它们。我想要做的是选择一个播放器,然后点击一下按钮,表格中的数据将被填充到gridview中。现在我只是收到“没有数据返回”的消息,我无法弄清楚原因。
<asp:DropDownList ID="ddl_QB1" runat="server" Width="200px" AppendDataBoundItems="True"
AutoPostBack="True" Height="16px" DataTextField="Player" DataValueField="id" ></asp:DropDownList>
<asp:Gridview ID="GridView1" runat="server" AutoGenerateColumns="false" Visible="true"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
<Columns>
<asp:TemplateField HeaderText="Total Points">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("[Pts]") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Pts") %>'>
</asp:Label>
</ItemTemplate>
protected void Page_Load(object sender, EventArgs e)
{
LoadQuarterbacks();
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("select [id], [Player], [Pts], [Att], [Cmp], [Yds], [TD] from Quarterbacks", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
ddl_QB1.DataSource = dt;
ddl_QB1.DataBind();
}
}
private void LoadQuarterbacks()
{
ddl_QB1.Items.Clear();
ddl_QB2.Items.Clear();
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("SELECT id, Player FROM Quarterbacks", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("Quarterbacks");
da.Fill(ds); // fill dataset
ddl_QB1.DataTextField = ds.Tables[0].Columns["Player"].ToString(); // text field name of table dispalyed in dropdown
ddl_QB2.DataTextField = ds.Tables[0].Columns["Player"].ToString();
ddl_QB1.DataValueField = ds.Tables[0].Columns["id"].ToString();
ddl_QB2.DataValueField = ds.Tables[0].Columns["id"].ToString(); // to retrive specific textfield name
ddl_QB1.DataSource = ds.Tables[0];
ddl_QB2.DataSource = ds.Tables[0];
ddl_QB2.DataBind();//assigning datasource to the dropdownlist
ddl_QB1.DataBind(); //binding dropdownlist
con.Close();
ddl_QB1.Items.Insert(0, new ListItem("--Select QuarterBack--", "0"));
ddl_QB2.Items.Insert(0, new ListItem("--Select QuarterBack--", "0"));
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionstring);
string query = "SELECT [id], [Player], [Pts], [Att], [Cmp], [Yds], [TD] FROM Quarterbacks where id=" + ddl_QB1.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter(query,con);
con.Open();
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
答案 0 :(得分:1)
以下3次更改会产生预期效果。
首先 - 在下拉列表元素上应该将AutoPostBack设置为false,因为即使在单击按钮之前也会导致回发。
第二 - 删除if(!Page.IsPostback)中的当前代码。此代码不是必需的。此外,此代码不为ddl_QB1和ddl_QB2设置DataTextField和DataValueField属性。
第三 - 将方法调用LoadQuarterbacks()置于if(!Page.IsPostback)中。我们不必在每个请求上绑定ddl_QB1和ddl_QB2的这些值。
如果下拉控件需要刷新,则在按钮单击结束时绑定gridview后调用LoadQuarterbacks()方法。这样,您可以在重新绑定它们之前从下拉列表中捕获所选值。重新绑定DropDowns会导致它们失去选定的值。