Why can't I see my dynamically created controls? - asp.net

When I click on the button just nothing happens!
public void cmdButton1_OnClick(object sender, EventArgs e)
{
Panel myPanel = new Panel();
myPanel.ID = "Panel1";
for (int i = 0; i < numtourist; i++)
{
Label myLabel = new Label();
myLabel.ID = "lblNameL" + i.ToString();
myLabel.Text = "Трите имена на латиница ";
TextBox myTextBox1 = new TextBox();
myTextBox1.ID = "txtNameL" + i.ToString();
myPanel.Controls.Add(myLabel);
myPanel.Controls.Add(myTextBox1);
Label mylabel2 = new Label();
mylabel2.ID = "lblNameK" + i.ToString();
mylabel2.Text = "Трите имена на кирилица";
TextBox myTextBox2 = new TextBox();
myTextBox2.ID = "txtNameK" + i.ToString();
myPanel.Controls.Add(mylabel2);
myPanel.Controls.Add(myTextBox2);
Label myLabel3 = new Label();
myLabel3.ID = "lblEGN" + i.ToString();
myLabel3.Text = "EГН";
TextBox myTextBox3 = new TextBox();
myTextBox3.ID = "txtEGN" + i.ToString();
myPanel.Controls.Add(myLabel3);
myPanel.Controls.Add(myTextBox3);
Label myLabel4 = new Label();
myLabel4.ID = "lblPersonalCardNum" + i.ToString();
myLabel4.Text = "Номер на лична карта";
TextBox myTextBox4 = new TextBox();
myTextBox4.ID = "txtPersonalCardNum" + i.ToString();
myPanel.Controls.Add(myLabel4);
myPanel.Controls.Add(myTextBox4);
Label myLabel5 = new Label();
myLabel5.ID = "lblDateOfIssuePC " + i.ToString();
myLabel5.Text = "Дата на издаане на лична карта:";
TextBox myTextBox5 = new TextBox();
myTextBox5.ID = "txtDateOfIssuePC" + i.ToString();
myPanel.Controls.Add(myLabel5);
myPanel.Controls.Add(myTextBox5);
Label myLabel6 = new Label();
myLabel6.ID = "lblDateOfExpiryPC " + i.ToString();
myLabel6.Text = "Дата на валидност на лична карта:";
TextBox myTextBox6 = new TextBox();
myTextBox6.ID = "txtDateOfExpiryPC" + i.ToString();
myPanel.Controls.Add(myLabel6);
myPanel.Controls.Add(myTextBox6);
Label mylabel6_1 = new Label();
mylabel6_1.ID = "lblIssuedFrom" + i.ToString();
mylabel6_1.Text = "Издадеа от";
TextBox myTextBox6_1 = new TextBox();
myTextBox6_1.ID = "txtIssuedFrom" + i.ToString();
myPanel.Controls.Add(mylabel6_1);
myPanel.Controls.Add(myTextBox6_1);
Label myLabel7 = new Label();
myLabel7.ID = "lblDateOfIssuePass " + i.ToString();
myLabel7.Text = "Дата на издаване на международен паспорт:";
TextBox myTextBox7 = new TextBox();
myTextBox7.ID = "txtDateOfIssuePass" + i.ToString();
myPanel.Controls.Add(myLabel7);
myPanel.Controls.Add(myTextBox7);
Label myLabel7_1 = new Label();
myLabel7_1.ID = "lblPassportNum" + i.ToString();
myLabel7_1.Text = "Номер на паспорт:";
TextBox myTextBox7_1 = new TextBox();
myTextBox7_1.ID = "txtPassportNum" + i.ToString();
myPanel.Controls.Add(myLabel7_1);
myPanel.Controls.Add(myTextBox7_1);
Label myLabel8 = new Label();
myLabel8.ID = "lblDateOfExpiryPass " + i.ToString();
myLabel8.Text = "Дата на валидност на международен паспорт:";
TextBox myTextBox8 = new TextBox();
myTextBox8.ID = "txtDateOfExpiryPass" + i.ToString();
myPanel.Controls.Add(myLabel8);
myPanel.Controls.Add(myTextBox8);
Label myLabel9 = new Label();
myLabel9.ID = "lblHomeContact" + i.ToString();
myLabel9.Text = "Домашен телефон";
TextBox myTextBox9 = new TextBox();
myTextBox9.ID = "txtHomeContact" + i.ToString();
myPanel.Controls.Add(myLabel9);
myPanel.Controls.Add(myTextBox9);
Label myLabel10 = new Label();
myLabel10.ID = "lblMobContact" + i.ToString();
myLabel10.Text = "Мобилен телефон";
TextBox myTextBox10 = new TextBox();
myTextBox10.ID = "txtMobContact" + i.ToString();
myPanel.Controls.Add(myLabel10);
myPanel.Controls.Add(myTextBox10);
Label myLabel11 = new Label();
myLabel11.ID = "lblEmail" + i.ToString();
myLabel11.Text = "E-mail адрес";
TextBox myTextBox11 = new TextBox();
myTextBox11.ID = "txtEmail" + i.ToString();
myPanel.Controls.Add(myLabel11);
myPanel.Controls.Add(myTextBox11);
Label myLabel12 = new Label();
myLabel12.ID = "lblAddress" + i.ToString();
mylabel2.Text = "Адрес";
TextBox myTextBox12 = new TextBox();
myTextBox12.ID = "txbAddress" + i.ToString();
myPanel.Controls.Add(myLabel12);
myPanel.Controls.Add(myTextBox12);
}
}

