簡體   English   中英

c# asp.net 當我單擊 gridview 中的按鈕時,如何更改按鈕的圖像 url(其類型為圖像)

[英]c# asp.net how can i change the image url of button (it's type is image ) when i click the button in gridview

我創建了一個 gridview,它有最喜歡的按鈕,我想在單擊它時更改它的 url。 我該怎么做?

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" Width="446px" AllowPaging="True" 
            onrowcommand="GridView1_RowCommand1" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:ImageField AlternateText="Add To Favorites" Visible=false 
                        DataImageUrlFormatString="&quot;~/images/favorites.png&quot;" 
                        FooterText="Add To Favorites" HeaderText="Add To Favorites">
                        <FooterStyle Width="10px" />
                    </asp:ImageField>
                    <asp:ButtonField CommandName="AddComment" ButtonType="Image" HeaderText="Comment" ImageUrl="~/images/commentt.png" Text="Comment" ItemStyle-HorizontalAlign="Center" >
                     <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:ButtonField>
                    <asp:ButtonField CommandName="Share"  ButtonType="Image" HeaderText="Share with Friends" ImageUrl="~/images/openshare.png" ItemStyle-HorizontalAlign="Center" Text="Share" >
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:ButtonField>
                     <asp:ButtonField CommandName="ShareGroups"  ButtonType="Image" HeaderText="Share with Groups"   ImageUrl="~/images/openshare.png" ItemStyle-HorizontalAlign="Center" Text="Share" >
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:ButtonField>
                  <asp:ButtonField ButtonType="Image" HeaderText="Favorites" 
                        ImageUrl="~/images/StarEmpty.png" CommandName="Favorite" 
                        Text="Add to Favorites" ItemStyle-HorizontalAlign="Center">
                    <FooterStyle Height="20px" />
                    <HeaderStyle Width="3px" />
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:ButtonField>
                    <asp:ButtonField  ButtonType="Image"  CommandName="Mail" HeaderText="Send Mail" 
                        ImageUrl="~/images/email-icon.png.jpg" Text="Send Mail" 
                        ItemStyle-HorizontalAlign="Center" InsertVisible="False" >

                            <ItemStyle HorizontalAlign="Center"></ItemStyle>

                    </asp:ButtonField>
                    <asp:ButtonField CommandName="View" Text="View Rss" 
                        HeaderText="View Rss" ItemStyle-HorizontalAlign="Center" >
                    <HeaderStyle Width="50px" />
                    <ItemStyle Width="15px" />

                    </asp:ButtonField>
                     <asp:BoundField DataField="RSS_Title" HeaderText="RSS_Title"
                        SortExpression="RSS_Title" />
                   <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="HiddenField1" runat="server" 
                    Value='<%#Eval("RSS_ID")%>'/>

            </ItemTemplate>

            </asp:TemplateField>
                </Columns>
            </asp:GridView>

我的 c# 代碼如下

  if (e.CommandName == "Favorite")
        {
           //GridView1.Rows[rowIndex].Cells[2].

            string sValue = ((HiddenField)GridView1.Rows[rowIndex].FindControl("HiddenField1")).Value;
            int id = Convert.ToInt32(sValue);
            Session["SelectedRSS"] = sValue;

            DBConnection db = new DBConnection();
            bool res = db.CheckFavorites(id, User_Name);

            // if favorite rss  doesnot exist
            if (!res)
            {
                Boolean result = db.addFavorite(id, User_Name);

        DataSet selectedRSS = db.getRSS(id);
        DataTable dt = selectedRSS.Tables[0];
        DataRow row = null;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            row = dt.Rows[i];
            countOfFavorite = Convert.ToInt32(row[3].ToString());

        }
        countOfFavorite++;

                Boolean result1 = db.increaseFavoriteCount(id, countOfFavorite);

                // rssi favori olarak user dosyasına kaydet;


            }


        }           

首先,將 ID(在我的例子中為favBtn )分配給收藏夾按鈕字段,如下所示:

asp:ButtonField ID="favBtn" ButtonType="Image" HeaderText="Favorites" 
                    ImageUrl="~/images/StarEmpty.png" CommandName="Favorite" 
                    Text="Add to Favorites" ItemStyle-HorizontalAlign="Center">

if (e.CommandName == "Favorite")
{
    // ...
    // Code Updated here
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow row = GridView1.Rows[index];
    ImageButton ib = (ImageButton)row.Controls[0].Controls[0];
    ib.ImageUrl = "~/images/something.png"; // change to your new image

    // ...
}

它應該工作但不能保證。 :)

您是否嘗試過在 onclick 事件中設置src屬性?

onclick="this.src = 'http://mysite.com/images/differentimage.jpg'"

在服務器端代碼中,你會做這樣的事情:

myImageButton.ClientClick = "this.src = '" + ResolveUrl("~/images/someotherimage.jpg") + "';";

在服務器端 HTML 標記中,您可以執行以下操作:

<asp:Button OnClientClick="this.src='http://mysite.com/images/someotherimage.jpg" />

修改你喜歡的欄目如下

<asp:ButtonField ButtonType="Image" HeaderText="Favorites" ImageUrl='<%# GetCorrectImg(Convert.ToBoolean(Eval("IsFavirated"))) %>'
            CommandName="Favorite" Text="Add to Favorites" ItemStyle-HorizontalAlign="Center">
            <FooterStyle Height="20px" />
            <HeaderStyle Width="3px" />
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:ButtonField>

在此 function GetCorrectImg(IsFavirated) 中,您可以設置值,您可以通過這些值設置需要設置圖像的邏輯。 在后面的代碼中定義以下 function

public string GetActiveInactiveImg(Boolean IsFavirated)
    {
        string retVal = "~/images/buttons/StarEmpty.png";


        if (IsFavirated == true)
        {
            retVal = "~/images/buttons/ChangeImage.gif";
        }
    else 
        {
            retVal = "~/images/buttons/StarEmpty.png";
        }


        return retVal;
    }

觸發 ItemCommand 事件后再次綁定網格。

希望這會有所幫助。

您必須創建新文件夾和 R。在新文件夾中單擊添加 select 現有項目,選擇圖片並制作圖像和按鈕,雙擊按鈕寫入此代碼( image1.imageurl="example.jpg"; ,然后運行Web 單擊按鈕圖像將更改為另一張圖片。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM