C# 如何使用下拉列表在pageload处绑定gridview
我有一个按类别搜索的下拉列表。 我需要帮助在页面加载时绑定gridview,但同时,我还有一个select命令作为投票。 我知道pageload事件中存在诸如数据绑定之类的代码。但对于我的情况,我需要将select命令链接到一个按钮以更新投票。如果我对它进行数据绑定,我就无法获取数据键名称来更新投票计数器。 有没有办法绑定gridview,而不删除gridview本身中的DataSourceID 我的aspx代码如下C# 如何使用下拉列表在pageload处绑定gridview,c#,asp.net,visual-studio-2010,gridview,C#,Asp.net,Visual Studio 2010,Gridview,我有一个按类别搜索的下拉列表。 我需要帮助在页面加载时绑定gridview,但同时,我还有一个select命令作为投票。 我知道pageload事件中存在诸如数据绑定之类的代码。但对于我的情况,我需要将select命令链接到一个按钮以更新投票。如果我对它进行数据绑定,我就无法获取数据键名称来更新投票计数器。 有没有办法绑定gridview,而不删除gridview本身中的DataSourceID 我的aspx代码如下 <asp:SqlDataSource ID="SqlDataSource
<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>
您应该从GridView实现
row命令
事件。您已经有了命令字段
,因此请执行以下操作:
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
,因为您将使用选择按钮触发更新魔法。下面是带有selectIndexchange
的示例
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中选择的值。执行以下设置从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>
请注意,“投票”按钮的单击事件在数据源控件执行查询和检索数据之前触发。因此,一旦您按照当前的操作更新btnVote_click事件中的投票计数,就无需再次绑定数据。这部分代码对我来说似乎很好。我尝试了你的方法。。在sqlsoruce 2中添加控件样式后,我的下拉列表值消失..为什么这个soDropdownlist值消失?您正在使用“SqlDataSource2”在dropDownList中获取数据。那么,您可以检查第二个数据源是否确实正在获取数据吗?因为您没有对SqlDataSource2进行任何更改,所以它应该获取数据。
<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>
protected void btnVote_Click1(object sender, EventArgs e)
{
int i = CustomersGridView.SelectedIndex;
}