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

问题描述



这是我的整个代码..



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.

推荐答案

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.




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

09-12 06:44