如何在C#.NET中使用OnPageIndexChanging来使用MySql [英] How to get OnPageIndexChanging in C# .NET to work with MySql

查看:59
本文介绍了如何在C#.NET中使用OnPageIndexChanging来使用MySql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个MySql数据表,我在ASP.net的GridView中显示。如果没有分页,表格太大而无法显示,所以我在GridView中添加了OnPageIndexChanging事件和相应的处理程序。但是,在GridView中,当我单击下一页索引时,网格视图会消失。请参阅下面的代码。

对此非常感谢。

最好的问候

Kurt Jakobsen




代码:(文字)

protected void g_GridCtrl_PageIndexChanging(object sender,GridViewPageEventArgs e)

{

g_GridCtrl.PageIndex = e.NewPageIndex;

g_GridCtrl.DataBind();

}



void FillInGridView()

{

string myConnString = ConfigurationSettings.AppSettings [" DSN_Fyrr"];

MySqlConnection myConnection = new MySqlConnection( myConnString);

string strSQL =" SELECT LocationName FROM PartLocationDef ORDER BY LocationName;" ;

MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(strSQL,myConnection);

DataSet myDataSet = new DataSet();

myDataAdapter.Fill(myDataSet, " FailTypeDef");

g_GridCtrl.DataSource = myDataSet;

g_GridCtrl.DataBind();

myConnection.Close();

}


< asp:GridView ID =" g_GridCtrl" RUNAT = QUOT;服务器" AllowSorting = QUOT;真" AllowPaging = QUOT;真"的DataKeyNames = QUOT; LOCATIONNAME" OnPageIndexChanging = QUOT; g_GridCtrl_PageIndexChanging"的AutoGenerateColumns = QUOT假QUOT; OnSorting =" g_GridCtrl_Sorting">

< Columns>

< asp:BoundField ReadOnly =" true" HeaderText =" Location Name"数据字段= QUOT; LOCATIONNAME"的SortExpression = QUOT; LOCATIONNAME" />

< / Columns>

< / asp:GridView>

Hello,
I have a MySql data table that I display in a GridView in ASP .NET. The table is too large to display without paging, so I have added and OnPageIndexChanging event and corresponding handler to the GridView. But, in the GridView, when I click the next page index, the grid view disappear. See my code below.
Any help on this is very appreciated.
Best regards
Kurt Jakobsen



Code: ( text )
protected void g_GridCtrl_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
g_GridCtrl.PageIndex = e.NewPageIndex;
g_GridCtrl.DataBind();
}



void FillInGridView()
{
string myConnString = ConfigurationSettings.AppSettings["DSN_Fyrr"];
MySqlConnection myConnection = new MySqlConnection(myConnString);
string strSQL = "SELECT LocationName FROM PartLocationDef ORDER BY LocationName;" ;
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(strSQL, myConnection);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "FailTypeDef");
g_GridCtrl.DataSource = myDataSet;
g_GridCtrl.DataBind();
myConnection.Close();
}


<asp:GridView ID="g_GridCtrl" runat="server" AllowSorting="true" AllowPaging="true" DataKeyNames="LocationName" OnPageIndexChanging="g_GridCtrl_PageIndexChanging" AutoGenerateColumns="False" OnSorting="g_GridCtrl_Sorting">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="Location Name" DataField="LocationName" SortExpression="LocationName" />
</Columns>
</asp:GridView>

推荐答案

onPageIndexchanging事件你需要再次绑定数据源.......

u可以将数据集保存在会话中并在事件中再次绑定它
in the onPageIndexchanging event u need to bind the datasource again.......
u can keep the dataset in a session and bind it again in the event


这里是一篇可能有用的文章:
在ASP.NET中分页
Here is an article that may help:
Paging In ASP.NET


onPageIndexchanging事件中的
你需要再次绑定数据源.......

u可以将数据集保存在会话中并在事件中再次绑定它
in the onPageIndexchanging event u need to bind the datasource again.......
u can keep the dataset in a session and bind it again in the event



感谢您的回复,leela mn和kenobewan,我看了两个解决方案。在来自rolla的4个人页面中,使用DataGrid代替GridView,所以我改变了我的代码以反映这一点,但没有成功。此外,我试图再次绑定数据源,但也没有任何成功。


我显示第一页,但只有当我按下箭头显示第2页时才会再次收到第一页。


必须是一个我遗漏的小细节,但我没有看到它。


请参阅下面的修改后的代码。有什么建议吗?


void FillInGridView()

{

string myConnString = ConfigurationSettings.AppSettings [" DSN_Fyrr"];

MySqlConnection myConnection = new MySqlConnection(myConnString);

string strSQL =" SELECT LocationName FROM PartLocationDef ORDER BY LocationName;" ;

MySqlCommand myCommand = new MySqlCommand(strSQL,myConnection);

MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(myCommand);

DataSet myDataSet = new DataSet ();

myDataAdapter.Fill(myDataSet," FailTypeDef");

g_GridCtrl.DataSource = myDataSet;

Session [" MyDataSett" ;] = myDataSet;

g_GridCtrl.DataBind();

myConnection.Close();

}


protected void g_GridCtrl_PageIndexChanging(object sender,DataGridPageChangedEventArgs e)

{

g_GridCtrl.DataSource =(Int32)Session [" MyDataSett"];

g_GridCtrl.CurrentPageIndex = e.NewPageIndex;

g_GridCtrl.DataBind();

}



< asp:DataGrid ID =" g_GridCtrl" RUNAT = QUOT;服务器" AllowPaging =" true"

OnPageIndexChanging =" g_GridCtrl_PageIndexChanging" >

< / asp:DataGrid>

Thanks for your reply, leela mn and kenobewan, I''ve looked at both solutions. On the ''4 guys from rolla'' page, DataGrid is used in stead of GridView, so I changed my code to reflect that, without success. Also, I tried to bind the datasource again without any success there either.

I get the first page displayed, but only receive the first page again when I push the arrow for displaying page 2.

There must be a minor detail that I am missing, but I just don''t manage to see it.

Please see my modified code below. Any suggestions?

void FillInGridView()
{
string myConnString = ConfigurationSettings.AppSettings["DSN_Fyrr"];
MySqlConnection myConnection = new MySqlConnection(myConnString);
string strSQL = "SELECT LocationName FROM PartLocationDef ORDER BY LocationName;" ;
MySqlCommand myCommand = new MySqlCommand(strSQL, myConnection);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "FailTypeDef");
g_GridCtrl.DataSource = myDataSet;
Session["MyDataSett"] = myDataSet;
g_GridCtrl.DataBind();
myConnection.Close();
}

protected void g_GridCtrl_PageIndexChanging(object sender, DataGridPageChangedEventArgs e)
{
g_GridCtrl.DataSource = (Int32)Session["MyDataSett"];
g_GridCtrl.CurrentPageIndex = e.NewPageIndex;
g_GridCtrl.DataBind();
}


<asp:DataGrid ID="g_GridCtrl" runat="server" AllowPaging="true"
OnPageIndexChanging="g_GridCtrl_PageIndexChanging" >
</asp:DataGrid>


这篇关于如何在C#.NET中使用OnPageIndexChanging来使用MySql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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