2013-11-27 19 views
0

假設我有一個包含一些綁定字段和一個模板字段的gridview。模板字段包含一個按鈕(btnUpload)。從GridView的特定行索引傳遞列值

btnUpload觸發一個modalpopupextender來顯示一個包含一些控件和一個提交按鈕的面板。

我需要做的是從行btnUpload被單擊並將其傳遞到面板的單元格0的值,以便我可以檢索一些數據到panel_load事件的面板控件基於該來自gridview的單元格0的值。

我想我可以通過將單元格0中的值存儲在會話變量中來做到這一點,但不確定這是否是這樣做的「最佳」方式?

更新 - 使用面板存儲行索引上隱藏字段由Karl

的建議我增加了兩個隱藏字段的面板和我填充它們如下:

If e.CommandName = "Upload" Then 
     Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow) 
     Dim ref As String = CType(row.Cells(0), DataControlFieldCell).Text 
     Dim dt As String = CType(row.Cells(2), DataControlFieldCell).Text 
     hfRef.Value = ref 
     hfDate.Value = dt 
    End If 

這正在工作並填充隱藏的字段。然而,面板上的btnSubmit_Click事件過程中我做下面我分配沒有得到從隱藏字段的值的變量:

If fuCertificate.HasFile Then 
     Dim fileName As String = Path.GetFileName(fuCertificate.PostedFile.FileName) 
     fuCertificate.SaveAs(Server.MapPath("~/CalibrationCerts/" & fileName)) 
     Dim ref As String = hfRef.Value 
     Dim dt As String = hfDate.Value 
     Dim dc As New ExternalCalibrationDataContext 
     Dim thisRecord = (From x In dc.ext_calibration_records 
         Where x.calibration_no = ref AndAlso 
         x.calibration_date = dt).ToList()(0) 
     thisRecord.certificate_no = txtCertNumber.Text 
     thisRecord.certificate = "~/CalibrationCerts/" & fileName 
    Else 
     lblError.Text = "Please select a file to upload" 
    End If 

回答

1

我建議把一個HiddenField控制由ModalPopupExtender使用的面板。使用HiddenField控件比使用Session更容易,因爲您不需要將隱藏字段Value轉換爲字符串,因爲它已經是一個字符串。所有Session對象都存儲爲Object,因此在從Session中檢索值時,必須將其轉換爲字符串。


隱藏字段選項:

<asp:Panel> 
    // Other stuff for your modal popup 
    <asp:HiddenField id="HiddenField1` runat="server" /> 
</asp:Panel> 

// Set hidden field 
HiddenField1.Value = cell0.Text 

// Get hidden field 
string theValue = HiddenField1.Value 

session選項:

// Set the value in Session 
Session["Cell0Text"] = cell0.Text 

// Retrieve the value from Session 
// First check to see if the Session value still exists 
if(Session["Cell0Text"] != null) 
{ 
    // Now cast the Session object to a string 
    string theValue = Session["Cell0Text"].ToString() 
} 
+0

感謝卡爾,我更喜歡隱藏字段選項。我之前沒有用過這些,並且喜歡你說它比使用會話變量更容易。雖然隱藏的字段值似乎沒有保存 – Jimsan