ASP.NET AJAX Toolkit CalendarExtender not working C# - asp.net

I have a folowing problem:
I have a page where I need to create a lot of elements dynamically from the code behind. It obviously depends on what is passed from the database, but the number of elements can change, that's why I cannot do it static.
What I have at the moment, is:
I have statically created PANEL:
<asp:Panel ID="pFullInfo_lStartDateStr" runat="server"></asp:Panel>
Then in Code Behind, I'm creating other Controls and add them to my Label. The Issue I have is that the CalendarExtender that should appear after clicking the iEditStartDateCalendar doesn't popup :( I can't see what I'm doing wrong here ? Any help please ?!?!
// StartDate
Label lStartDateSite = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDateSite);
Label lStartDate = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDate);
ImageButton ibEditStartDate = new ImageButton();
ibEditStartDate.ID = "ibEditStartDate_" + this_site_id;
pFullInfo_lStartDateStr.Controls.Add(ibEditStartDate);
TextBox tbEditStartDate = new TextBox();
pFullInfo_lStartDateStr.Controls.Add(tbEditStartDate);
Image iEditStartDateCalendar = new Image();
iEditStartDateCalendar.ID = "iEditStartDateCalendar";
iEditStartDateCalendar.ImageUrl = "~/i/small/calendar.png";
iEditStartDateCalendar.ImageAlign = ImageAlign.AbsMiddle;
pFullInfo_lStartDateStr.Controls.Add(iEditStartDateCalendar);
CalendarExtender ceEditStartDate = new CalendarExtender();
ceEditStartDate.ID = "ceEditStartDate_" + this_site_id;
ceEditStartDate.PopupButtonID = iEditStartDateCalendar.UniqueID;
ceEditStartDate.TargetControlID = tbEditStartDate.UniqueID;
ceEditStartDate.PopupPosition = CalendarPosition.Right;
pFullInfo_lStartDateStr.Controls.Add(ceEditStartDate);

Normally, when adding the calendar extender in markup, you'd just set the PopupButtonID and TargetControlID to the ID of those controls, not the UniqueID.
When adding things like Labels dynamically, you set the AssociatedControlID to the ID of the control, not the UniqueID/ClientID and the framework works it out at render time.
Also, most JavaScript libraries prefer you to use the actual id of the control, rather than the name attribute, so you should use ClientID instead.

I found the solution.
The problem was with the "UniqueID" I was passing.
The correct solution is here:
// StartDate
Label lStartDateSite = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDateSite);
Label lStartDate = new Label();
pFullInfo_lStartDateStr.Controls.Add(lStartDate);
ImageButton ibEditStartDate = new ImageButton();
ibEditStartDate.ID = "ibEditStartDate_" + this_site_id;
pFullInfo_lStartDateStr.Controls.Add(ibEditStartDate);
TextBox tbEditStartDate = new TextBox();
pFullInfo_lStartDateStr.Controls.Add(tbEditStartDate);
tbEditStartDate.ID = "tbEditStartDate_" + this_site_id;
Image iEditStartDateCalendar = new Image();
iEditStartDateCalendar.ID = "iEditStartDateCalendar";
iEditStartDateCalendar.ImageUrl = "~/i/small/calendar.png";
iEditStartDateCalendar.ImageAlign = ImageAlign.AbsMiddle;
pFullInfo_lStartDateStr.Controls.Add(iEditStartDateCalendar);
CalendarExtender ceEditStartDate = new CalendarExtender();
ceEditStartDate.ID = "ceEditStartDate_" + this_site_id;
ceEditStartDate.PopupButtonID = iEditStartDateCalendar.ID;
ceEditStartDate.TargetControlID = tbEditStartDate.ID;
ceEditStartDate.PopupPosition = CalendarPosition.Right;
pFullInfo_lStartDateStr.Controls.Add(ceEditStartDate);
So what I did basicaly, I assigned an ID's to the TextBox and Image which are been used for the calendar, and that worked :]
Many thanks to Stack Overflow :]

Related

Dynamic hyperlink from code behind asp.net

I want dynamic hyperlink on each field in a table column from code behind in asp.net, I implement it thus:
table.Append("<td><asp:HyperLink ID='HyperLink1' NavigateUrl='#' runat='server'>" + (string)strNAME + "</asp:HyperLink></td>");
on the field but when I run it there is no link to click. it is not effective. what is the correct way of implementing it?
You need to approach this in a different way. Server side controls cannot be added as string literals, they should be objects. So what you can do is either add it as a server side control:
HyperLink hl = new HyperLink();
hl.ID = "HyperLink1";
hl.NavigateUrl = "#";
hl.Text = (string)strNAME;
TableCell tc = new TableCell();
tc.Controls.Add(hl);
table.Controls.Add(tc);
Or add it as a client side link:
table.Append("<td><a href='#'>" + (string)strNAME + "</a></td>");
Side note: adding table cell to "table" kind of does not make sense because there supposed to be a row, not a table, but I just left your code as is, adjust as needed.
Creating hyperlink from code-behind
HyperLink hlnk = new HyperLink();
hlnk.InnerText = (string)strNAME;
hlnk.ID = "HyperLink1";
hlnk.NavigateUrl = "/test.aspx";
table.Controls.Add(hlnk);
hope it helps

