【问题标题】:stop refreshing page on button click单击按钮时停止刷新页面
【发布时间】:2014-05-08 00:29:22
【问题描述】:

我需要让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;
}

【问题讨论】:

  • postback 上的Button 属性设置为false
  • 我想你想要更新面板
  • 或者考虑使用html按钮。但是您将无法访问您的服务器端代码。(必须改用javascript)
  • 在页面加载方法中编写代码 if(!isPostback){ // 将索引设置为 0 的代码 } 这样每次单击按钮时索引都不会设置为 0。您需要的是对 web 方法的 ajax 调用。
  • @user2235321 请发布您的代码,以便我更清楚您的问题......以及为什么你不想使用 ajax 这是避免回发的最佳方法......

标签: c# asp.net html


【解决方案1】:

您需要使用 UpdatePanel 进行部分更新。

<asp:ScriptManager runat="server"></asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always" >
    <ContentTemplate>
    <asp:Image ID="Image1" runat="server" Height="23px"  Width="24px" />

    <asp:Button ID="btnImageChange" runat="server" Text="Check" OnClick="btnImageChange_Click1"  />
     </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnImageChange" EventName="Click" />
    </Triggers>
    </asp:UpdatePanel>

然后在 .cs 后面的代码中这样写:

protected void btnImageChange_Click1(object sender, EventArgs e)
    {
     // you can add a loop here for the list of images...
            Image1.ImageUrl = "~/Images/loading.gif";

}

【讨论】:

  • 不,这不起作用...图像没有改变,因为仍在调用加载页面方法
  • 我更新了它...请在表单标签内..添加这一行
  • 这可能是最适合 OP 的解决方案,客户端返回是错误的,因为服务器端代码(OP 需要执行)无论如何都不会执行。跨度>
  • @user2235321 当然调用了 Page_Load 方法,您的按钮正在回发!任何页面实例服务器端代码执行 Page_Load 处理程序,为了防止其内部的代码执行,请使用if (!IsPostBack) 约定。
  • @ReboKoye 如果您在答案中的 Page_Load 示例中添加 IsPostBack 条件,那么 OP 肯定会拥有他们需要的一切。
【解决方案2】:
onClientClick="return false"

应该这样做! Onclick 将引用一个 ASP.net 函数,onClientClick 将呈现为 HTML 中的控件上的 OnClick。

【讨论】:

  • 如你所知,它的渲染方式类似于 onClick,它将客户端函数封装为一个守卫......如果你放 return false 意味着你永远不会访问服务器。
【解决方案3】:

点击按钮调用这个javascript函数

        <script type="text/Javascript">
     function ChangeIndex(){
      $.ajax({
                url: "Url of your page/Methodname",
                data: JSON.stringify({variableName,VariableValue}),
                dataType: 'json',
                type: 'POST',
                contentType: 'application/json',
                success: function (data) {
                    //set your image scr here 
                },
                error: function (data, status, jqXHR) {
                    //alert('There was an error.');
                }

            }
      </script>

在你的代码后面写一个webmethod

    [WebMethod]
    public static string Methodname(string variableName)
    { 
      //Logic to get the index. 
      return "Index";
     }

希望这会有所帮助。请发布您的代码,以便我可以进一步帮助您。

【讨论】:

    【解决方案4】:

    尽量不要实现这种老式的按钮。相反,您可以使用不会回发的fly buttonsjQuery 触发按钮。

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 2021-12-08
      • 1970-01-01
      • 2014-04-10
      • 2019-04-17
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多