user3247426
user3247426

Reputation: 127

selecting id of the row in the event of the link button inside the gridview

I have populate the data in gridview.In gridview I have one link button when click it will make one panel visible.Now I want to keep the id of that selected row in session and use that in panel.I also know how to find out the id of gridview but I am not being able to generate event of gridview when the link button of that specific row is selected.

 private void Bindgrid()
    {

        if (Session["CartId"] != null)
        {
            DataTable dt = new DataTable();
            int introws = 0;
            //string mandir_id = Request.QueryString["id"];
            string cmdstr = "select p.id,p.name,p.photo_id,p.price,m.name from puja p,mandir m,shoppingcart s where s.session_id='" + Session["CartId"] + "' and p.with_mandir=m.id and p.id=s.with_puja";
            //sQLcONN.Open();
            MySqlCommand cmd = new MySqlCommand(cmdstr, sQLcONN);
            MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
            adp.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();

            sQLcONN.Close();
        }
        else { }

    }

protected void Linkedit_click(object sender, EventArgs e)
    {



        pnl1.Visible = true;

    }

And this is the button event inside the panel where I actually need to get that Id.

 protected void submit_Click(object sender, EventArgs e)
    {

       string  price = Session["price"].ToString();

        WebClient web = new WebClient();
        string url = string.Format("http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={0}{1}=X", ddlFrom.SelectedValue.ToUpper(), ddlTo.SelectedValue.ToUpper());
        string response = web.DownloadString(url);
        string[] values = Regex.Split(response, ",");
        decimal rate = System.Convert.ToDecimal(values[1]);
        decimal amount = System.Convert.ToDecimal(price);
        rate = rate * amount;
        //string  result = System.Convert.ToString(rate);
        //result = rate;
        //rate = System.Convert.ToDecimal(lblResult.Text);
        lblResult.Text = rate.ToString();
    }

Please help me to solve this....

Upvotes: 1

Views: 1769

Answers (3)

Manoj
Manoj

Reputation: 631

Use This Code:

 protected void Linkedit_click(object sender, EventArgs e)
        {
           LinkButton lnk_Button = (LinkButton)sender;
           GridViewRow id = (GridViewRow)lnk_Button.NamingContainer;
           Session["id"] = (string)gvrow.Cells[0].Text.ToString();//here you get first coloumn value
           pnl1.Visible = true;
        }

Upvotes: 0

Kapil
Kapil

Reputation: 1931

try this..

   protected void Linkedit_click(object sender, EventArgs e)
        {
          LinkButton  lnkedit  = sender as LinkButton;
          GridViewRow gvrow = lnkedit.NamingContainer as GridViewRow;
          int index = gvrow.RowIndex;  
          pnl1.Visible = true;

        }

gvrow is the gridview current row you can do what ever you want...

Upvotes: 3

Afnan Ahmad
Afnan Ahmad

Reputation: 2542

In GridView store your id like following:-

 <asp:TemplateField ShowHeader="False" meta:resourcekey="TemplateFieldResource4">
  <ItemTemplate>
  <asp:ImageButton ID="imgbtn" runat="server" CausesValidation="False"
  ImageUrl="~/Images/img.gif"   OnClick="imgbtn_Click" CommandArgument='<%#Eval("Id") %>'/>
  </ItemTemplate>

On imgbtn_Click get id and store in Session:-

 protected void imgbtn_Click(object sender, ImageClickEventArgs e)
 {
            Session["Id"] = Convert.ToInt32((sender as ImageButton).CommandArgument);
            // show panel here
 }

In panel if you have button do the following:-

 <asp:Button ID="imgbtnSave" CausesValidation="False" runat="server" Text="yourText" OnClick="imgbtnSave_Click"/>

  protected void imgbtnSave_Click(object sender, EventArgs e) 
  {

            int id =  Convert.ToInt32(Session["Id"]); 
            // do stuff with your id    
  }

Upvotes: 0

Related Questions