You are missing one more step. Now add your panel control to another control container or at least your page control.

You have created a panel and addet the others controls on it,
but you forgot to add this created panel (myPanel) to your form.
Youre missing the following line of code:
myForm.Controls.Add(myPanel);
Change myForm to the name of your form.

Related

Nested foreach() messing up

Good Day, guys.
I'm currentl making a full page overlay for navigation and I've been trying to figure out this problem for the past 2 days regarding my nested foreach() loops.
I have these pages which may be linked with posts. and here is what I'm trying to achieve:
Home Page
Technologies Page
Post 1/ Post 2/ Post3
Contact Us Page
About Us Page
But what I'm getting is:
Home Page
Technologies Page
Post 1/ Post 2/ Post3
Contact Us Page
Post 1/ Post 2/ Post3
About Us Page
Post 1/ Post 2/ Post3
Here are my code:
public DataTable GetMain()
{
string sql = "SELECT Id,Title,Slug from Pages";
SqlCommand SQLComm = new SqlCommand(sql, con);
SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm);
DataTable dt = new DataTable();
SQLAd.Fill(dt);
return dt;
}
private void GetAll()
{
DataTable dt = new DataTable();
HyperLink newhyperlink = new HyperLink();
Label newlabel = new Label();
foreach (DataRow row in GetMain().Rows)
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown"" data-toggle=""collapse""";
newlabel = new Label();
newhyperlink.ID = "Main";
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row["Title"].ToString();
newhyperlink.NavigateUrl = row["Slug"].ToString();
newhyperlink = new HyperLink();
html.Append("></li>");
html.Append("<ul class=\"list-group\">");
var sql2 = "SELECT po.Id as PostId, po.Slug as PostSlug, po.Title as PostTitle, pa.Slug as PageSlug, pa.Id as PageId FROM Posts po " +
"LEFT JOIN PagesPostsMap m ON po.Id = m.PostId " +
"LEFT JOIN Pages pa ON m.PageId = pa.Id " +
"WHERE pa.Id = "+row["Id"] + "";
SqlDataAdapter SQLAd = new SqlDataAdapter(sql2, con);
SQLAd.Fill(dt2);
foreach (DataRow row2 in dt2.Rows)
{
object value = row2["PostId"];
if (value != DBNull.Value)
{
if (row["Id"] != row2["PageId"])
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown-menu""";
newlabel = new Label();
newhyperlink.ID = "Sub" + i.ToString();
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row2["PostTitle"].ToString();
newhyperlink.NavigateUrl = row2["PageSlug"].ToString() + "/" + row2["PostSlug"].ToString();
newhyperlink = new HyperLink();
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = "</li>";
newlabel = new Label();
}
}
}
}
}
I'm sorry if the the classes and stuff are not adding up. I'm still trying to figure out how css and JS works.
And thank for helping me.
Your code is actually just doing what you tell it to do. For every row in your GetMain, you order everything in your dt2 (datatable2). If you want what you try to achive, you have to set a condition and put your second foreach into that condition.
if(tech parts row)
{
"your second foreach"
}
If I missunderstand anything, feel free to add more info
public DataTable GetMain()
{
string sql = "SELECT Id,Title,Slug from Pages";
SqlCommand SQLComm = new SqlCommand(sql, con);
SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm);
DataTable dt = new DataTable();
SQLAd.Fill(dt);
return dt;
}
private void GetAll()
{
DataTable dt = new DataTable();
HyperLink newhyperlink = new HyperLink();
Label newlabel = new Label();
foreach (DataRow row in GetMain().Rows)
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown"" data-toggle=""collapse""";
newlabel = new Label();
newhyperlink.ID = "Main";
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row["Title"].ToString();
newhyperlink.NavigateUrl = row["Slug"].ToString();
newhyperlink = new HyperLink();
html.Append("></li>");
html.Append("<ul class=\"list-group\">");
var sql2 = "SELECT po.Id as PostId, po.Slug as PostSlug, po.Title as PostTitle, pa.Slug as PageSlug, pa.Id as PageId FROM Posts po " +
"JOIN PagesPostsMap m ON po.Id = m.PostId " +
"JOIN Pages pa ON m.PageId = pa.Id " +
"WHERE pa.Id = "+row["Id"] + "";
SqlDataAdapter SQLAd = new SqlDataAdapter(sql2, con);
SQLAd.Fill(dt2);
foreach (DataRow row2 in dt2.Rows)
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown-menu""";
newlabel = new Label();
newhyperlink.ID = "Sub" + i.ToString();
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row2["PostTitle"].ToString();
newhyperlink.NavigateUrl = row2["PageSlug"].ToString() + "/" + row2["PostSlug"].ToString();
newhyperlink = new HyperLink();
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = "</li>";
newlabel = new Label();
}
}
}

