18 July 2011

findcontrol of datalist control in asp.net



Source:


        <asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand">


C# code:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
da = new SqlDataAdapter("select * from details", con);
ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox t1 = (TextBox)e.Item.FindControl("TextBox1");
TextBox t2 = (TextBox)e.Item.FindControl("TextBox2");
TextBox t3 = (TextBox)e.Item.FindControl("TextBox3");
TextBox t4 = (TextBox)e.Item.FindControl("TextBox4");

da = new SqlDataAdapter("insert into details values('" + t1.SelectedValue.ToString() + "','" + t2.Text + "','" + t3.Text + "','" + t4.Text + "')", con);
ds = new DataSet();
da.Fill(ds);
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('Inserted Successfully')", true);
}
}

No comments: