0

我创建了一个 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;


            }


        }           
4

4 回答 4

2

首先,为收藏按钮字段分配一个 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

    // ...
}

它应该工作,但不能保证。:)

于 2012-04-06T10:51:22.530 回答
0

您是否尝试src在 onclick 事件中设置属性?

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" />
于 2012-04-06T10:34:46.377 回答
0

修改您喜欢的列如下

<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>

在此函数 GetCorrectImg(IsFavirated) 中,您可以设置值,通过这些值您可以设置需要设置图像的逻辑。在后面的代码中定义以下函数

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 事件后再次绑定网格。

希望这会有所帮助。

于 2012-04-06T11:09:15.967 回答
-1

you Must to Create new Folder and R.Click in the new Folder add select Exist item , choice the Pictures and make image & Button , double Click into the Button write this Code ( image1.imageurl="example.jpg"; , then run the Web click the button the image will change to the another Picture .

于 2014-11-18T20:26:40.750 回答