在asp.net c#

时间:2016-05-26 08:08:23

标签: c# asp.net gridview dynamic

我在动态创建的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事件。

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

0 个答案:

没有答案