onclick复选框,在Gridview中设置文本框 [英] onclick checkbox, set textbox inside a Gridview

查看:66
本文介绍了onclick复选框,在Gridview中设置文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对此有些困难.
我有一个GridView,有一些行,还有一个带有复选框和文本框的列.
我想在选中复选框时,文本框将具有当前日期. (仅选中复选框的行.)
在使用Javascript时遇到问题.有帮助吗?

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?

<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

推荐答案

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



您可以修改上面的代码并尝试GridView事件之一.



You can modify the above code and try in one of the GridView events.


确定,做到了.

ok, done it.

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



然后是Javascript



and then the Javascript

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 :)


这篇关于onclick复选框,在Gridview中设置文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