网格视图页面索引点击问题(每次都加载相同的页面)。 [英] Grid View Page index click problem(same page load every time).

查看:58
本文介绍了网格视图页面索引点击问题(每次都加载相同的页面)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的整个代码..



Hi,
This my whole code..

////////////
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Admin_deletescore : System.Web.UI.Page
{
string username = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["UserName"] != null)
{
username = Convert.ToString(Session["UserName"]);
lblUser.Text = "Welcome" + ' ' + username;
}
else
{
Response.Redirect("login.aspx");
}
bindTournament();
bindRecentMatches();
}
}
private void bindTournament()
{
try
{
string str = "select_t_by_organizer_name";
SqlConnection objCon;
SqlCommand objCmd;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(str, objCon))
{
if (username == "vipin")
{
GetTournament();
}
else
{
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.AddWithValue("@eventorganizer", Convert.ToString(Session["UserName"]));
using (objDa = new SqlDataAdapter(objCmd))
{
objDa.Fill(objDs);
if (objDs.Tables[0].Rows.Count > 0)
{
if (grdlistTeam != null)
{
ddlComp.DataSource = objDs.Tables[0].DefaultView;
ddlComp.DataTextField = "tname";
ddlComp.DataValueField = "tid";
ddlComp.DataBind();
ddlComp.Items.Insert(0, new ListItem("Select Tournament"));
}
}
else
{
}
}
}
}
}
}
catch (Exception ex)
{
}
}
private void GetTournament()
{
SqlConnection objCon;
SqlCommand objCmd;
string strQuery;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
int i = 0;
try
{
strQuery = "select * from tdescription order by TDescription.TStartDate ASC";
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(strQuery, objCon))
{
using (objDa = new SqlDataAdapter(objCmd))
{
i = objDa.Fill(objDs);
if (i > 0)
{
ddlComp.DataSource = objDs;
ddlComp.DataTextField = "TName";
ddlComp.DataValueField = "TtId";
ddlComp.DataBind();
ddlComp.Items.Insert(0, new ListItem("--Select Tournament--", "0"));
}
}
}
}
}
catch (Exception ex)
{
Exceptionhandle exception = new Exceptionhandle();
exception.Exceptionhandle1(ex.ToString());
}
}
private void bindRecentMatches()
{
SqlConnection objCon;
SqlCommand objCmd;
string strQuery;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
int i = 0;
try
{
strQuery = "select_recent_match_by_eventorganizer";
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(strQuery, objCon))
{
objCmd.CommandType = CommandType.StoredProcedure;
//objCmd.Parameters.AddWithValue("@eventorganizer", username);
objCmd.Parameters.AddWithValue("@eventorganizer", Convert.ToString(Session["UserName"]));
using (objDa = new SqlDataAdapter(objCmd))
{
// try {
i = objDa.Fill(objDs);
if (objDs.Tables[0].Rows.Count > 0)
{
if (grdlistTeam != null)
{
grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.DataBind();
}
}
else
{
}
//} catch (Exception ej) { throw ej; }
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void grdlistTeam_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
if (e.Row.RowType != DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
cell.Attributes.Add("Style", "border-bottom:5px solid #e1dfe0;");
}
Label lblDate = (Label)e.Row.FindControl("lbldate");
string dtCust = ConvertToDate(lblDate.Text);
lblDate.Text = dtCust;
Label lblResult = (Label)e.Row.FindControl("lblResult");
if (lblResult.Text.Length > 35)
{
lblResult.ToolTip = lblResult.Text;
lblResult.Text = lblResult.Text.Substring(0, 35) + "..";
}
else
{
lblResult.Text = lblResult.Text;
lblResult.ToolTip = lblResult.Text;
}
LinkButton lnkFirstTeam = (LinkButton)e.Row.FindControl("lnkFirstTeam");
if (lnkFirstTeam.Text.Length > 15)
{
lnkFirstTeam.ToolTip = lnkFirstTeam.Text;
lnkFirstTeam.Text = lnkFirstTeam.Text.Substring(0, 15) + "..";
}
else
{
lnkFirstTeam.Text = lnkFirstTeam.Text;
lnkFirstTeam.ToolTip = lnkFirstTeam.Text;
}
LinkButton lnkSecondTeam = (LinkButton)e.Row.FindControl("lnkSecondTeam");
if (lnkSecondTeam.Text.Length > 15)
{
lnkSecondTeam.ToolTip = lnkSecondTeam.Text;
lnkSecondTeam.Text = lnkSecondTeam.Text.Substring(0, 15) + "..";
}
else
{
lnkSecondTeam.Text = lnkSecondTeam.Text;
lnkSecondTeam.ToolTip = lnkSecondTeam.Text;
}
LinkButton lnkTournament = (LinkButton)e.Row.FindControl("lnkTournament");
if (lnkTournament.Text.Length > 9)
{
lnkTournament.ToolTip = lnkTournament.Text;
lnkTournament.Text = lnkTournament.Text.Substring(0, 9) + "..";
}
else
{
lnkTournament.Text = lnkTournament.Text;
lnkTournament.ToolTip = lnkTournament.Text;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
public static string ConvertToDate(string DateString)
{
string[] strArr = DateString.Split('-');
string strMM = strArr[0];
string strDD = strArr[1];
string strYYYY = strArr[2];
DateString = strDD + " " + strMM + "," + strYYYY;
return DateString;
}
protected void grdlistTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdlistTeam.PageIndex = e.NewPageIndex;
bindRecentMatches();
}
protected void ddlComp_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
// string str = "select * from currentmatch where tid=@tid order by EventDate ASC";
string str = "select * from currentmatch where tid=@tid";
SqlConnection objCon;
SqlCommand objCmd;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(str, objCon))
{
objCmd.CommandType = CommandType.Text;
objCmd.Parameters.AddWithValue("@tid", Convert.ToInt32(ddlComp.SelectedValue));
using (objDa = new SqlDataAdapter(objCmd))
{
objDa.Fill(objDs);
if (objDs.Tables[0].Rows.Count > 0)
{
grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.DataBind();
}
else
{
grdlistTeam.EmptyDataText = "No record found";
grdlistTeam.DataBind();
}
}
}
}
}
catch (Exception ex)
{
}
}
}





GridView来源。





GridView source.

runat="server" AutoPostBack="true" AllowPaging="true"  
                                            AllowSorting="true" AutoGenerateColumns="false" DataKeyNames="Currentmatch_Id" PageSize="10"
                                            OnPageIndexChanging="grdlistTeam_PageIndexChanging" 
                                            OnRowDataBound="grdlistTeam_RowDataBound1" ShowHeader="false" 
                                            Width="100%" PagerSettings-Mode="NumericFirstLast">



//////////////

提前致谢。


//////////////
Thanks in advance.

推荐答案

在您的代码中,不清楚GridView是否具有AllowPaging = true。这可能在.aspx文件中设置



您没有为 grdlistTeam设置PageIndex属性 GridView在BindRecentMatches()方法中:

您应该在类中添加一个属性,以便可以使用BindRecentMatches方法中grdlistTeam_PageIndexChanging方法的NewPageIndex值。



In your code it is not clear if the GridView has the AllowPaging = true. This might be set in the .aspx file

You don't set the PageIndex property for the grdlistTeam GridView in the BindRecentMatches() method:
You should add a property in your class so you can use the NewPageIndex value from the grdlistTeam_PageIndexChanging method in the BindRecentMatches method.

grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.DataBind()





您必须在grdLisTeal.DataBind()方法之前设置grdLstTeam.PageIndex值。 br />
实际上你现在要做的是设置一个PageIndex并在那之后重新加载所有数据。由于在设置pageindex时没有数据,因此当添加新数据集时,pageindex将为0(请参阅DataSet objDs = new DataSet();在BindRecentMatches()中)。所以在添加数据后,可以设置正确的pageIndex。



You must set the grdLstTeam.PageIndex value before the grdLisTeal.DataBind() method.
In fact what yo do now is set a PageIndex and reload all the data after that. Since there is no data when setting the pageindex, the pageindex will be 0 when addng the new dataset (see DataSet objDs = new DataSet(); in BindRecentMatches()). So after the data is added the correct pageIndex can be set.


检查以下几点



1.检查GridView的AllowPaging Propery到真。

2.查看Fire GridView的PageIndexChanging事件。
Check the below points

1. Check GridView's AllowPaging Propery to True.
2 .Check Fire GridView's PageIndexChanging event.




现在我改了一些代码,但现在它没有给出点击页面索引上的任何数据。



protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{



if(Session [UserName]!= null)

{

username = Convert.ToString(Session [UserName]);

lblUser.Text =Welcome+''+ username;



}

else

{

Response.Redirect(login.aspx);

}



bindTournament();

// ViewState [SortExpr] = Sort_Direction;

DataView dvEmployee = Getdata();

grdlistTeam.DataSource = dvEmployee;

grdlistTeam.DataBind();





/ * string strQuery =select c。*,来自currentmatch的eventorganizer c加入c的锦标赛。 tournamentid = tournamentdescription.tournamentid where tournamentdescription.eventorganizer=@eventorganizer;

SqlCommand cmd = new SqlCommand(strQuery);

DataTable dt = GetData(cmd);

grdlistTeam.DataSource = dt;

grdlistTeam.DataBind(); * /



// bindRecentMatches();



}





