josna
josna

Reputation: 103

how to access html table td values to codebehind?

Using jquery i append the gridview values to a table. How to read the dynamic created td values in code behind.

my jquery code is

 $(function () {
        SetEmptyMessage();
        $("[id*=cbSelect]").on("click", function () {
            var selected = $('[id$=gvCompanyListing] tr td input[type=checkbox]:checked');
            if (selected.length > 0) {
                $(selected).each(function () {
                    var appendRow = $(this).closest('tr').clone(true);
                    var row = $(this).closest('tr');
                    $(row).remove();
                    $("[id$=tblAssigned]").append(appendRow);
                });
                SetEmptyMessage();
                return false;
            }
            else {
                var selected = $('[id$=tblAssigned] tr td input[type=checkbox]:not(:checked)');
                if (selected.length > 0) {
                    $(selected).each(function () {
                        var appendRow = $(this).closest('tr').clone(true);
                        var row = $(this).closest('tr');
                        $(row).remove();
                        $("[id$=gvCompanyListing]").append(appendRow);
                    });
                }
                SetEmptyMessage();
                return false;
            }
        });
    });

    function SetEmptyMessage() {
        if ($('[id$=tblAssigned] td').closest('tr').length == 0) {
            //var colspan = $('[id$=gvAssigned] th').length;
            //$('[id$=tblAssigned]').append('<tr class="empty"><td colspan=' + colspan + '>No records were found</td></tr>');
        } else {
            $('[id$=tblAssigned]').find('.empty').remove();
        }

        if ($('[id$=gvCompanyListing] td').closest('tr').length == 0) {
            //var colspan = $('[id$=gvCompanyListing] th').length;
            //$('[id$=gvCompanyListing]').append('<tr class="empty"><td colspan=' + colspan + '>No records were found</td></tr>');
        } else {
            $('[id$=gvCompanyListing]').find('.empty').remove();
        }
    }

my aspx code is

<asp:GridView ID="gvCompanyListing" runat="server" AutoGenerateColumns="False" ShowHeader="False" GridLines="None" >
   <Columns>
       <asp:TemplateField>
           <ItemTemplate>
               <asp:CheckBox ID="cbSelect" CssClass="gridCB" runat="server" ItemStyle-Width="10%"></asp:CheckBox>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField DataField="client_name" SortExpression="CompanyInfo" ItemStyle-Width="92%" ItemStyle-Height="1px" ControlStyle-CssClass="companyInfo">
       </asp:BoundField>
   </Columns>
</asp:GridView>
<table id="tblAssigned" runat="server">
</table>

And code behind i have,

protected void btnRequestAccess_Click(object sender, EventArgs e)
{
try{
    System.Text.StringBuilder strBuild = new System.Text.StringBuilder();
    string strUserName = Session["UserName"].ToString();
    int nUserName = strUserName.Length;
    int nRemaing = nUserName - 21;
    //string strActualName = strUserName.Substring(21, nRemaing);
    strBuild.Append("Request to set Clients to Hays user " + strUserName + "\n");
    int nval = 0;        
    for (int i = 0; i <= this.tblAssigned.Rows.Count; i++){                 
        HtmlTable td = (HtmlTable)tblAssigned.FindControl("companyInfo");
        if (td != null){  
            strBuild.Append(tblAssigned.Items[i].Text.ToString()+"\n")
            nval = nval + 1;
            }
        }
    }
}

How to get the tblAssigned value as text in code behind.what is the mistake i done?

 for (int i = 0; i < tblAssigned.Rows.Count; i++)
        {
         for (int j = 0; j < tblAssigned.Rows[i].Cells.Count; j++)
         {
             string cellValue= tblAssigned.Rows[i].Cells[j].InnerHtml;
          // do something here
         }
       }

Can we access like this but the code doesnot enter into j loop

Upvotes: 3

Views: 5481

Answers (1)

Sumit Jambhale
Sumit Jambhale

Reputation: 625

You can not access the values that you have assigned in javascript at server side after postback

you can use Hidden Field Control to store the table data in it at client side and once page postback you will be able to retrieve the values at server side from Hidden Field Control.

e.g.

Define one javacript global variable for inserting the number of rows into javascript array

var counter =0;

Create an javascript array

var arr = [];

on new row created insert a javascript object in array

arr[counter] = { /* row data that you want to access at server side */ };

hiddenField.Value = arr;

on server side button click event get the value from Hidden Field

Upvotes: 3

Related Questions