从GridView的特定行索引传递列值

时间:2013-11-27 14:49:44

标签: asp.net vb.net gridview

假设我有一个包含一些绑定字段和模板字段的gridview。模板字段包含一个按钮(btnUpload)。

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 个答案:

答案 0 :(得分: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

会话选项:

// 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()
}