How do I auto generate content based on my database?

Good day. I have this content form but I do not know how to generate it automatically depending on the number of results from the database. I'm fairly new to ASP.Net
<asp:Content ID="Content1" ContentPlaceHolderID="AdminMain" runat="server">
<h1 class="container"><asp:Label runat="server" ID="PageTitle"></asp:Label></h1>
<div class="container">
<hgroup class="title">
<h1 class="post-title">
<asp:Label runat="server" ID="PostName"></asp:Label>
</h1>
<h6>Posted by <asp:Label runat="server" ID="AuthorName"></asp:Label> on <asp:Label runat="server" ID="PublushedDate"></asp:Label></h6>
</hgroup>
<div class="post-content">
<asp:Label runat="server" ID="PostContent"></asp:Label>
</div>
<div class="post-tags">
<h6>Categorized as:<asp:Label runat="server" ID="PostCategory"></asp:Label><br /></h6>
<h6>Tagged as:<asp:Label runat="server" ID="PostTag"></asp:Label></h6>
</div>
Here's what I'm going to populate it with:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetAllPosts();
}
}
private void GetAllPosts()
{
var sql = "SELECT p.*, t.Id as TagId, t.Name as TagName, " +
"t.UrlFriendlyName as TagUrlFriendlyName, u.Username FROM Posts p " +
"LEFT JOIN PostsTagsMap m ON p.Id = m.PostId " +
"LEFT JOIN Tags t ON t.Id = m.TagId " +
"INNER JOIN Users u ON u.Id = p.AuthorId";
SqlConnection SQLConn = new SqlConnection(con);
SqlCommand SQLComm = new SqlCommand(sql,SQLConn);
SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm);
DataTable dt = new DataTable();
SQLAd.Fill(dt);
foreach (DataRow row in dt.Rows)
{
PageTitle.Text = "All Posts";
PostName.Text = row["Title"].ToString();
AuthorName.Text = row["Username"].ToString();
PublishedDate.Text = Convert.ToDateTime(row["DatePublished"]).ToString();
PostContent.Text = row["Content"].ToString();
PostCategory.Text = "Events";
PostTag.Text = row["TagName"].ToString();
}
}
I appreciate the input and the help.
Webforms are terrible.
int i = 1;
PageTitle.Text = "All Posts";
foreach (DataRow row in dt.Rows)
{
Label newlabel = new Label();
PlaceHolder newplaceholder = new PlaceHolder();
newlabel.ID ="PostName" + i.ToString() + "</a>";
//html.Append(#"<hgroup class=""title"">");
//html.Append(#"<h1 class=""post-title"">");
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<hgroup class=""title""><h1 class=""post-title"">" + row["Title"].ToString() + "</h1>";
newlabel = new Label();
newlabel.ID = "AuthorName" + i.ToString();
html.Append(#"<h6>");
PanelPosts.Controls.Add(newlabel);
newlabel.Text ="Posted by: "+row["Username"].ToString()+" on ";
newlabel = new Label();
newlabel.ID = "PublishedDate" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = row["DatePublished"].ToString()+"</h6>";
html.Append("</hgroup>");
newlabel = new Label();
newlabel.ID = "PostContent" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<div class=""post-content""><h3>" + row["Content"].ToString() + "</h3><br /></div>";
newlabel = new Label();
newlabel.ID = "PostCategory" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<div class=""post-tags""><h6>Categorized as: " + "Category" + " <br/> </h6>";
newlabel = new Label();
newlabel.ID = "PostTag" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<h6>Tagged as: <a href=""/archive/tag/{TagUrlFriendlyName}/"">"+row["TagName"].ToString()+"</div>";
PanelPosts.Controls.Add(new LiteralControl("<br />"));
i++;
}

Can't get value from dynamically created control in dynamically created foter row in grid view

I dynamically created footer row in row_created event and add dynamically created controls on button click which also dynamically created in footer I want to save data in linq table But dosen't works
here is my code I really need help
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
GridViewRow foter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter1 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter2 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter3 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
TableCell span = new TableCell();
span.Attributes.Add("rowspan", "3");
span.Attributes.Add("width", "180px");
FileUpload f = new FileUpload();
f.ID = "flupImage";
f.Attributes.Add("onchange", "ShowpImagePreview(this);");
Image img = new Image();
img.ID = "ImgPrv";
img.ClientIDMode = System.Web.UI.ClientIDMode.Static;
img.Attributes.Add("Height", "60px");
img.Attributes.Add("Width", "60px");
span.Controls.Add(img);
span.Controls.Add(new LiteralControl("<br />"));
span.Controls.Add(f);
span.HorizontalAlign = HorizontalAlign.Left;
TableCell celf = new TableCell();
Label l1 = new Label();
l1.Text = "ID ";
TextBox t1 = new TextBox();
t1.ID = "id";
celf.Controls.Add(l1);
celf.Controls.Add(t1);
celf.ColumnSpan = 4;
celf.HorizontalAlign = HorizontalAlign.Right;
TableCell celf2 = new TableCell();
Label l2 = new Label();
l2.Text = "naziv";
TextBox t2 = new TextBox();
t2.ID = "naziv";
celf2.Controls.Add(l2);
celf2.Controls.Add(t2);
celf2.ColumnSpan = 3;
celf2.HorizontalAlign = HorizontalAlign.Right;
TableCell celf3 = new TableCell();
Label l3 = new Label();
l3.Text = "opis";
TextBox t3 = new TextBox();
t3.ID = "opis";
celf3.Controls.Add(l3);
celf3.Controls.Add(t3);
celf3.ColumnSpan = 4;
celf3.HorizontalAlign = HorizontalAlign.Right;
TableCell celf4 = new TableCell();
Label l4 = new Label();
l4.Text = "cena";
TextBox t4 = new TextBox();
t4.ID = "cena";
celf4.Controls.Add(l4);
celf4.Controls.Add(t4);
celf4.ColumnSpan = 3;
celf4.HorizontalAlign = HorizontalAlign.Right;
TableCell celf5 = new TableCell();
Label l6 = new Label();
l6.Text = "kategorija";
TextBox t6 = new TextBox();
t6.ID = "kategorija";
celf5.Controls.Add(l6);
celf5.Controls.Add(t6);
celf5.ColumnSpan = 7;
celf5.HorizontalAlign = HorizontalAlign.Right;
TableCell celf6 = new TableCell();
Button Button1 = new Button();
Button1.Text = "ubaci nov";
celf6.Controls.Add(Button1);
Button1.Click += new System.EventHandler(Button1_Click);
celf6.ColumnSpan = 7;
celf6.HorizontalAlign = HorizontalAlign.Center;
foter.Cells.Add(span);
foter.Cells.Add(celf);
foter.Cells.Add(celf2);
foter1.Cells.Add(celf3);
foter1.Cells.Add(celf4);
foter2.Cells.Add(celf5);
foter3.Cells.Add(celf6);
GridView1.Controls[0].Controls.Add(foter);
GridView1.Controls[0].Controls.Add(foter1);
GridView1.Controls[0].Controls.Add(foter2);
GridView1.Controls[0].Controls.Add(foter3);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext data = new DataClasses1DataContext();
Proizvodi video = new Proizvodi();
FileUpload file = (GridView1.FooterRow.FindControl("flupImage") as FileUpload);
byte[] fileByte = file.FileBytes;
Binary bin = new Binary(fileByte);
video.slika = bin;
string prID = (GridView1.FooterRow.FindControl("id") as TextBox).Text;
int proID = Convert.ToInt32(prID);
video.proizvodID = proID;
string naziv = (GridView1.FooterRow.FindControl("naziv") as TextBox).Text;
video.naziv = naziv;
string opis = (GridView1.FooterRow.FindControl("opis") as TextBox).Text;
video.opis = opis;
decimal cena=Convert.ToDecimal( (GridView1.FooterRow.FindControl("cena") as TextBox).Text);
video.cena = cena;
string katId = (GridView1.FooterRow.FindControl("kategorija") as TextBox).Text;
video.kategorijaID = Convert.ToInt32(katId);
data.Proizvodis.InsertOnSubmit(video);
data.SubmitChanges();
GridView1.DataBind();
}
I try some solution from net but dosen't work.
please help

