使用Update Pannel在GridView动态asp:TextBox上绑定Focus Out事件

时间:2017-01-12 23:31:48

标签: c# asp.net gridview webforms updatepanel

我在更新面板中有一个网格视图,网格有一列包含asp:textbox。我想绑定并触发asyncAutoPostBackTrigger焦点,从动态生成的文本框。我有以下代码

<asp:scriptmanager id="ScriptManager2" runat="server"></asp:scriptmanager>
<asp:updatepanel id="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:GridView ID="GridView1" CssClass="table table-customized table-layout-fixed col-sm-offset-2" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvLicense_RowDataBound">
            <Columns>
                <asp:TemplateField ControlStyle-CssClass="word-wrap-break" HeaderText="">
                    <ItemTemplate>
                        <label class="col-sm-2 control-label" runat="server">Title</label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ControlStyle-CssClass="word-wrap-break" HeaderText="Title">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" placeholder="Title" Text='<%# Bind("vCertType") %>' OnTextChanged="Code_TextChanged"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ControlStyle-CssClass="word-wrap-break" HeaderText="Add">
                    <ItemTemplate>
                        <button type="button" id="btnAddLicense" runat="server" class="btn btn-success-o">+</button>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="<%=Bind(txtLicense.ClientID) %>"   EventName="" />
</Triggers>

问题分解

  • 将焦点绑定在javascript
  • 转到后面的代码获取值并保存到DB

1 个答案:

答案 0 :(得分:0)

您可以删除<Triggers>节点,并将AutoPostBack="true"添加到TextBox。

<asp:TextBox ID="TextBox1" runat="server" placeholder="Title" Text='<%# Bind("vCertType") %>' OnTextChanged="Code_TextChanged" AutoPostBack="true"></asp:TextBox>

然后在代码后面你可以将发送者强制转换回TextBox并获得它的价值。

protected void Code_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMe", "alert('" + tb.Text + "')", true);

    //save to database
}