Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a dynamic table with dynamic buttons for company roster according to the no of days in a month, like a company has 5 employees in month of june, i.e table has 150 buttons and on click of every button text appears "OFF" and i want to read that button ID and Value on submit
at server side to save button id in database
Posted

So what is the issue?
When you created those buttons, you must have added the button click event too (if not add it!). Once you add the click event, (which can be generic event for all 150 buttons!) just check the 'sender' object, and you will be able to retrieve the ID using it.
Try out!
 
Share this answer
 
This is not exact code, only a sample. will help you how to find controls and save to database. here am just printing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class dynamicButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < 10; i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "genTxt" + i.ToString();
                PlaceHolder1.Controls.Add(txt);
            }
        }
        else
        {
            //after postback regenerating controls
            for (int i = 0; i < 10; i++)
            {
                if (PlaceHolder1.FindControl("genTxt" + i.ToString()) == null)
                {
                    TextBox txt = new TextBox();
                    txt.ID = "genTxt" + i.ToString();
                    PlaceHolder1.Controls.Add(txt);
                }
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Collections.Hashtable hastTab = new System.Collections.Hashtable();
        for (int i = 0; i < 10; i++)
        {
            string txtID = "genTxt" + i.ToString();
            if (PlaceHolder1.FindControl(txtID) != null)
            {
                TextBox txt = (TextBox)PlaceHolder1.FindControl(txtID);
                if (txt != null)
                {
                    hastTab.Add(txt.ID, txt.Text);
                }
            }
        }

        System.Collections.IDictionaryEnumerator IdEnum = hastTab.GetEnumerator();
        while (IdEnum.MoveNext())
        {
            Response.Write(IdEnum.Key.ToString() + "," + IdEnum.Value.ToString()+"</br>");
        }
    }
}



<pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicButton.aspx.cs" Inherits="dynamicButton" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900