How to add rows and columns to a table in asp.net dynamically in button

I intend to create a dynamic table with the proviso that every time I click on the button I can add a row to that table.
I'm using the following code but it does not work.
For the first time I click on the button runs, but for the second time that must be added the second row it does not do so, and only one row is added to the previous.
In page
DropDownList[] dlsathetasilat = new DropDownList[50];
Label[] lblradif = new Label[50];
TextBox[] txtreshtetahsili = new TextBox[50];
TextBox[] txtmoasese = new TextBox[50];
TextBox[] txtcity = new TextBox[50];
TextBox []txtdateakhz = new TextBox[50];
CheckBox[] chmadrakmoadel = new CheckBox[50];
TableCell []tc = new TableCell[7];
static int i=0;
In button :
TableRow tr = new TableRow();
lblradif[i] = new Label();
lblradif[i].Text = (i + 1).ToString();
lblradif[i].CssClass = "lbl";
dlsathetasilat[i] = new DropDownList();
dlsathetasilat[i].Items.Add("دیپلم");
dlsathetasilat[i].CssClass = "dl";
txtreshtetahsili[i] = new TextBox();
txtreshtetahsili[i].CssClass = "dl";
txtmoasese[i] = new TextBox();
txtmoasese[i].CssClass = "dl";
txtdateakhz[i] = new TextBox();
txtdateakhz[i].CssClass = "dl";
txtcity[i] = new TextBox();
txtcity[i].CssClass = "dl";
chmadrakmoadel[i] = new CheckBox();
chmadrakmoadel[i].CssClass = "d2";
for (int k = 0; k < 7; k++)
tc[k] = new TableCell();
tc[0].Controls.Add(lblradif[i]);
tc[1].Controls.Add(dlsathetasilat[i]);
tc[2].Controls.Add(txtreshtetahsili[i]);
tc[3].Controls.Add(txtmoasese[i]);
tc[4].Controls.Add(txtcity[i]);
tc[5].Controls.Add(txtdateakhz[i]);
tc[6].Controls.Add(chmadrakmoadel[i]);
for (int j = 0; j < 7; j++)
tr.Controls.Add(tc[j]);
Table1.Rows.Add(tr);
i++;
This is the issue of PostBack and ViewState. ASP.NET does not save the ViewState for Table control. Which means if you want to keep the table data during the post back, you need to bind the table on every post back.
What you can do is save the table values in a ViewState object. Then write a function which creates the table by using the ViewState object. In the on click event of the button, add additional rows to the view state object and then call the function again to recreate the table. This looks quite simple, but the main problem is that if you have additional controls in the table, like TextBox, DropDownList etc. then they do not retain the values after postback, becuase we are recreating the table every time.
By looking at all these problems, I would consider using DataBound controls like Repeater with which you can build the table based on the number of rows that you want to create. Also you can deal with the dynamic controls if you want.

Dynamic Textbox Using Asp.net

in asp.net TextBox will create pressure to button1's. Button2 to the pressure inside the TextBox data consisting of label1 my yazdırıca. I tried to do it like this gives an error.
Object reference not set to an instance of an object.
button1_click{
TextBox txt = new TextBox();
txt.ID = "a";
txt.EnableViewState = true;
Panel1.Controls.Add(txt);
}
Button2_click{
TextBox deneme= Panel1.FindControl("a") as TextBox;
Label1.Text = deneme.Text;
}
After you create your control, on the first click, the control shown on the page and exist.
On the second click the page is not know any more about that text control because you did not save that information somewhere and because only you know that some time in the past you create it.
So on this code you have:
Button2_click{
TextBox deneme= Panel1.FindControl("a") as TextBox;
// here the deneme is null ! and you get the exception !
// the deneme is not exist on the second click, not saved anywhere
Label1.Text = deneme.Text;
}
The solution is to keep on viewstate what control you create and how, and re-create them on PageInit. Alternative you can redesign your page and think a different approach to that, eg you can have the TextControl's all ready on page hidden, and just open them.
Button2_click{
TextBox txt = (TextBox)Panel1.FindControl("a");
Label Label1 = new Label();
Label1.Text=txt.Texxt;
}

A Panel Control doesn't appear in a GridView Column