私有DataView Getdata()

{

string strQuery =select_recent_match_by_eventorganizer;

using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [sports]。ToString()))

{

DataSet dsEmployee = new DataSet();

// string strQuery =select c。*,来自currentmatch的eventorganizer c加入TournamentDescription on c.tournamentid = TournamentDescription.tournamentid,其中TournamentDescription.eventorganizer = @ event组织者按TournamentDescription.TournamentStartDate ASC排序;

SqlCommand strSelectCmd = new SqlCommand(strQuery,conn);

// SqlCommand strSelectCmd =select c。*,eventorganch from currentmatch c join tournamentdescription on c.tournamentid = tournamentdescription.tournamentid where tournamentdescription.eventorganizer=@eventorganizer;

strSelectCmd.Comm andType = CommandType.StoredProcedure;

//objCmd.Parameters.AddWithValue(\"@ventorganizer,用户名);

strSelectCmd.Parameters.AddWithValue(@ eventorganizer,转换.ToString(Session [UserName]));

// strSelectCmd.Parameters.AddWithValue(@ eventorganizer,用户名);

SqlDataAdapter da = new SqlDataAdapter( strSelectCmd);

da.Fill(dsEmployee,tournamentdescription);

DataView dvEmp = dsEmployee.Tables [tournamentdescription]。DefaultView;

// dvEmp.Sort = ViewState [SortExpr]。ToString();

返回dvEmp;

}

}



protected void grdlistTeam_PageIndexChanging(object sender,GridViewPageEventArgs e)

{

grdlistTeam.PageIndex = e.NewPageIndex;

DataView dvEmployee = Getdata();

grdlistTeam.DataSource = dvEmployee;

grdlistTeam.DataBind();

}



有没有人为我的p提供解决方案roblem。



谢谢
Hi,
Now i changed some code but now it not giving any data on page index clicked.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

if (Session["UserName"] != null)
{
username = Convert.ToString(Session["UserName"]);
lblUser.Text = "Welcome" + ' ' + username;

}
else
{
Response.Redirect("login.aspx");
}

bindTournament();
// ViewState["SortExpr"] = Sort_Direction;
DataView dvEmployee = Getdata();
grdlistTeam.DataSource = dvEmployee;
grdlistTeam.DataBind();


/* string strQuery = "select c.*, eventorganizer from currentmatch c join tournamentdescription on c.tournamentid=tournamentdescription.tournamentid where tournamentdescription.eventorganizer=@eventorganizer";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
grdlistTeam.DataSource = dt;
grdlistTeam.DataBind(); */

// bindRecentMatches();

}


private DataView Getdata()
{
string strQuery = "select_recent_match_by_eventorganizer";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
DataSet dsEmployee = new DataSet();
// string strQuery = "select c.*,eventorganizer from currentmatch c join TournamentDescription on c.tournamentid= TournamentDescription.tournamentid where TournamentDescription.eventorganizer=@eventorganizer order by TournamentDescription.TournamentStartDate ASC";
SqlCommand strSelectCmd = new SqlCommand(strQuery,conn);
// SqlCommand strSelectCmd = "select c.*, eventorganizer from currentmatch c join tournamentdescription on c.tournamentid=tournamentdescription.tournamentid where tournamentdescription.eventorganizer=@eventorganizer";
strSelectCmd.CommandType = CommandType.StoredProcedure;
//objCmd.Parameters.AddWithValue("@eventorganizer", username);
strSelectCmd.Parameters.AddWithValue("@eventorganizer", Convert.ToString(Session["UserName"]));
// strSelectCmd.Parameters.AddWithValue("@eventorganizer", username);
SqlDataAdapter da = new SqlDataAdapter(strSelectCmd);
da.Fill(dsEmployee, "tournamentdescription");
DataView dvEmp = dsEmployee.Tables["tournamentdescription"].DefaultView;
// dvEmp.Sort = ViewState["SortExpr"].ToString();
return dvEmp;
}
}

protected void grdlistTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdlistTeam.PageIndex = e.NewPageIndex;
DataView dvEmployee = Getdata();
grdlistTeam.DataSource = dvEmployee;
grdlistTeam.DataBind();
}

Is there anyone who have the solution for my problem.

Thanks


这篇关于网格视图页面索引点击问题(每次都加载相同的页面)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