Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all
I am developing movie theater seats booking website, for that i have placed images as Seats in asp:gridview(all cells) using ImageButton Template, for selection of seat click on ImageButton(cell) at that time i need that cell value where i clicked ImageButton in gridview.

I am getting RowIndex but not ColIndex.
ASP.NET
 <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" 
                                                Width="755px" AllowPaging="True" 
                                                
                                                PageSize="26" ShowHeader="true" CellSpacing="1" 
                            onrowupdating="Gridview1_RowUpdating" 
                            onrowcommand="Gridview1_RowCommand">
                                                <rowstyle borderstyle="None" />
                                                
                                                <columns>
                                               
                                                
                   <asp:TemplateField >
                                                <itemtemplate>
         <asp:HyperLink ID="HyperLink1" Text='<%# Eval("SeatRow") %>' runat="server">
                                                </itemtemplate>
                                                <itemstyle borderstyle="None" />
                                                
                   
                   
                   <asp:TemplateField HeaderText ="1" >
                   
                                                <itemtemplate>
                                                
 <asp:ImageButton ID="ImageButton1"  CommandArgument='<%# Eval("C1") %>' CommandName="ib1" 
 Visible='<%# MyVisible(Eval("C1").ToString()) %>' runat="server" ImageUrl='<%# MyImg(Eval("C1").ToString()) %>' Text='<%# MyValue(Eval("C1").ToString()) %>' OnClick ="ImageButton1_Click"/>
                                                </itemtemplate>
                                                <itemstyle borderstyle="None" />
                   
                   
                   <asp:TemplateField HeaderText ="2">
                                                <itemtemplate>
 <asp:ImageButton ID="ImageButton2" runat="server" CommandArgument='<%# Eval("C2") %>' 
 CommandName="ib2" Visible='<%# MyVisible(Eval("C2").ToString()) %>' ImageUrl='<%# MyImg(Eval("C2").ToString()) %>' 
 OnClick="button_click" RowIndex='<%# Container.DisplayIndex %>'/>
                                                </itemtemplate>
                                                <itemstyle borderstyle="None" />
                                                
                   
                   <asp:TemplateField HeaderText ="3">
                                                <itemtemplate>
<asp:ImageButton ID="ImageButton3" runat="server" CommandArgument='<%# Eval("C3") %>'
 CommandName="ib3" Visible='<%# MyVisible(Eval("C3").ToString()) %>' ImageUrl='<%# MyImg(Eval("C3").ToString()) %>'/>
                                                </itemtemplate>
                                                <itemstyle borderstyle="None" />
                                                



 </columns>
                                                <footerstyle borderstyle="None" />
                                                <SelectedRowStyle BorderStyle="None" />
                                                <editrowstyle borderstyle="None" />
                                                <alternatingrowstyle borderstyle="None" />

Defaust.aspx.cs
C#
public bool MyVisible(string txt)
    {
        txtNum = txt;
        if (txt == "")
        {
            return false;
            //Gridview1
        }
        else { return true; }
    }

    string ImgPath = "";
    public string MyImg(string txt)
    {
        try
        {
            if (txtNum == "1")
                ImgPath = "~/Images/wchair.JPG";
            else
                ImgPath = "~/Images/wchair.JPG";
            return ImgPath;
        }
        catch (Exception ex)
        {
            throw;
        }
    }






protected void gvScreenLayout_RowCommand(object sender, GridViewCommandEventArgs e)
    {

int id = (int)gvScreenLayout.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
    }
Posted
Updated 5-Sep-11 20:04pm
v2
Comments
Al Moje 7-Sep-11 22:13pm    
I think the most appropriate way to your solution is to store those images in the database field.
Then make a handler (.ashx) code file to handle the retrieving of such image. By then the indexing of your rows and columns in your grid is fix and is easily pin point.

you can use this..
C#
private void GridView1_Cell_Click(object sender, GridViewCellEventArgs e)
       {
           int cellindex=e.CellIndex;
           int rowindex =e.rowindex;
           var value = GridView1.Rows[rowindex].Cells[cellindex].value;

       }
 
Share this answer
 
v2
Comments
mahigurrala 7-Sep-11 3:04am    
This GridView1_Cell_Click event is not their in web.
How to add this event in webGridview events.
Try to use, GridViewRowCommand event, with CommandName and CommandArgument.
I have given a similar type of answer over here,
CommandArgument in gridview asp.net[^]
 
Share this answer
 
ASP.NET
<asp:gridview pagerstyle-cssclass="dataGridPager" onpageindexchanging="pageIndexChange" onselectedindexchanged="selectedRowChanged" runat="server" onrowdatabound="machineGridView" xmlns:asp="#unknown">
                    PageSize="25"  UseAccessibleHeader="true" AllowPaging="true" SelectedRowStyle-BackColor="Blue"  
                            ID="dataGrid"   CssClass="dataGrid" >
                   <alternatingrowstyle backcolor="WhiteSmoke" horizontalalign="Center" />
                   <headerstyle font-bold="true" horizontalalign="Center" backcolor="#2C4E5B" forecolor="White" />
                   <pagersettings visible="true" mode="Numeric" />
                   <pagerstyle horizontalalign="Center" font-underline="true" />
                   <selectedrowstyle font-bold="true" />
                  
</asp:gridview>


For example you have dataGrid like above
Try this in your .cs file

C#
   protected void machineGridView(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onmouseover"] = "this.style.color='red';";
        e.Row.Attributes["onmouseout"] = "this.style.color='black';";
        e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.dataGrid, "Select$" + e.Row.RowIndex);



    }

}

protected void selectedRowChanged(object sender, EventArgs e)
{

    String test = dataGrid.SelectedRow.Cells[0].Text;
    // by doing that you get the text value of the cell_0 of selected row

}
 
Share this answer
 
Comments
mahigurrala 7-Sep-11 21:21pm    
I need Gridview cell value dynamically when i clicked on cell

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