Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I'm having some difficult with this.
I have a GridView, with some rows, and a colums with a checkbox and a textbox.
I want to when the checkbox is checked, the textbox will have the current date. (only the row where the checkbox is checked).
I0m having a problem to do it in Javascript. Any help?

XML
<asp:GridView ID="gvGridView" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:TemplateField HeaderText="test">
            <ItemTemplate>
                  <asp:CheckBox ID="cbEscolhido1" runat="server" />
              <asp:TextBox ID="txtData1" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
   </Columns>
</asp:GridView>


Thank you in advance
Posted

C#
foreach(GridViewRow row in gvGridView.Rows)
{
    CheckBox chk = (CheckBox)row.FindControl("cbEscolhido1");
    if (chk.Checked)                
       txtData1.Text = DateTime.Today.ToString();                 
                
}


You can modify the above code and try in one of the GridView events.
 
Share this answer
 
ok, done it.

VB
Protected Sub gvGridView_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDocsEmpresas.PreRender
    For Each row As GridViewRow In gvGridView.Rows
        Dim cbEscolhido1 As CheckBox = row.FindControl("cbEscolhido1")
        Dim txtData1 As TextBox = row.FindControl("txtData1")
        cbEscolhido1.Attributes.Add("onclick", "InsereData(this,'" + txtData1.ClientID + "');")
    Next

End Sub


and then the Javascript

C#
function InsereData(me, txtId)
   {
       var dt = new Date();
       var nmonth = dt.getMonth();
       var ntoday = dt.getDate();
       var nyear = dt.getYear();
       nmonth += 1;
       var today = ntoday + "-" + nmonth + "-" + nyear;

       if (me.checked)
       {
          document.getElementById(txtId).value = today;
       }
       else
       {
          document.getElementById(txtId).value = '';
       }
   }



with this, no postbacks :)
 
Share this answer
 

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