GridView内部的Textbox Inside UpdatePanel textchange事件未触发

时间:2013-01-30 05:41:23

标签: c# asp.net updatepanel

我有一个带有GridView的UpdatePanel。此GridView有一个模板列,它是一个文本框。问题是textchange事件不会为文本框触发。

我哪里错了?

以下是代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="updatepanelgridview.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="scrManager" runat="server"> 
</asp:ScriptManager> 
<asp:UpdatePanel ID="updPnl" runat="server"> 
<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> 

    </div>
    </form>
</body>
</html>

这是代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace updatepanelgridview
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        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)
        {

        }
    }
}

这里我在第二列中使用dropdownlist,但是我的文本框textchange事件没有变得冷静请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

将更新面板的更新模式属性设置为“始终”。

<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Always">

在TextBox1_TextChanged事件下的代码隐藏文件中,您可以使用sender.text属性获取最新值。

如果将updateMode设置为conditional,则需要添加触发器。

---------------------这是我试过的代码及其工作原理----------------- -

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="About.aspx.cs" Inherits="About" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
        <asp:ScriptManager ID="scrManager" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="updPnl" runat="server" >

            <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>
    </div>
</asp:Content>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class About : System.Web.UI.Page
{
    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;
    }
}