one of the GridView Column has to store a Panel Control (with some Controls inside). The problem is, that code doesn't work, I mean, the Panel doesn't appear inside the column.
Panel myPanel = new Panel();
LinkButton zatw = new LinkButton();
zatw.CommandName = "Accept";
zatw.Text = "Accept";
LinkButton odrz = new LinkButton();
odrz.CommandName = "Deny";
odrz.Text = "Deny";
myPanel.Controls.Add(zatw);
myPanel.Controls.Add(odrz);
DataTable DT = new DataTable();
DT.Columns.Add("Options", typeof(Panel));
DataRow myRow = DT.NewRow();
myRow1[0] = myPanel;
DT.Rows.Add(myRow1);
GridView1.DataSource = DT;
GridView1.DataBind();
...
That's because DT.Columns.Add("Options", typeof(Panel)); won't accept a control type as the second argument.
From the documentation. DT.Columns is of type
DataColumnCollection
that, indeed, has a method Add(String, Type) as you used it. But the Type is the column data type... it does not accept a control.
Example:
Private Sub AddColumn()
Dim columns As DataColumnCollection = _
DataSet1.Tables("Orders").Columns
Dim column As DataColumn = columns.Add( _
"Total", System.Type.GetType("System.Decimal"))
column.ReadOnly = True
column.Unique = False
End Sub
In this example, a column of name "Total" and type "decimal" is being created.
I can't really tell what you are trying to do exactly but I don't think you can create a grid out of controls in this manner. Why don't have your grid use a template column and then adjust the template based on the data you bind to instead of binding to a prebuilt UI like you are trying to do?

How to create line breaks between dynamically generated labels in a placeholder?

This is the code below in code behind file's Page_Load event:
LinkButton linkButton = new LinkButton();
linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i;
linkButton.ForeColor = Color.Blue;
linkButton.Font.Bold = true;
linkButton.Font.Size = 14;
linkButton.Font.Underline = false;
linkButton.Text = itemList[i].ItemTitle.InnerText;
linkButton.Click += new EventHandler(LinkButton_Click);
linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText);
PlaceHolder1.Controls.Add(linkButton);
Label label = new Label();
label.ID = "LabelDynamicInPlaceHolder1Id" + i;
label.ForeColor = Color.DarkGray;
label.Text = itemList[i].ItemDescription.InnerText;
PlaceHolder1.Controls.Add(label);
I want a line break between each control generated.
Solution to your Line Break issue is below however, if you're doing this in the Page_Load event, then your event handlers won't work and your going to run into Page Life Cycle issues. Basically, in order for your event handlers to fire on PostBack, you really need to be creating these dynamic controls earlier in the Page Life Cycle. Try moving your code to the OnInit method if you do run into this problem.
LinkButton linkButton = new LinkButton();
linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i;
linkButton.ForeColor = Color.Blue;
linkButton.Font.Bold = true;
linkButton.Font.Size = 14;
linkButton.Font.Underline = false;
linkButton.Text = itemList[i].ItemTitle.InnerText;
linkButton.Click += new EventHandler(LinkButton_Click);
linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText);
PlaceHolder1.Controls.Add(linkButton);
//Add This
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
Label label = new Label();
label.ID = "LabelDynamicInPlaceHolder1Id" + i;
label.ForeColor = Color.DarkGray;
label.Text = itemList[i].ItemDescription.InnerText;
PlaceHolder1.Controls.Add(label);
Another solution is that you could add each control to a Panel, which will render them each in a <div> resulting in the effect you're looking for.
To me this would be more dynamic because if you hide any of the controls the div will collapse and not leave empty lines.
LinkButton linkButton = new LinkButton();
linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i;
linkButton.ForeColor = Color.Blue;
linkButton.Font.Bold = true;
linkButton.Font.Size = 14;
linkButton.Font.Underline = false;
linkButton.Text = itemList[i].ItemTitle.InnerText;
linkButton.Click += new EventHandler(LinkButton_Click);
linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText);
//Add control to a panel, add panel to placeholder
Panel lbPan = new Panel();
lbPan.Controls.Add(linkButton);
PlaceHolder1.Controls.Add(lbPan);
Label label = new Label();
label.ID = "LabelDynamicInPlaceHolder1Id" + i;
label.ForeColor = Color.DarkGray;
label.Text = itemList[i].ItemDescription.InnerText;
//Add control to a panel, add panel to placeholder
Panel lblPan = new Panel();
lblPan.Controls.Add(label);
PlaceHolder1.Controls.Add(lblPan);
How to: Add Controls to an ASP.NET Web Page Programmatically
In some instances, you might want to
create both static text and controls.
To create static text, you can use either a Literal or a Label Web server
control. You can then add these
controls to the container as you would
any other control. For information
about view state in controls created
at run time, see Dynamic Web Server
Controls and View State.

Resources