Reputation: 585
I created an gridview it has favorite button and I want to change the url of it when i click on it. how can i do it?
<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=""~/images/favorites.png""
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>
My c# codes are below
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;
}
}
Upvotes: 0
Views: 8435
Reputation: 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 .
Upvotes: -1
Reputation: 547
Modify your favorite column as below
<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>
In this function GetCorrectImg(IsFavirated) u can set values by which u can set logic which image need to be set. define below function in your code behind
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;
}
Bind the grid again once your ItemCommand event is fired.
Hope this will help.
Upvotes: 0
Reputation: 3821
First, assign an ID(favBtn, in my case) to the Favorite button field like as follows:
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
// ...
}
It should work but can't gurantee. :)
Upvotes: 3
Reputation: 23123
Have you tried setting the src
property in an onclick event?
onclick="this.src = 'http://mysite.com/images/differentimage.jpg'"
In server-side code you would do something like this:
myImageButton.ClientClick = "this.src = '" + ResolveUrl("~/images/someotherimage.jpg") + "';";
In server-side HTML markup you can do:
<asp:Button OnClientClick="this.src='http://mysite.com/images/someotherimage.jpg" />
Upvotes: 0