我的问题是失去价值。我有一个带有标准asp.net分页的DataGrid。当我更改页面索引时,名为“ id”的全局变量将丢失其值。请帮助我。
int id = 0;
void Payments()
{
radioBtnList = GetData();
radioBtnList.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
Payments();
Response.Write(id); // I get value 0 :(
}
protected void radioBtnList_Changed(object sender, EventArgs e)
{
id = int.Parse(radioBtnList.SelectedItem.Text);
}
protected void dgw_pagechange(object source, DataGridPageChangedEventArgs e)
{
dgw.CurrentPageIndex = e.NewPageIndex;
dgw.DataBind();
}
答案 0 :(得分:2)
与其将ID存储在页面更改后将重新加载的页面中,您不应该将其存储在将保留回发的位置。
示例可能是:
答案 1 :(得分:0)
您可以像这样使用ViewState。
int id = 0;
void Payments ()
{
radioBtnList = GetData();
radioBtnList.DataBind ();
}
protected void Page_Load (object sender, EventArgs e)
{
Payments();
Response.Write(ViewState["id"]);
}
protected void radioBtnList_Changed (object sender, EventArgs e)
{
id = int.Parse (radioBtnList.SelectedItem.Text);
ViewState["id"]=id;
}
protected void dgw_pagechange (object source, DataGridPageChangedEventArgs e)
{
dgw.CurrentPageIndex = e.NewPageIndex;
dgw.DataBind();
}