0

お気に入りのボタンがあるグリッドビューを作成しましたが、クリックしたときにその 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

srconclick イベントでプロパティを設定しようとしましたか?

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

新しいフォルダーを作成し、新しいフォルダーを R.Click で追加する必要があります。既存のアイテムを選択し、写真を選択して画像とボタンを作成し、ボタンをダブルクリックしてこのコードを書き込みます ( image1.imageurl="example.jpg"; を実行します)。 Web ボタンをクリックすると、画像が別の画像に変わります。

于 2014-11-18T20:26:40.750 に答える