how to add button in Dynamic gridview?

public void BuildMeatTally()
{
try
{
DataTable dtdate = new DataTable();
objRetailPL.status = 4;
dtdate = objRetailBAL.GetTypeNew(objRetailPL);
if (dtdate.Rows.Count > 0)
{
for (int i = 0; i < dtdate.Rows.Count; i++)
{
string branchdate;
int totbirds; float totwt, meatyield, Avgweight;
DateTime datewr;
DataTable dtdatedetailsTable = new DataTable();
objRetailPL.branchdate = dtdate.Rows[i]["BranchDate"].ToString();
objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
dtdatedetailsTable = objRetailBAL.getbradisdatewisedet(objRetailPL);
branchdate = dtdatedetailsTable.Rows[0]["BranchDate"].ToString();
totbirds = Convert.ToInt32(dtdatedetailsTable.Rows[0]["numofbirds"].ToString());
totwt = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalweght"].ToString());
meatyield = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalmeatyeild"].ToString());
Avgweight = Convert.ToSingle(dtdatedetailsTable.Rows[0]["AvgWeight"].ToString());
Table tbldynamic = new Table();
tbldynamic.BorderStyle = BorderStyle.Ridge;
tbldynamic.Width = 600;
TableCell tc = new TableCell();
TableRow tr = new TableRow();
Label lblbranchdate = new Label();
lblbranchdate.ID = "lblbrandate" + i.ToString();
lblbranchdate.Text = "&nbsp&nbsp" + "<B>Branch Date:</B>" + "&nbsp";
tc.Controls.Add(lblbranchdate);
Label lblbranchdatedata = new Label();
lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
lblbranchdatedata.Text = branchdate + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblbranchdatedata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lbltotwt1 = new Label();
lbltotwt1.ID = "lbltotwt1" + i.ToString();
lbltotwt1.Text = "<B>Total Weight:</B>" + "&nbsp";
tc.Controls.Add(lbltotwt1);
Label lbltotwt1data = new Label();
lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
lbltotwt1data.Text = totwt + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lbltotwt1data);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
tc = new TableCell();
tr = new TableRow();
Label lbltotbirds = new Label();
lbltotbirds.ID = "lbltotbird" + i.ToString();
lbltotbirds.Text = "<B>Total Birds:</B>" + "&nbsp";
tc.Controls.Add(lbltotbirds);
Label lbltotbirddata = new Label();
lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
lbltotbirddata.Text = totbirds + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lbltotbirddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblmeatyield = new Label();
lblmeatyield.ID = "lblmeatyield" + i.ToString();
lblmeatyield.Text = "<B>Meat Yield in Kgs:</B>" + "&nbsp";
tc.Controls.Add(lblmeatyield);
Label lblmeatyielddata = new Label();
lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
lblmeatyielddata.Text = meatyield + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblmeatyielddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblavgweight = new Label();
lblavgweight.ID = "lblavgweight" + i.ToString();
lblavgweight.Text = "<B>Avg Weight:</B>" + "&nbsp";
tc.Controls.Add(lblavgweight);
Label lblavgweightdata = new Label();
lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
lblavgweightdata.Text = Avgweight + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblavgweightdata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
DataTable dtdcDetails = new DataTable();
objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
dtdcDetails = objRetailBAL.dtdateTallyTable(objRetailPL);
GridView gv = new GridView();
//gv.ID = "gv" + dtdate.Rows[i]["BranchDate"].ToString();
gv.Width = 600;
Label lblTotal = new Label();
Label lbltotaleggs = new Label();
BoundField partyname = new BoundField();
partyname.HeaderText = "PartyName";
partyname.DataField = "partyname";
gv.Columns.Add(partyname);
BoundField birdtype = new BoundField();
birdtype.HeaderText = "Bird Type";
birdtype.DataField = "birdname";
gv.Columns.Add(birdtype);
BoundField totweight = new BoundField();
totweight.HeaderText = "Total Weight";
totweight.DataField = "totalweight";
gv.Columns.Add(totweight);
BoundField rateperkg = new BoundField();
rateperkg.HeaderText = "Rate/Kg";
rateperkg.DataField = "rateperkg";
gv.Columns.Add(rateperkg);
BoundField Bdcno = new BoundField();
Bdcno.HeaderText = "DCNo";
Bdcno.DataField = "dcno";
gv.Columns.Add(Bdcno);
// Here I want to add Button(confirm) In dynamic Gridview..
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtdcDetails;
gv.DataBind();
}
}
}
how can i add button control in Dynamic gridview , i tried with Templeate field, but i am not getting exactly output...after how can i add Button Click event for it? please help me?
Thanks in advance...
Sambasiva
You cannot add control dynamically in GridView Control,
You need to create a class and extends it ITemplate.
Following links will explain it
http://www.codedigest.com/Articles/ASPNET/168_Create_Dynamic_GridView_Control_in_C_ASPNet.aspx
http://www.codeproject.com/Articles/13462/How-to-create-template-columns-dynamically-in-a-gr

