简体   繁体   中英

Create session in gridview cell

i have a gridview with tempaltefield buttons, i want to create a session with value of a cell in selected button row , can anyone help me i tryed this but didnt work:

protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {
    Session["mysession"] = GridView1.SelectedRow.Cells[1].Text;
    }

First of all, if it's just a imagebutton in a templatefield, actually you don't select de row. This line will problably throw an exception because SelectedRow is null. But if you are using a command to select, that's correct. Maybe your event (ImageButton1_Click1) is not assigned to your image (OnClick).

You can try something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //Add and event RowDataBound
            grvGrid.RowDataBound += new GridViewRowEventHandler(grvGrid_RowDataBound); 
        }
        catch
        {
            //...throw
        }
    }

    protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {                
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //...
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {          
                //Add an ImageButton foreach row in GridView
                ImageButton ibtImageAlt = new ImageButton();
                ibtImageAlt.ImageUrl = "App_Images/y.gif";

                //ImageButton's ID will be the index of the row
                ibtImageAlt.ID = e.Row.RowIndex.ToString();                    
                ibtImageAlt.ForeColor = System.Drawing.Color.White;
                ibtImageAlt.Font.Overline = false;
                ibtImageAlt.Click += ibtImageAlt_Click;
            }
        }
        catch
        {
        //...throw
        }
    }

    protected void ibtImageAlt_Click(object sender, EventArgs e)
    {
        try
        {
            //Catch the ImageButton ID and the row in GridView

            //An example to catch the value of the row selected by the ImageButton
            Int32 intIndexRow = Convert.ToInt32(((ImageButton)sender).ID);
            String strTest = grvGrid.Rows[intIndexRow].Cells[0].Text;
        }
        catch
        {
            //...throw
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM