Click here to Skip to main content
16,019,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my gridview I have one template field with Header text="Action"

In that template field I have an item template as a linkbutton with id lnkassign.

I have set some image to that linkbutton.

I want it so that when I click on that linkbutton another image should be set.

How do I do this?

The code will come in gridview1_rowdatabound
or
gridview_rowcommand
Posted
Updated 8-Sep-10 0:12am
v2
Comments
Dalek Dave 8-Sep-10 6:12am    
Edited for Grammar, Syntax and Readability.

1 solution

hi,

This will come under "gridview_rowcommand" Event

please follow bellow steps:

step 1:
XML
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="false"
            onrowcommand="grd_RowCommand">
    <Columns>
    <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
            <asp:Label ID="lbl" Text='<%#Eval("ID") %>' runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:LinkButton ID="lnk" CommandArgument='<%#Eval("ID") %>' CommandName="vsr" Text="<img src='images/img1.jpg' border='0' />" runat="server">
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>


Step 2:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable myDataTable = new DataTable();

DataColumn myDataColumn;

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "ID"; //this is index for update
myDataTable.Columns.Add(myDataColumn);
//
DataRow row;
for(int i=0;i<10;i++)
{
row = myDataTable.NewRow();
row["ID"] = i;
myDataTable.Rows.Add(row);
}
grd.DataSource = myDataTable.DefaultView;
grd.DataBind();
}
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "vsr")
{
int i=Convert.ToInt32(e.CommandArgument);
GridView gr = (GridView)sender;
LinkButton lnk = (LinkButton)(gr.Rows[i].FindControl("lnk"));
lnk.Text = "<img src='images/img2.jpg' border='0' />";
}
}

Step 3:

add image folder in your project, add add 2 images in that folder
those image names img1.jpg and img2.jpg
 
Share this answer
 
Comments
Sandeep Mewara 8-Sep-10 13:13pm    
Comment from OP: its giving error

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900