日期:2014-05-18  浏览次数:20596 次

点击gridview时如何获取单个cell的值
VB.NET code
Protected Sub gridview1_rowdatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
      
        Dim count As Integer = gridview1.rows.count    
        For i As Integer = 0 To count - 1
            session("course") = gridview1.datakeys(i).value.tostring() 
            response.write(session("course"))
‘这里只能获取所有cell的值,怎样点击一次就获取一个gridview1.datakeys(i)的值?
        Next
    End Sub


------解决方案--------------------
如果想在客户端取得:

<script type="text/javascript">
function OnSuccess(result)
{
document.getElementById('<%= HiddenX.ClientID %>').value = result;
}

</script>

<asp:TemplateField HeaderText="我爱你">
<ItemTemplate>
<asp:Button runat="server" ID="Btn" Text="噢耶~" />
</ItemTemplate>
</asp:TemplateField>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("Btn");

btn.OnClientClick = "OnSuccess('" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString() + "');return false;";
}
 }
------解决方案--------------------
绑定前
 GridView2.DataKeyNames = New String() {"classnum"}

然后
GridView2.DataKeys(GridView2.SelectedIndex).Value