Dynamically added dropdownlist in GridView based on TextBox value using C# not working well
What i did was to add dropdown list and the code stop working because i think am not inputing the right code.
Originally when you click the add button it is suppose to add (n numbers) of 2 text boxes. But i added a dropdown list so that when you click on add button it adds (n numbers) 2 textboxes and dropdownlist.
My code
public partial class Ast : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.GridView1.Rows.Count > 0 && Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];
dt.Clear();
foreach (GridViewRow row in this.GridView1.Rows)
{
string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
string lastName = (row.FindControl("txtLastName") as TextBox).Text;
string age = (row.FindControl("ageperson") as DropDownList).Text;
dt.Rows.Add(firstName, lastName,age);
}
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
Session["myDatatable"] = dt;
}
}
public void AddNewData()
{
DataTable dt = new DataTable();
if (Session["myDatatable"] != null)
{
dt = (DataTable)Session["myDatatable"];
}
else
{
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("age");
}
DataRow drow = dt.NewRow();
drow["FirstName"] = "";
drow["LastName"] = "";
drow["age"] = "";
dt.Rows.Add(drow);
Session["myDatatable"] = dt;
}
public void BindGridview()
{
if (Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];
if ((dt != null) && (dt.Rows.Count > 0))
{
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.Visible = false;
}
}
}
protected void Add(object sender, EventArgs e)
{
for (int i = 0; i < Convert.ToInt32(txtNo.Text); i++)
{
AddNewData();
}
BindGridview();
}
protected void OnSave(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
string lastName = (row.FindControl("txtLastName") as TextBox).Text;
string age = (row.FindControl("ageperson") as DropDownList).Text;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Student VALUES(@First,@Last,@age)";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@First", firstName);
cmd.Parameters.AddWithValue("@Last", lastName);
cmd.Parameters.AddWithValue("@age", age);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Session["myDatatable"] = null;
Response.Redirect("material.aspx");
}
}
<asp:TextBox ID="txtNo" runat="server" />
<asp:Button ID="Button1" Text="Add" OnClick="Add" runat="server" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="315px">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:DropDownList ID="ageperson" runat="server" Height="25px" Text='<%# Eval("age") %>'
Style="margin-left: 0px" Width="416px"
AutoPostBack="True" Font-Names="Century Gothic" Font-Bold="True">
<asp:ListItem Selected="True" Value="1">Offline Die</asp:ListItem>
<asp:ListItem Value="3">Cylinder</asp:ListItem>
<asp:ListItem Value="4">Cutting Die</asp:ListItem>
<asp:ListItem Value="6">Embossing</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
please help