3

クリックしたときにページ全体を更新しないように 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;
}
4

4 に答える 4

4

部分更新には 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";

}
于 2013-04-02T09:10:03.927 に答える
-2

ボタンをクリックすると、この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";
     }

お役に立てれば。さらにお手伝いできるように、コードを投稿してください。

于 2013-04-02T09:21:44.103 に答える