User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 456,233 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,778 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 3915 | Replies: 8
Reply
Join Date: Nov 2007
Posts: 7
Reputation: vsr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vsr vsr is offline Offline
Newbie Poster

Help datagrid, innertext, javascript, database update

  #1  
Nov 6th, 2007
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>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 1,058
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: datagrid, innertext, javascript, database update

  #2  
Nov 6th, 2007
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)"
Last edited by SheSaidImaPregy : Nov 6th, 2007 at 1:30 pm.
Reply With Quote  
Join Date: Sep 2007
Posts: 1,058
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: datagrid, innertext, javascript, database update

  #3  
Nov 6th, 2007
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 7
Reputation: vsr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vsr vsr is offline Offline
Newbie Poster

Help Re: datagrid, innertext, javascript, database update

  #4  
Nov 6th, 2007
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:


<aspataGrid 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>
</aspataGrid>
Reply With Quote  
Join Date: Sep 2007
Posts: 1,058
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: datagrid, innertext, javascript, database update

  #5  
Nov 6th, 2007
Can you please post your update code, cause it doesn't seem to have a flaw on what you have posted so far.
Reply With Quote  
Join Date: Nov 2007
Posts: 7
Reputation: vsr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vsr vsr is offline Offline
Newbie Poster

Help Re: datagrid, innertext, javascript, database update

  #6  
Nov 6th, 2007
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>
Reply With Quote  
Join Date: Nov 2007
Posts: 7
Reputation: vsr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vsr vsr is offline Offline
Newbie Poster

Solution Re: datagrid, innertext, javascript, database update

  #7  
Nov 7th, 2007
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>
Reply With Quote  
Join Date: Sep 2007
Posts: 1,058
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: datagrid, innertext, javascript, database update

  #8  
Nov 7th, 2007
oh I thought you solved this already! I would have helped otherwise
Reply With Quote  
Join Date: Nov 2007
Posts: 7
Reputation: vsr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vsr vsr is offline Offline
Newbie Poster

Re: datagrid, innertext, javascript, database update

  #9  
Nov 8th, 2007
Hi SheSaidImaPregy
Thanks, I am sure I will need help again some time.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 5:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC