Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

add comma delimited string to a hiddenfield on my page

I have a gridview that displays client data. The first column on each row for the gridview is a checkbox.  The second column on the gridview is client name and the third column is account holder. The user can select checkboxes (on multiple rows) to modify data (account holder).

I want to add clientName column value in a comma delimited string to a hiddenfield on my page.
How do I do that using javascript, as I do not want to use postback and looping through each row on the grid to get the checkbox status. When a checkbox is checked, it should append the hiddenfield value.

I am a newb on javascript. Please review my javascript code and suggest corrections. I am getting the hiddenfield value as blank no matter what checkboxes are checked. I also know that my code is not targeting the correct row to get ClientName (row where checkbox is checked).

Any help is highly appreciated.

Thanks.

function RowClick(CheckBox) 
        {
            var BaseControl = document.getElementById('<%= this.myGrid.ClientID %>');
            var TargetControl = document.getElementById('MyHiddenField');
            for (var i = 0; i < BaseControl.rows.count; i++)
            {
                var rowElem = BaseControl.rows[i];
                for (var x = 0; x < rowElem.cells.count; x++) 
                {
                    var ClientName = rowElem.cells[1];
                    if (CheckBox.checked) TargetControl.Text += "," + Case_Id;
                }
            }
        }

Open in new window

0
patd1
Asked:
patd1
  • 3
2 Solutions
 
chaitu chaituCommented:
can you post the view source code??
0
 
hiteshgoldeneyeCommented:
Correct me if i am wrong but are you referring to  the right variable name when appending to the hiddenfield (TargetControl.Text)

See revised code below
function RowClick(CheckBox) 
        {
            var BaseControl = document.getElementById('<%= this.myGrid.ClientID %>');
            var TargetControl = document.getElementById('MyHiddenField');
            for (var i = 0; i < BaseControl.rows.count; i++)
            {
                var rowElem = BaseControl.rows[i];
                for (var x = 0; x < rowElem.cells.count; x++) 
                {
                    var ClientName = rowElem.cells[1];
                    if (CheckBox.checked) TargetControl.Text += "," + ClientName;
                }
            }
        }

Open in new window

0
 
patd1Author Commented:
Please find attached partial view source code. Pasting the whole thing will make reading it too complicated. Please let me know if you need any other part to help me. Appreciate your help.

Thanks.

<td align="center">
                                    <span id="ctl00_ContentPlaceHolderMain_Error"></span>
                                    <input type="hidden" name="ctl00$ContentPlaceHolderMain$MyHiddenField" id="ctl00_ContentPlaceHolderMain_MyHiddenField" />                                    
                                </td>

....
..
..


</th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$CaseNo&#39;)">Case Number</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$CaseID&#39;)">CaseID</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$ClientName&#39;)">Client Name</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$CompanyName&#39;)">Company Name</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$PatientName&#39;)">Patient Name</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$CreatedDate&#39;)">Created Date</a></th><th scope="col"><a href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolderMain$gvWorklist&#39;,&#39;Sort$AssignedDoctor&#39;)">Assigned Doctor</a></th>
			</tr><tr>
				<td>
                                                    <input id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl02_CheckBoxActiveClient" type="checkbox" name="ctl00$ContentPlaceHolderMain$gvWorklist$ctl02$CheckBoxActiveClient" onclick="javascript:RowClick(this);" />
                                                </td><td align="left" style="white-space:nowrap;">
                                                    <span id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl02_LabelCaseNumber">OTH11-000511</span>
                                                </td><td style="white-space:nowrap;">186450</td><td style="white-space:nowrap;">C702</td><td>Georgia Cancer Specialists-Macon Coliseum</td><td>TEST1, Deep </td><td style="white-space:nowrap;">2/15/2011 11:51:33 PM</td><td align="left">
                                                    <span id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl02_LabelAssignedDoctor"></span>
                                                </td>
			</tr><tr class="alt">
				<td>
                                                    <input id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl03_CheckBoxActiveClient" type="checkbox" name="ctl00$ContentPlaceHolderMain$gvWorklist$ctl03$CheckBoxActiveClient" onclick="javascript:RowClick(this);" />
                                                </td><td align="left" style="white-space:nowrap;">
                                                    <span id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl03_LabelCaseNumber">OTH11-000509</span>
                                                </td><td style="white-space:nowrap;">186440</td><td style="white-space:nowrap;">C702</td><td>Georgia Cancer Specialists-Macon Coliseum</td><td>TEST, CASE </td><td style="white-space:nowrap;">2/8/2011 6:36:50 PM</td><td align="left">
                                                    <span id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl03_LabelAssignedDoctor"></span>
                                                </td>
			</tr><tr>
				<td>
                                                    <input id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl04_CheckBoxActiveClient" type="checkbox" name="ctl00$ContentPlaceHolderMain$gvWorklist$ctl04$CheckBoxActiveClient" onclick="javascript:RowClick(this);" />
                                                </td><td align="left" style="white-space:nowrap;">
                                                    <span id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl04_LabelCaseNumber">OTH11-000508</span>
                                                </td><td style="white-space:nowrap;">186428</td><td style="white-space:nowrap;">C702</td><td>Georgia Cancer Specialists-Macon Coliseum</td><td>TEST, CASE </td><td style="white-space:nowrap;">1/27/2011 5:05:07 PM</td><td align="left">
                                                    <span id="ctl00_ContentPlaceHolderMain_gvWorklist_ctl04_LabelAssignedDoctor"></span>
                                                </td>

Open in new window

0
 
patd1Author Commented:
I used viewstate = true on the grid and tracked my checkboxes in a ayyar in the OnDataBinding event and then reassigned the checks in the RowDatabound event. That took care of the requirments without the javascript.

Thanks to all for help though.
0
 
patd1Author Commented:
used a different solution.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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