2014-02-21 60 views
1

我已經在gridview中填充數據。gridview我有一個鏈接按鈕,當點擊它會使一個面板visible.Now我想保留id在會話中選中的行並在面板中使用它。我也知道如何找出gridview的id,但是當選定該特定行的鏈接按鈕時,我無法生成gridview事件。選擇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(); 
    } 

請幫我解決這個....

+0

你可以使用這個rowcommand事件, –

回答

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當前行,你可以做你想做什麼都...

+0

此處指數值始終爲零。 – user3247426

+0

可能是你點擊gridview中的第一行鏈接是y嗎?索引即將到達「0」。請點擊其他的行鏈接按鈕,然後你可以得到rowindex ...我測試了它從我的結尾工作的代碼.... – Kapil

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

NOP ID不來了。 – user3247426

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  
    }