繁体   English   中英

在asp.net c#中的gridview中将TextboxTexchange添加到动态创建的文本框中

[英]Adding TextboxTexchange to dynamically created textbox in gridview in asp.net c#

我在动态创建的gridview上标记textchanged事件时遇到问题(由于复杂的数据源,数据透视表等,我在没有templatefield或boundfield的情况下使其成为动态的)

这是我的gridview片段

  <asp:HiddenField ID="SelectedGridCellIndex" runat="server" Value="-1" />
    <asp:GridView ID="dgvTask" runat="server" OnRowDataBound="dgvTask_RowDataBound" OnRowEditing="dgvTask_RowEditing">
        <Columns>
            <asp:BoundField DataField="Task" HeaderText="Task" >
            <ControlStyle BackColor="White" BorderColor="Black" />
            <ItemStyle BackColor="White" BorderColor="Black" BorderStyle="Solid" ForeColor="Black" />
            </asp:BoundField>
        </Columns>
        <HeaderStyle BorderStyle="Solid" />
    </asp:GridView>

这是在gridview中生成文本框的单独类中的代码

        _Default def = new _Default();
                //Creates a new text box control and add it to the container.

                TextBox tb1 = new TextBox();                            //Allocates the new text box object.
                tb1.AutoPostBack = true;
                tb1.TextChanged += new EventHandler(def.TextBox1_TextChanged);
                tb1.DataBinding += new EventHandler(tb1_DataBinding);   //Attaches the data binding event.

                tb1.Columns = 8;                                        //Creates a column with size 4.                    
                container.Controls.Add(tb1);                            //Adds the newly created textbox to the container.

正如您所看到的,我添加了一个回发并从默认页面标记了textchanged事件。

这是默认页面中的TextChanged事件

        public void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string task = dgvTask.SelectedRow.Cells[0].Text;
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + task + "')", true);
    }    

我还尝试在RowDatabound事件上标记它

 protected void dgvTask_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[1].Visible = false;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                TableCell cell = e.Row.Cells[i];
                cell.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                cell.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                cell.ToolTip = "You can click this cell";
                TextBox txt = e.Row.FindControl("txtdata") as TextBox;
                txt.TextChanged += new EventHandler(TextBox1_TextChanged);
                //cell.Attributes["onclick"] = string.Format("document.getElementById('{0}').value = {1}; {2}"
                //   , SelectedGridCellIndex.ClientID, i
                //   , Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, string.Format("Select${0}", e.Row.RowIndex)));
            }
        }
    }

发生的事情是,在我输入文本框并按Tab键(它应该触发textchanged事件)后,会触发回发,但不会触发textchanged事件。

希望您理解我的问题和代码。 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM