C# 将TextboxTexchange添加到asp.net c中gridview中动态创建的textbox#
我在动态创建的gridview上标记textchanged事件时遇到问题(由于复杂的数据源、透视表等,我故意在没有templatefield或boundfield的情况下将其动态化) 下面是我的gridview代码片段C# 将TextboxTexchange添加到asp.net c中gridview中动态创建的textbox#,c#,asp.net,gridview,dynamic,C#,Asp.net,Gridview,Dynamic,我在动态创建的gridview上标记textchanged事件时遇到问题(由于复杂的数据源、透视表等,我故意在没有templatefield或boundfield的情况下将其动态化) 下面是我的gridview代码片段 <asp:HiddenField ID="SelectedGridCellIndex" runat="server" Value="-1" /> <asp:GridView ID="dgvTask" runat="server" OnRowDataB
<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>
如您所见,我添加了一个回发,并从默认页面标记了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)));
}
}
}
protectedvoid dgvTask_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
e、 行。单元格[1]。可见=假;
如果(e.Row.RowType==DataControlRowType.DataRow)
{
for(int i=0;i
发生的情况是,在我在textbox上输入了一些内容并按tab键(这将触发textchanged事件)后,会触发回发,但textchanged事件不会被触发
希望您理解我的问题和代码。谢谢
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)));
}
}
}