Hi
I have a .aspx page that contains some javascript so that
when a user presses a checkbox another cell in the datagrid is filled with the current date . That works fine

My probelm.... when I have the user press the update button (which does a
post back that loops through the datagrid and updates a database) the
field/cell that is filled by the javascript appears to be blank in my update
code, even though I can see it on the screen.

 function getCurrentDateTime(chk, chkId)
    {
      var checkbox = chk
      var lstIndx = chkId.lastIndexOf('chkComplete')
      var spanId = chkId.substr(0,lstIndx) + 'lblDate';
      var spanText = document.getElementById(spanId);

      if (checkbox.checked == true)
      {
        var currentDate = new Date()
        var crDT = currentDate.getDate()+ "/" + currentDate.getMonth() + "/" + currentDate.getFullYear()    ;
      spanText.innerText=crDT;
      spanText.innerHTML=crDT;

      } 
      else
      {
     spanText.innerHTML='';
      }
   }

My datagrid is as follows:

 <asp:DataGrid ID="dgCycleCheckList" runat="server"  Visible="true" AutoGenerateColumns="false" DataKeyField="Cycle_Checklist_id_pk">
      <Columns>
      <asp:TemplateColumn HeaderText="Completed" HeaderStyle-
        HorizontalAlign="Center">
        <ItemTemplate>
          <input type="checkbox" id="chkComplete" runat="server" 
          checked='<%#  DataBinder.Eval(Container.DataItem, "Completed") %>' 
            onclick ="getCurrentDateTime(this,this.id)" />
         </ItemTemplate>  
       </asp:TemplateColumn>
       <asp:BoundColumn DataField="DateCompleted" HeaderText="Date Completed" ReadOnly="true" HeaderStyle-HorizontalAlign="Center"></asp:BoundColumn>
       <asp:TemplateColumn HeaderText="Date Completed"
                  HeaderStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lblDate" runat="server"
                Text='<%# DataBinder.Eval(Container.DataItem, "DateCompleted") %
                 >'></asp:Label
         </ItemTemplate>  
       </asp:TemplateColumn>
    </Columns>  
 </asp:DataGrid>

Have you tried entering in your own values as well? Try entering in your own date value and see if it is posting correctly. Then get back to us.

Oh, and get rid of the space between onclick and =

onclick ="getCurrentDateTime(this,this.id)"

What is also might be, cause I had the same problem when looping through checkboxes to check if they were checked, is the postback variable. On mine, I had to have "if Not Page.IsPostBack then" on my databinder or it did not work. Make sure that when binding your information for checking your checkboxes, that your data is bound on every load, or your viewstate will keep the checkboxes at "false" and it will never be checked.

Hi SheSaidImaPregy-

I entered my own values and they got updated fine. I debugged the code the update function works fine.

Thanks.

Hi
I have a .aspx page that contains some javascript so that
when a user presses a checkbox another cell in the datagrid is filled with the current date . That works fine

My probelm.... when I have the user press the update button (which does a
post back that loops through the datagrid and updates a database) the
field/cell that is filled by the javascript appears to be blank in my update
code, even though I can see it on the screen.
function getCurrentDateTime(chk, chkId)
{
var checkbox = chk
var lstIndx = chkId.lastIndexOf('chkComplete')
var spanId = chkId.substr(0,lstIndx) + 'lblDate';
var spanText = document.getElementById(spanId);

if (checkbox.checked == true)
{
var currentDate = new Date()
var crDT = currentDate.getDate()+ "/" + currentDate.getMonth() + "/" + currentDate.getFullYear() ;
spanText.innerText=crDT;
spanText.innerHTML=crDT;

}
else
{
spanText.innerHTML='';
}
}


My datagrid is as follows:


<asp:DataGrid ID="dgCycleCheckList" runat="server" Visible="true" AutoGenerateColumns="false" DataKeyField="Cycle_Checklist_id_pk">
<Columns>
<asp:TemplateColumn HeaderText="Completed" HeaderStyle-
HorizontalAlign="Center">
<ItemTemplate>
<input type="checkbox" id="chkComplete" runat="server"
checked='<%# DataBinder.Eval(Container.DataItem, "Completed") %>'
onclick ="getCurrentDateTime(this,this.id)" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="DateCompleted" HeaderText="Date Completed" ReadOnly="true" HeaderStyle-HorizontalAlign="Center"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Date Completed"
HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "DateCompleted") %
>'></asp:Label
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Can you please post your update code, cause it doesn't seem to have a flaw on what you have posted so far.

