当我单击它时,我需要使 asp 按钮不刷新整个页面。
我的代码只是将图片更改为另一张图片,但图片的索引是在页面加载方法中设置的。并且每次点击按钮跳转到下一张图片索引时,整个页面都会刷新并调用页面加载方法。然后将索引设置回0。
单击按钮时如何使页面停止调用页面加载方法
这是我正在使用的基本代码
在表格中:
<tr>
<td> <asp:Button ID="Button1" runat="server" Text="Prev" OnClick="Button1_Click" OnClientClick="return false"/> </td>
<td> <img ID="pic" alt="" src="010.JPG" runat="server" width="200" height="200" /> </td>
<td> <asp:Button ID="Button2" runat="server" Text="Next" OnClick="Button2_Click" OnClientClick="return false"/> </td>
</tr>
这是.cs文件
private List<String> imagePathList = new List<String> { };
private List<Boolean> isActivePath = new List<Boolean> { };
protected void Page_Load(object sender, EventArgs e)
{
Debug.WriteLine("GALLARY *page load*");
pic.Width = 200;
pic.Height = 200;
addToList();
getImagePath(1);
}
protected void Button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("GALLARY *Button1_Click*");
int index = getActive();
getImagePath(index = index - 1);
}
protected void Button2_Click(object sender, EventArgs e)
{
Debug.WriteLine("GALLARY *Button2_Click*");
int index = getActive();
getImagePath(index = index + 1);
}
private void getImagePath(int index)
{
Debug.WriteLine("GALLARY *getImagePath* index = "+index);
int length = imagePathList.Count;
if (index < length && index >= 0)
{
//pic.Src = imagePathList[index];
//pic.Alt = imagePathList[index];
pic.Src = imagePathList[index];
pic.Alt = imagePathList[index];
setActive(index);
}
else
{
pic.Src = "DOES NOT EXIST";
pic.Alt = "DOES NOT EXIST";
}
}
private void addToList()
{
Debug.WriteLine("GALLARY *addToList*");
imagePathList.Clear();
isActivePath.Clear();
addImage("08.JPG");
addImage("09.JPG");
addImage("010.JPG");
addImage("011.JPG");
addImage("012.JPG");
}
private void addImage(String filename)
{
Debug.WriteLine("GALLARY *addImage* filename = "+filename);
imagePathList.Add(filename);
isActivePath.Add(false);
}
private void setActive(int index)
{
Debug.WriteLine("GALLARY *setActive* index = " + index);
for (int i = 0; i > isActivePath.Count; i++)
{
isActivePath[i] = false;
}
isActivePath[index] = true;
}
private int getActive()
{
Debug.Write("GALLARY *getActive*");
int temp = 0;
for (int i = 0; i > isActivePath.Count; i++)
{
if (isActivePath[i] == true)
{
temp = i;
}
}
Debug.WriteLine("index = " + temp);
return temp;
}