我有一个GridView,它显示了一个餐馆列表和它来自我的数据库的数据。在最后一栏我有一个ButtonField
。当用户点击按钮时,RowCommand
将启动,另一个GridView将显示所选餐厅的评论。
我遇到的问题是,当RowCommand事件被触发时,第二个GridView gvReviews
根本没有出现。
visible
设置为true,但这似乎不起作用。TemplateField
与Button
相反,但这也不起作用。if (!IsPostback)
声明这是我的GridViews片段:
<asp:GridView ID="gvRestaurants" runat="server" AutoGenerateColumns="false" OnRowCommand="gvRestaurants_RowCommand">
<Columns>
<asp:BoundField DataField="RestaurantID" HeaderText="ID" />
<asp:BoundField DataField="RestName" HeaderText="Restaurant" />
<asp:BoundField DataField="RestAddr" HeaderText="Address" />
<asp:BoundField DataField="RestCity" HeaderText="City" />
<asp:BoundField DataField="RestState" HeaderText="State" />
<asp:BoundField DataField="RestZip" HeaderText="Zip Code" />
<asp:BoundField DataField="CategoryDesc" HeaderText="Category" />
<asp:ButtonField HeaderText="Reviews" CommandName="viewReviews" Text="View" ButtonType="Button" />
</Columns>
</asp:GridView>
<asp:GridView ID="gvReviews" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RestaurantID" HeaderText="ID" />
<asp:BoundField DataField="RestName" HeaderText="Restaurant" />
<asp:BoundField DataField="ReviewDate" HeaderText="Date of Review" />
<asp:BoundField DataField="FoodQuality" HeaderText="Food Quality" />
<asp:BoundField DataField="ServiceRating" HeaderText="Service" />
<asp:BoundField DataField="AtmosphereRating" HeaderText="Atmosphere" />
<asp:BoundField DataField="PriceRating" HeaderText="Price" />
<asp:BoundField DataField="ReviewText" HeaderText="Review" />
</Columns>
</asp:GridView>
这是aspx.cs的片段
protected void btnSearch_Click(object sender, EventArgs e)
{
gvRestaurants.Visible = true;
gvAllRestaurants.Visible = false;
DataSet ds = p.SearchByCategory(ddCategories.SelectedItem.Value, ddCategories2.SelectedItem.Value);
gvRestaurants.DataSource = ds;
gvRestaurants.DataBind();
}
protected void gvRestaurants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "viewReviews")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvRestaurants.Rows[index];
int restID = int.Parse(row.Cells[0].Text);
gvReviews.DataSource = p.GetReview(restID);
gvReviews.DataBind();
gvRestaurants.Visible = false;
gvReviews.Visible = true;
}
else
{
string error = "There are no reviews for this restaurant.";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
}
这是我使用的方法的片段:
public DataSet SearchByCategory(string category1, string category2)
{
DBConnect objDB = new DBConnect();
objCmd.Parameters.Clear();
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.CommandText = "GetRestaurantByCategory";
SqlParameter sqlParameter = new SqlParameter("@theCategory", category1);
SqlParameter sqlParameter2 = new SqlParameter("@theCategory2", category2);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.SqlDbType = SqlDbType.VarChar;
sqlParameter.Size = 50;
sqlParameter2.Direction = ParameterDirection.Input;
sqlParameter2.SqlDbType = SqlDbType.VarChar;
sqlParameter2.Size = 50;
objCmd.Parameters.Add(sqlParameter);
objCmd.Parameters.Add(sqlParameter2);
objDB.GetConnection().Open();
DataSet ds = objDB.GetDataSetUsingCmdObj(objCmd);
objDB.CloseConnection();
return ds;
}
public DataSet GetReview(int restaurant)
{
DBConnect objDB = new DBConnect();
objCmd.Parameters.Clear();
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.CommandText = "GetReviewByRestaurantID";
SqlParameter sqlParameter = new SqlParameter("@theRestaurantID", restaurant);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.SqlDbType = SqlDbType.Int;
sqlParameter.Size = 4;
objCmd.Parameters.Add(sqlParameter);
objDB.GetConnection().Open();
DataSet ds = objDB.GetDataSetUsingCmdObj(objCmd);
objDB.CloseConnection();
return ds;
}
这是我使用的存储过程:
CREATE PROCEDURE [dbo].GetRestaurantByCategory
@theCategory varchar(50),
@theCategory2 varchar(50)
AS
SELECT rest.RestaurantID, rest.RestName, rest.RestAddr, rest.RestCity, rest.RestState, rest.RestZip, cat.CategoryDesc
FROM Restaurants rest JOIN Categories cat ON rest.CategoryID = cat.CategoryID
WHERE CategoryDesc = @theCategory OR CategoryDesc = @theCategory2
CREATE PROCEDURE [dbo].GetReviewByRestaurantID
@theRestaurantID int
AS
SELECT rest.RestaurantID, rest.RestName, rev.ReviewDate, rev.FoodQuality, rev.ServiceRating, rev.AtmosphereRating, rev.PriceRating, rev.ReviewText
FROM Restaurants rest JOIN Reviews rev ON rest.RestaurantID = rev.RestaurantID
WHERE Rest.RestaurantID = @theRestaurantID
你的GridView
没有显示,因为你将它绑定到DataSet
而不是直接绑定到这个DataTable
中的DataSet
。
尝试更改此行:
gvReviews.DataSource = p.GetReview(restID);
对此:
gvReviews.DataSource = p.GetReview(restID).Tables[0];
使用此查询
SELECT rest.RestaurantID, rest.RestName, rev.ReviewDate, rev.FoodQuality, rev.ServiceRating, rev.AtmosphereRating, rev.PriceRating, rev.ReviewText
FROM Restaurants rest JOIN Reviews rev ON rev.RestaurantID = rest.RestaurantID
WHERE Rest.RestaurantID = @theRestaurantID