Dynamically created textbox event Not firing

Posted by Manoj_B under ASP.NET on 5/8/2013 | Points: 10 | Views : 965 | Status : [Member] | Replies : 3
hii,

how to create dynamically created textbox event ??

here is my code :

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.Header)
{
DataTable dt = new DataTable();

dt = obj.GetDataTable("ConnectionString", "SELECT vchName FROM Employee");

if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TableCell HeaderCell = new TableCell();
HeaderCell.Text = dt.Rows[i].ItemArray[0].ToString();
HeaderCell.Width = 80;
e.Row.Cells.AddAt(0, HeaderCell);

}

}

}
if (e.Row.RowType == DataControlRowType.DataRow)
{

DataTable dt = new DataTable();

dt = obj.GetDataTable("ConnectionString", "SELECT vchName FROM Employee");

if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox txt = new TextBox();
txt.ID = "txtID"+i;
txt.Visible = true;
txt.Text = "";
txt.Width = 50;
txt.MaxLength = 2;
//txt.TextChanged += new EventHandler(this.txt_TextChanged);
TableCell RowCell = new TableCell();

RowCell.Controls.Add(txt);

e.Row.Cells.AddAt(0, RowCell);

}

}

}

}



Please help me .it's urgent.

Thanks & Regards
Manoj Kumar



Responses

Posted by: Kmandapalli on: 5/8/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Try this..
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox txtBox = new TextBox();
txtBox.Name = "TextBoxName";
txtBox.Location = new Point(x, y);
this.panel1.Controls.Add(txtBox);
txtBox.TextChanged += new EventHandler(this.TextBox_TextChanged);
}

//After writing txtBox.TextChanged += ,click the tab twice, then you will get the event..

private void TextBox_TextChanged(object sender, System.EventArgs e)
{
//write your code here...
}

Mark as Answer if satisfied........

Regards,
Shree M.

Kavya Shree Mandapalli

Manoj_B, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manojmevada on: 5/8/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

You need to define your text box event in design mode in Item Template. Please refer below code:
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlCountry" AutoPostBack="true" OnSelectedIndexChanged="General_SelectedIndexChanged" >
</asp:DropDownList>
</ItemTemplate>
Thanks,
Manoj Mevada

Manoj Mevada

Manoj_B, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manojmevada on: 5/8/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
here is another solution to this problem:
You need to remove event binding from design mode and find controls in all rows and bind events after DataBind method of GridView
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
ddl = (DropDownList)row.Cells[1].FindControl("ddlCountry");
ddl.SelectedIndexChanged += new EventHandler(General_SelectedIndexChanged);
}
}
Thanks,
Manoj Mevada

Manoj Mevada

Manoj_B, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response