18 July 2011

findcontrol of datalist control in asp.net

Source:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server"
            onitemcommand="DataList1_ItemCommand">
        <ItemTemplate>
        <table border="1">
        <tr>
        <td>
        <%#Eval("id") %>
        </td>
        <td>
        <%#Eval("name") %>
        </td>
        <td>
        <%#Eval("adress") %>
        </td>
        <td>
        <%#Eval("mobil") %>
        </td>
        </tr>
        </table>
        </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                </asp:DropDownList>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="add" CommandName="hhh" />
            </FooterTemplate>
        </asp:DataList>
 
    </div>
    </form>
</body>

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: