如何将页面加载时的网格视图与下拉列表绑定
本文关键字:视图 网格 下拉列表 绑定 加载 | 更新日期: 2023-09-27 18:24:19
我有一个下拉列表,可以按类别进行搜索。我需要帮助在页面加载时绑定我的网格视图,但同时,我还有一个选择命令作为投票。我知道pageload事件中有一些代码,比如Databinding。但对于我的情况,我需要将select命令链接到一个按钮来更新投票。如果我对它进行数据绑定,我就无法获取数据密钥名称来更新我的投票计数器。有没有任何方法可以在不删除gridview本身中的DataSourceID的情况下绑定gridview?
我的aspx代码如下。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Review] WHERE ([Category] = @Category)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCat" Name="Category"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Category] FROM [ReviewCategory]">
</asp:SqlDataSource>
<asp:DropDownList ID="ddlCat" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Category"
DataValueField="Category" AutoPostBack="True"
onselectedindexchanged="SelectionChange">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" Width="1114px"
Height="272px" AutoGenerateColumns="False" PageSize="5"
DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="ReviewID">
<Columns>
<asp:BoundField DataField="Votes" HeaderText="Votes"
SortExpression="Votes" />
<asp:BoundField DataField="Category" HeaderText="Category"
SortExpression="Category" />
<asp:CommandField SelectText="VOTE as your FAVOURITE!"
ShowSelectButton="True" />
</Columns>
c#代码
protected void btnVote_Click(object sender, EventArgs e)
{
int reviewid = Convert.ToInt16(GridView1.SelectedDataKey.Value);
SqlConnection conn = new SqlConnection(@"Data Source=.'SQLEXPRESS;AttachDbFilename=|DataDirectory|'Database.mdf;Integrated Security=True;User Instance=True");
string sqlstmt = "select Votes from Review where ReviewID = '" + reviewid + "'";
SqlCommand comm = new SqlCommand(sqlstmt, conn);
try
{
conn.Open();
SqlDataReader read = comm.ExecuteReader();
if (read.Read())
{
int votes = (int)read["Votes"];
votes += 1;
string updatestatement = "Update Review set Votes= '" + votes + "' Where ReviewID = '" + reviewid + "'";
SqlCommand command = new SqlCommand(updatestatement, conn);
read.Close();
command.ExecuteNonQuery();
}
}
finally {
conn.Close();
GridView1.DataBind();
}
}
protected void SelectionChange(object sender, EventArgs e)
{
int stored = ddlCat.SelectedIndex;
if (stored == 0)
{
SqlDataSource1.SelectCommand = "SELECT * from Review ORDER BY [Votes] DESC ";
}
else { }
}
您应该从GridView实现RowCommand
事件。你已经有了CommandField
,所以做这样的事情:
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
//
// Get the keys from the selected row
//
LinkButton lnkBtn = (LinkButton)e.CommandSource; //the button
GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent; //the row
GridView myGrid = (GridView)sender; // the gridview
int reviewid = Convert.ToInt32(GridView1.DataKeys[myRow.RowIndex].Value); //value of the datakey **strong text**
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
// In this case you are pressing the button Select, as ou already
// defined this at the aspx code.
if(e.CommandName=="Select")
{
// Put the logic from btnVote_Click here
}
}
另一种方法可以是实现SelectIndexChanging
或SelectIndexChanged
,假设您将使用Select按钮来激发更新魔法。这里是SelectIndexChanging
。
void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
int reviewid = Convert.ToInt32(GridView1.DataKeys[e.NewSelectedIndex].Value); //value of the datakey **strong text**
// Put the logic from btnVote_Click here
}
让我们逐一了解您的需求:
1.)*使用DropDownList在PageLoad绑定GridView:
在这种情况下,您需要检索在下拉列表中选择的值。执行以下设置以从DropDownList 获取值
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Category] FROM [ReviewCategory] where Category=@Category">
<SelectParameters><asp:ControlParameter ControlID="ddlCat" Name="Category"
PropertyName="SelectedValue" /></SelectParameters>
</asp:SqlDataSource>
正在发生什么:
- 每次在下拉列表中选择一个值时,都会进行Postback(AutoPostback="true")
- Page.PreRender事件之后,DataSource控件[SqlDatSource here]执行所需的查询并检索数据。因此,SqlDataSource将使用选定的DropDownList值。因此,无需担心以任何方式更改/操纵DataSourceID
2.)"但对于我的情况,我需要将select命令链接到更新投票的按钮"'
在这种情况下,您在网格视图内有一个选择按钮,在网格视图外有一个"投票"按钮,但在页面的某个位置。因此,一旦您在网格视图中选择了任何一行,请单击"投票"按钮。您可以像往常一样访问SelectedRow和Index。
protected void btnVote_Click1(object sender, EventArgs e)
{
int i = CustomersGridView.SelectedIndex;
}
请注意,"Vote"按钮的Click事件在DataSource控件执行查询之前激发&检索数据。因此,一旦您像当前一样更新btnVote_click事件中的投票计数,就不需要再次绑定数据。你的这部分代码对我来说似乎很好。