• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

asp.net vb - How to read an asp.net table cell value in a button-click event?

I tried both of these:

   If tblTable.Rows(0).Cells(0) IsNot Nothing Then
               Dim strstring As String = "something"
   End If


   If tblTable.Rows(0).Cells(0).Text <> "" Then
                Dim strstring As String = "something"
   End If

This is the error message I get:
Specified argument was out of the range of valid values.
Parameter name: index
0
Rick
Asked:
Rick
  • 2
1 Solution
 
AliSyedCommented:
tblTable is data table?

Please check to make sure that the dataset is not empty.
do this
if (ds ! = nul && ds.tbltable != null && ds.tbltable.count > 0 )
0
 
RickAuthor Commented:
It is a web control (asp.net table).
 
0
 
RickAuthor Commented:
I found my own solution... will store the values of tblTable in an array then retrieve them later when I need them. It is a small table anyway. An arraylist should do it.
If anybody has a better idea, let me know.   Thanks.
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

Please check code for reading cell values (C#).

Regards,
VSS

--------------
//Default.aspx
--------------
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:table ID="myTable" runat="server" Caption="My Table" Width="107px">
        <asp:TableRow runat="server">
            <asp:TableCell runat="server">First</asp:TableCell>
            <asp:TableCell runat="server">Second</asp:TableCell>
        </asp:TableRow>
        <asp:TableRow runat="server">
            <asp:TableCell runat="server">Third</asp:TableCell>
            <asp:TableCell runat="server">Fourth</asp:TableCell>
        </asp:TableRow>
    </asp:table>
    <asp:Button ID="btnValue" Text="Read" runat="Server" OnClick="btnValue_Click" />

    </div>
    </form>
</body>
</html>

------------------
//Default.aspx.cs
------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnValue_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < myTable.Rows.Count; i++)
        {
            for (int j = 0; j < myTable.Rows[i].Cells.Count; j++)
            {
                Response.Write(myTable.Rows[i].Cells[j].Text+"|");
            }
        }
    }
}

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now