在gridview中的链接按钮的情况下选择行的id

时间:2014-02-21 06:07:23

标签: c# asp.net gridview

我已经在gridview中填充数据。在gridview中我有一个链接按钮,当它点击它将使一个面板可见。现在我想保持所选行的id在会话中并在面板中使用它。我也知道如何找出gridview的id,但是当选择该特定行的链接按钮时,我无法生成gridview事件。

 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;

    }

这是面板中的按钮事件,我实际上需要获得该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();
    }

请帮我解决这个问题....

4 个答案:

答案 0 :(得分:3)

试试这个..

   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是gridview当前行,你可以随心所欲地做...

答案 1 :(得分:0)

pelase通过这个链接,这里是一个很好的rowcommand事件样本

http://www.c-sharpcorner.com/uploadfile/syedshakeer/rowcommand-event-in-gridview/

答案 2 :(得分:0)

使用此代码:

 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;
        }

答案 3 :(得分:0)

在GridView商店中您的ID如下: -

 <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>

imgbtn_Click上获取ID并存储在Session中: -

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

如果您有button,请在面板中执行以下操作: -

 <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    
  }