Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Comments
Sinisa Hajnal 30-Nov-15 2:34am    
You're filling it with jQuery and you should do your work on the client.

BUT, you could (since tblAssigned runs at server) just ask for its row collection and loop through that.
Member 12133978 30-Nov-15 2:45am    
is there any another solution?
Krunal Rohit 30-Nov-15 2:58am    
No. Sinisa Hajnal is right. If you want to access the td values in code-behind, you need to use AJAX.

-KR
F-ES Sitecore 30-Nov-15 4:02am    
Changes you do to the html via jQuery won't be visible when you do a post-back. If you want to add rows etc and access that data from code-behind you're going to have to additionally store the data somewhere it can be read server-side such as in a hidden field. So if you add a row with three values you can maintain a hidden field

&;ltinput type-"hidden" name="myData" />

that also has the data in some encoded form, like

"col1|col2|col3"

etc. In your code-behind you can then read that field (Request.Form["myData"]) and de-code the data that is in it.
Member 12133978 30-Nov-15 4:30am    
how to set td values to hidden field

Try It.
In Code Behind

HTML
var result=this.tblAssigned.InnerText;
or
var result= this.tblAssigned.Rows[0].Cells[0].InnerText ;
 
Share this answer
 
v2
On second thought, here is concrete example.

C#
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
           }
       }


I hope this helps...good luck
 
Share this answer
 
v2
Comments
Member 12133978 30-Nov-15 3:33am    
its not entering to the for loop of j
Sinisa Hajnal 30-Nov-15 3:59am    
Check that your Rows.Count > 0...if there are no rows in the table, obviously it will not loop. One of the things you need to learn as a developer is how to debug properly. For now, learn how to set breakpoints and step through code.
Member 12133978 30-Nov-15 4:03am    
thanku for your advice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900