Object reference not set to an instance of an object-exception

I have a panel with ID:Panel2 in my web page and I'm adding adynamic table into it.
I'm getting an "Object reference not set to an instance of an object" exception.
My code:
for (int i = 0; i <= val; i++)
{
Table Table4 = new Table();
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thc2 = new TableHeaderCell();
thc2.BorderColor = Color.Black;
thc2.BorderWidth = 2;
TableHeaderCell thc3 = new TableHeaderCell();
thc3.BorderColor = Color.Black;
thc3.BorderWidth = 2;
TableHeaderCell thc4 = new TableHeaderCell();
thc4.BorderColor = Color.Black;
thc4.BorderWidth = 2;
TableHeaderCell thc5 = new TableHeaderCell();
thc5.BorderColor = Color.Black;
thc5.BorderWidth = 2;
Label l2 = new Label();
l2.Text = "Check Point";
l2.ForeColor = Color.Black;
Label l3 = new Label();
l3.Text = "Applicability";
l3.ForeColor = Color.Black;
Label l4 = new Label();
l4.Text = "Effectiveness";
l4.ForeColor = Color.Black;
Label l5 = new Label();
l5.Text = "Score";
l5.ForeColor = Color.Black;
thc2.Controls.Add(l2);
thc3.Controls.Add(l3);
thc4.Controls.Add(l4);
thc5.Controls.Add(l5);
thr.Cells.Add(thc2);
thr.Cells.Add(thc3);
thr.Cells.Add(thc4);
thr.Cells.Add(thc5);
Table4.Rows.Add(thr);
TableRow tr = new TableRow();
tr.BorderColor = Color.Black;
tr.BorderWidth = 2;
TableCell c2 = new TableCell();
c2.BorderColor = Color.Black;
c2.BorderWidth = 2;
c2.Text = Convert.ToString(s[i]);
TableCell c3 = new TableCell();
c3.BorderColor = Color.Black;
c3.BorderWidth = 2;
TableCell c4 = new TableCell();
c4.BorderColor = Color.Black;
c4.BorderWidth = 2;
TableCell c5 = new TableCell();
c5.BorderColor = Color.Black;
c5.BorderWidth = 2;
DropDownList ddl1 = new DropDownList();
ddl1.Items.AddRange(items2);
ddl1.ID = "ddl1" + i;
DropDownList ddl2 = new DropDownList();
ddl2.Items.AddRange(items1);
ddl2.ID = "ddl2" + i;
TextBox t4=new TextBox();
t4.ID = "textID4" + i;
t4.EnableViewState = true;
c2.ID = "newC2" + i;
c3.ID = "newC3" + i;
c4.ID = "newC4" + i;
c5.ID = "newC5" + i;
tr.ID = "newRow" + i;
c3.Controls.Add(ddl1);
c4.Controls.Add(ddl2);
c5.Controls.Add(t4);
tr.Cells.Add(c2);
tr.Cells.Add(c3);
tr.Cells.Add(c4);
tr.Cells.Add(c5);
Table4.Rows.Add(tr);
-----> this.Panel2.Controls.Add(Table4);
Session["table"] = Table4;
}
In line (this.Panel2.Controls.Add(Table4)), I'm getting
Object reference not set to an instance of an object-exception.
even though I have created a Panel in the design page I'm getting the error
Here any table is added dynamically not the Panel, Panel is created statically and I'm inserting dynamic table in it.
There are a couple of reasons this can happen, but the most important is:
Are you absolutely sure you have created a control with the exact id
with the runat attribute set to "server"?
This applies to both Panel2 and Table4.
Other than that you need to check these things:
Your class (where the exception is occuring) is inheriting from System.UI.Page
The #Page CodeBehind attribute is set to your class (spelling, capitalisation)
You're referencing the object at the correct time during the page lifecycle (i.e. before it's being initialised)
There's no where that the reference has been modified prior to use

Resources