Hi SheSaidImaPregy-

I have added a text box to enter and display date. The value in the text box is getting updated and displayed right.

Thanks.

Hi
I have a .aspx page that contains some javascript so that
when a user presses a checkbox another cell in the datagrid is filled with the current date . That works fine

My probelm.... when I have the user press the update button (which does a
post back that loops through the datagrid and updates a database) the
field/cell that is filled by the javascript appears to be blank in my update
code, even though I can see it on the screen.
This is an urgent request.Any help will be greatly appreciated.

Code behind :

    private void Save()
    {
           string dateCompleted;
            Boolean completed  ;
            int id;

        foreach (DataGridItem dgItem in dgCycleCheckList.Items)
        {
           completed = false;

            HtmlInputCheckBox chkCompleted = (HtmlInputCheckBox)dgItem.FindControl("chkComplete");

            // Label lblDateCompleted = (Label)dgItem.FindControl("lblDate");
           // dateCompleted = lblDateCompleted.Text;

            TextBox txtDate = (TextBox)dgItem.FindControl("txtDate");
            dateCompleted = txtDate.Text;
           id = Convert.ToInt32(dgCycleCheckList.DataKeys[dgItem.ItemIndex]);

            if (chkCompleted.Checked == true)
                completed = true;

                DCycleChecklist dCycleChecklist = new DCycleChecklist();
                dCycleChecklist.UpdateCycleChecklist(completed, dateCompleted, id);

        }
        LoadGrid();
    }

    protected void btnSaveUp_Click(object sender, EventArgs e)
    {
        Save();
    }



<script type="text/javascript">
 function getCurrentDateTime(chk, chkId)
    {
      var checkbox = chk
      var lstIndx = chkId.lastIndexOf('chkComplete')
      var spanId = chkId.substr(0,lstIndx) + 'lblDate';
      var spanText = document.getElementById(spanId);
      //chk.parentNode.childNodes(2).value=chk.parentNode.parentNode.childNodes(0).innerText;
     // chk.parentNode.parentNode.childNodes(2).innerHTML=chk.parentNode.parentNode.childNodes(0).innerText;

      if (checkbox.checked == true)
      {
        var currentDate = new Date()
        var crDT = currentDate.getDate()+ "/" + currentDate.getMonth() + "/" + currentDate.getFullYear()+ " " + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds() ;
        spanText.innerText=crDT;
       spanText.innerHTML=crDT;

       // chk.parentNode.childNodes(1).value=crDT
      } 
      else
      {
     // chk.parentNode.childNodes(1).value='';
        spanText.innerHTML='';
      }
   }

</script>



<table border="0" runat="server">
                <tr>
                    <td>
                        <asp:DataGrid ID="dgCycleCheckList" runat="server"  Visible="true" AutoGenerateColumns="false" DataKeyField="Cycle_Checklist_id_pk">
                                <Columns>
                                    <asp:TemplateColumn HeaderText="Completed"  HeaderStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <input type="checkbox" id="chkComplete" runat="server" checked='<%# DataBinder.Eval(Container.DataItem, "Completed") %>' onclick="getCurrentDateTime(this,this.id)" />
                                        </ItemTemplate>  
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="Date Completed">
                                        <ItemTemplate>
                                             <asp:Label ID="lblDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DateCompleted") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn HeaderText="Date Completed"  HeaderStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                           <asp:TextBox ID="txtDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DateCompleted") %>'></asp:TextBox>
                                        </ItemTemplate>  
                                    </asp:TemplateColumn>
                                              <asp:BoundColumn DataField="Cycle_Checklist_id_pk" Visible="false" HeaderStyle-HorizontalAlign="Center"></asp:BoundColumn>
                                </Columns>  
                        </asp:DataGrid>
                    </td>
                    <td valign="top"  style="height:100%">
                        <table style="height:100%">
                            <tr>
                                <td valign="top" style="width: 45px;height:50%">
                                    <asp:Button ID="btnSaveUp" runat="server" Text="Save" OnClick="btnSaveUp_Click" />
                                </td>
                            </tr>
                            <tr>
                                <td valign="bottom" style="width: 45px; height: 107px">
                                    <asp:Button ID="btnSaveBomttom" runat="server" Text="Save" OnClick="btnSaveBomttom_Click"/>
                                </td>
                            </tr>
                        </table>

                    </td>
                </tr>
            </table>

I solved my problem. After finding the control ((TextBox)dgItem.FindControl("txtDate");)
I am getting the client Id and replacing the "_" with "$" to get the name of the control.
I got it from the viewsource page. I used Request.Form to get the value to update the database.

CODE BEHIND:

private void Save()
{
string dateCompleted;
Boolean completed;
string completedBy;
int id;
string controlPrefix;


foreach (DataGridItem dgItem in dgCycleCheckList.Items)
{
completed = false;


HtmlInputCheckBox chkCompleted = (HtmlInputCheckBox)dgItem.FindControl("chkComplete");



TextBox txtDate = (TextBox)dgItem.FindControl("txtDate");
TextBox txtCompletedBy = (TextBox)dgItem.FindControl("txtCompletedBy");


controlPrefix = txtDate.ClientID;
controlPrefix = controlPrefix.Replace("_", "$");
dateCompleted = Request.Form[controlPrefix];
if (dateCompleted == "")
dateCompleted = null;


controlPrefix = txtCompletedBy.ClientID;
controlPrefix = controlPrefix.Replace("_", "$");
completedBy = Request.Form[controlPrefix];


if (chkCompleted.Checked == true)
completed = true;


id = Convert.ToInt32(dgCycleCheckList.DataKeys[dgItem.ItemIndex]);


DCycleChecklist dCycleChecklist = new DCycleChecklist();
dCycleChecklist.UpdateCycleChecklist(completed, dateCompleted, completedBy, id);



}
LoadGrid();
}



<script type="text/javascript">
function getCurrentDateTime(chk, chkId)
{
var checkbox = chk
var currentUser;
var lstIndx = chkId.lastIndexOf('chkComplete')
var sStr = chkId.substr(0,lstIndx)


var inputId = sStr + 'txtDate';
var oInput= document.getElementById(inputId);


var sUId = sStr + 'txtCompletedBy';
var oUId = document.getElementById(sUId);


currentUser=document.getElementById("ctl00$ContentPlaceHolder1$hdnUserName");
oUId.innerText=currentUser.value;


if (checkbox.checked == true)
{


var currentDate = new Date()
var crDT =(currentDate.getMonth()+1) + "/" + currentDate.getDate()+ "/" + currentDate.getFullYear()+ " " + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds() ;
oInput.innerText=crDT;
oInput.value=crDT;
}
else
{
// spanText.innerHTML='';
oInput.innerText=''
}
}
</script>
<div >
<div class="PageTitle">Cycle Checklist</div>
<table border="2" runat="server">
<tr>
<td>
<asp:DataGrid ID="dgCycleCheckList" runat="server"  Visible="true" AutoGenerateColumns="false" DataKeyField="Cycle_Checklist_id_pk" ItemStyle-VerticalAlign="Bottom" ItemStyle-BorderWidth="15px">
<Columns>
<asp:TemplateColumn HeaderText="Completed"  HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<input type="checkbox" id="chkComplete" runat="server" checked='<%# DataBinder.Eval(Container.DataItem, "Completed") %>' onclick="getCurrentDateTime(this,this.id)" />
</ItemTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Completed By"  HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="txtCompletedBy" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CompletedBy")%>' ReadOnly="true" BackColor="transparent"  BorderStyle="None" BorderColor="transparent" BorderWidth="0"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Date Completed"  HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="50px" HeaderStyle-Width="50px">
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DateCompleted") %>' ReadOnly="true" BackColor="transparent"  BorderStyle="None" BorderColor="transparent" BorderWidth="0" Width="145px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>



<asp:BoundColumn DataField="Cycle_Checklist_id_pk" Visible="false" HeaderStyle-HorizontalAlign="Center"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
<td valign="top" style="height:100%">
<table style="height:100%" border="2">
<tr>
<td valign="top" style="width: 45px; height:50%" >
<asp:Button ID="btnSaveUp" runat="server" Text="Save" OnClick="btnSaveUp_Click" />
</td>
</tr>
<tr>
<td valign="bottom" style="width: 45px; height:50%">
<asp:Button ID="btnSaveBomttom" runat="server" Text="Save" OnClick="btnSaveBomttom_Click"/>
</td>
</tr>
</table>


</td>
</tr>
</table>
<input type="hidden" id="hdnUserName" runat="server" />
</div>

oh I thought you solved this already! I would have helped otherwise :)

Hi SheSaidImaPregy
Thanks, I am sure I will need help again some time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.