在 GridView 中触发值更改的回发

Trigger postback on value change in GridView

我在这个 link DyntimeSample

中使用 dyndatetime

这在 gridview 中使用,每当用户选择新的日期或时间我想使用更新触发回发 panel.I 已经尝试跟踪赋值文本框更改事件但它不是 working.Please 提出一个解决方案。 在下面的代码中,我想在更改事件时触发文本框。 编辑:添加代码

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript">  </script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>

  <script type = "text/javascript">
   $(document).ready(function () {
    $(".Calender").dynDateTime({
        showsTime: true,
        ifFormat: "%Y/%m/%d %H:%M",
        daFormat: "%l;%M %p, %e %m,  %Y",
        align: "BR",
        electric: false,
        singleClick: false,
        displayArea: ".siblings('.dtcDisplayArea')",
        button: ".next()"
      });
   }); 
  </script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Person" />
    <asp:TemplateField HeaderText="Date Of Birth">
        <ItemTemplate>
            <asp:TextBox ID="txtDOB" style="border: 0px solid #505050;"    AutoPostBack="True" OnTextChanged="txtDOB_OnTextChanged" runat="server" ReadOnly="true" class = "Calender" />
            <img src="calender.png" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
     <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
    </div>
   </form>
  </body>

代码隐藏

using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
         this.BindGridWithDummyData();
       }
}

private void BindGridWithDummyData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("DateOfBirth");
    dt.Rows.Add();
    dt.Rows[0]["Name"] = "John";
    dt.Rows.Add();
    dt.Rows[1]["Name"] = "Sam";
    dt.Rows.Add();
    dt.Rows[2]["Name"] = "Tommy";
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        DateTime dob =   DateTime.Parse(Request.Form[row.FindControl("txtDOB").UniqueID]);
        //Save the date to Database here
    }
}

  protected void txtDOB_OnTextChanged(object sender, EventArgs e)
  {
  }
}

注意:如果您在更新面板中将updateMode设置为conditional,那么您需要添加Triggers。在 TextBox1_TextChanged 事件下的代码隐藏文件中,您可以使用 sender.text 属性.

获取最新值

尝试使用以下方法:

<asp:ScriptManager ID="scrManager" runat="server">
</asp:ScriptManager>    
 <asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:GridView ID="grdNumber" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtNumber" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlNumber" runat="server" OnSelectedIndexChanged="ddlNumber_SelectedIndexChanged"
                            AutoPostBack="true">
                            <asp:ListItem>One</asp:ListItem>
                            <asp:ListItem>Two</asp:ListItem>
                            <asp:ListItem>Three</asp:ListItem>
                            <asp:ListItem>For</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

在代码隐藏中:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Rows.Add();
        dt.Rows.Add();
        dt.Rows.Add();
        dt.Rows.Add();

        grdNumber.DataSource = dt;
        grdNumber.DataBind();
    }
}

protected void ddlNumber_SelectedIndexChanged(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    var value = (sender as TextBox).Text;
}

有关更多信息,请查看此 link:http://www.aspsnippets.com/Articles/Assign-PostBack-Trigger-Full-PostBack-for-LinkButton-inside-GridView-within-AJAX-UpdatePanel-in-ASPNet.aspx