I found a bug on my site and I can't seem to find a solution. I am dynamically generating controls, two of which are a RadDatePicker and a RadTimePicker. When different events occur on the page, I am changing the visibility of the controls between true and false after generating them (essentially based on whether the page is in 'edit' mode or view mode). However, after the first instance where the controls are visible, the controls lose their styles. Refer to the screenshot below:
If anyone has any suggestions, or advice, that'd be great. Thanks
Update: Here is the code as requested
This hides and shows controls(just change true/false)
for (int i = 0; i < customProperties.Rows.Count; i++)
{
customProperties.FindControl("CustomControl" + (i + 1).ToString()).Visible = false;
customProperties.FindControl("lblCustomControl" + (i + 1).ToString()).Visible = true;
Type aType = customProperties.FindControl("CustomControl" + (i + 1).ToString()).GetType();
if (aType.Name == "RadBinaryImage")
customProperties.FindControl("CustomControl" + (i + 1).ToString() + "_btn").Visible = false;
}
So basically it searches the htmltable (customProperties) for controls and changes the visibility
This is where the control is generated:
case DynamicFieldTypeEnum.Date:
RadDatePicker dp = new RadDatePicker();
dp = currentValues as RadDatePicker;
if (dp.SelectedDate == null)
customProperty.Text = "Not Specified";
else
customProperty.Text = dp.SelectedDate.Value.ToString(dp.DateInput.DateFormat);
dp.ID = "CustomControl" + (position + 1).ToString();
newControls[0] = customProperty;
newControls[1] = dp;
break;
case DynamicFieldTypeEnum.Time:
RadTimePicker tp = new RadTimePicker();
tp = currentValues as RadTimePicker;
if (tp.SelectedDate == null)
customProperty.Text = "Not Specified";
else
customProperty.Text = tp.SelectedDate.Value.ToString(tp.DateInput.DateFormat);
tp.ID = "CustomControl" + (position + 1).ToString();
newControls[0] = customProperty;
newControls[1] = tp;
break;
This is within a method that is given an object definition and then returns a control array. The first value in the control array is a label for "view" mode and the second control is a control for "edit" mode.
This array is then passed to a different method which places the controls within a table cell which is placed in a table row and then placed in the table "customProperties"
I have already experienced this kind of problem.
As a suggestion of Telerik Support, I added this code in the pages I found your problems.
<telerik:RadStyleSheetManager ID="RadCssManager" runat="server">
<StyleSheets>
<telerik:StyleSheetReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Skins.RadDatePicker.css" />
<telerik:StyleSheetReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Skins.MetroTouch.RadDatePicker.MetroTouch.css" />
</StyleSheets>
</telerik:RadStyleSheetManager>
Ps: in my case I'm using the "MetroTouch" Skin.
Related
Right, so I have 13 textboxes with corresponding labels that are assigned after a user decides the name from a different form (instantiated by the 'Add field...' button). The issue arises when the user wishes to delete a textbox with previously entered data, as this results in an empty space where the textbox and label originally were as visualized by the following image:
My question is: how do I make it so that when a user chooses to delete a textbox, the textbox-label pair(s) that follow it replace the deleted textbox AND shift the remaining textboxes accordingly.
Textbox-label pairs in designer:
I've thought about this problem intensively over the past few days, and have concluded that with my current knowledge of C# I am limited to solving this issue with a horrendously tedious amount of if-statements (talking hundreds - thousands here). Any and all help would be appreciated!
Current code on the X-button for first textbox-label pair:
private void xButton1_Click(object sender, EventArgs e)
{
label14.Text = "";
textBox1.Text = "";
if (label14.Text.Equals(""))
{
label14.Visible = false;
textBox1.Visible = false;
xButton.Visible = false;
label14.Text = "";
textBox1.Text = "";
}
if (!textBox2.Text.Equals(""))
{
label14.Text = label15.Text;
textBox1.Text = textBox2.Text;
}
if (!textBox2.Text.Equals("") && (textBox3.Text.Equals("")))
{
label15.Visible = false;
textBox2.Text = "";
textBox2.Visible = false;
xButton2.Visible = false;
}
}
One simple thing you could do is give all your "dynamic" controls (label, textbox, button) a similar value in their Tag property (in my example, I used the string "dynamic" for all the control Tags. This enables you to query for them easily.
Next, you could follow the logic that, anytime you delete some controls, you move all controls below the deleted ones up a distance equal to the height of the control being deleted plus whatever padding you have between the controls.
For example, when a user clicks the X button, since you know the value of the Bottom of the control that's being deleted, you could find all controls that had a matching Tag property whose Top is greater than the x button Bottom, and you can move them up.
Here's an example (this assumes that all your X buttons are mapped to this same click event):
private void buttonX_Click(object sender, EventArgs e)
{
// This is represents the distance between the bottom
// of one control to the top of the next control
// Normally it would be defined globally, and used when you
// lay out your controls.
const int controlPadding = 6;
var xButton = sender as Button;
if (xButton == null) return;
var minTopValue = xButton.Bottom;
var distanceToMoveUp = xButton.Height + controlPadding;
// Find all controls that have the Tag and are at the same height as the button
var controlsToDelete = Controls.Cast<Control>().Where(control =>
control.Tag != null &&
control.Tag.ToString() == "dynamic" &&
control.Top == xButton.Top)
.ToList();
// Delete the controls
controlsToDelete.ForEach(Controls.Remove);
// Get all controls with the same tag that are below the deleted controls
var controlsToMove = Controls.Cast<Control>().Where(control =>
control.Tag != null &&
control.Tag.ToString() == "dynamic" &&
control.Top > minTopValue);
// Move each control up the specified amount
foreach (var controlToMove in controlsToMove)
{
controlToMove.Top -= distanceToMoveUp;
}
}
I've got a requirement where I need to be able to click dynamically generated div's
So,I've generated buttons also dynamically and want to link the ClickEvent of Button to div's Click event.
for (int i = 0; i < DtUsers.Rows.Count; i++)
{
ASPxButton btnButton = new ASPxButton();
btnButton.ID = "btnButton" + (i + 1);
btnButton.Visible = false;
btnButton.Click += new EventHandler(this.btnButton_Click);
btntButton.CommandArgument = DtOnlineUsers.Rows[i]["USER_ID"].ToString();
var divCustomItem = new HtmlGenericControl("div");
divUandMeUsers.Controls.Add(divCustomChatItem);
divCustomItem.Controls.Add(btnButton);
divCustomItem.Attributes.Add("onclick", "document.getElementById('<%= " + btnButton.ClientID + " %>').click()");
}
But,it is not working. Can you tell me where I've done wrong?
Is there something I need to do with the UpdatePanel AsyncPostBackTrigger?
When I click the div, in the developer tools, it is saying Uncaught TypeError: Cannot read property 'click' of null
Change
divCustomItem.Attributes.Add("onclick", "document.getElementById('<%= " + btnButton.ClientID + " %>').click()");
to
divCustomItem.Attributes.Add("onclick", "document.getElementById('" + btnButton.ClientID + "').click()");
You don't need the client side server includes during a server side operation.
Edit: OK so I've gone through your code with a finer toothed comb and I've found these additional problems:
The button's visibility is set to false so it won't even be rendered. If you want it to be clickable then you will need to hide it using css visibility.
What is an ASPxButton? Did you mean Button?
As there is nothing in the div its auto height and width will be set to zero.
Try sorting out those issues and post your new code if this still does not work.
Please help me understand what is this error that I'm getting:
lblTabCounter is a label coded in the aspx page while the lblc[index] is a collection of label created at runtime during page load.
Declaration outside of page load:
Label[] lblc = new Label[10];
Inside Page Load Event:
for (int i = 0; i < 10; i++)
{
lblc[i] = new Label() { Text = (i + 1).ToString() };
this.Controls.Add(lblc[i]);
}
Inside another event called NodeChanged:
int TabCount = Convert.ToInt32(lblTabCounter.Text.ToString());
int TabIndex = Convert.ToInt32(lblTabCounterIndex.Text.ToString());
if(TabCount <= 10)
{
divcont.Visible = true;
string tabName = getURLName(uRL);
MenuItem myItem = new MenuItem(tabName, TabIndex.ToString());
Menu1.Items.AddAt(TabIndex, myItem);
//f1.Attributes["src"] = url;
f1.Attributes.Add("src", lblURL.Text.ToString());
MultiView1.ActiveViewIndex = TabIndex;
lblc[TabCount].Text = lblTabCounter.Text;
lblc[TabCount + 1].Text = lblURL.Text;
TabCount++;
TabIndex++;
lblTabCounter.Text = TabCount.ToString();
lblTabCounterIndex.Text = TabIndex.ToString();
tvPermissions.ExpandAll();
//tvPermissions.CollapseAll();
int i = ctr;
}
Note: This are all inside site.master.
The problem is your web page is refreshing and losing the state of the labels.
Label[] lblc = new Label[10];
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
lblc[i] = new Label();
this.Controls.Add(lblc[i]);
if (Session["lblc" + i.ToString()] == null)
Session["lblc" + i.ToString()] = lblc[i].Text = (i + 1).ToString();
else
lblc[i].Text = (string)Session["lblc" + i.ToString()];
}
Then when you want to set a label you use the following (when the page is not being refreshed by the event)
lblc[4].Text = "cool";
Session["lblc4"] = "cool";
However because your click event is refreshing the page it loses contact with the lblc so you only set the Session so upon refresh you will see your new Label. (when the page is being refreshed by the event)
Session["lblc4"] = "cool";
The page is in the process of refreshing as a result of your particular event so the label disappears but the session state remains so when you set the session upon refresh the code grabs the session instead of setting it to the default number.
Rather than change the text of the label when it refreshes you are actually generating the new label with the session string you set.
Also make sure you have <sessionState mode="InProc" /> in your Web.config file under <system.web>
Please read more on Session States here http://msdn.microsoft.com/en-us/library/87069683(v=vs.80).aspx
There are two possible issues with that line of code:
lblc[TabCount] is null.
lblTabCount is null.
Since you are paused in the debugger, you can see which of those is the case, then look around in the rest of the code to find out why.
I would follow the path of the lblc[index] array to determine if the element offset is within range as well as it being created properly and not ending up there as a null (whether the null being the object lblc[index] or the text property) being referenced.
I am adding the dynamically TextBox in the placeholder on the button click in that.
When all the textboxes are loaded I am making changes in the values and again Press another Button to save the values to the SharePoint List. But when I press the Save button and I checked the placeholder by debugging it I found that there were no any control in the placeholder.
I am adding the Controls like follows :
TextBox[] tb = new TextBox[item_ans.Count];
Literal[] lt = new Literal[item_ans.Count];
for (int j = 0; j < item_ans.Count; j++)
{
ans_id.Add(item_ans[j]["ID"].ToString());
tb[j] = new TextBox();
tb[j].ID = "tb_ans" + (j + 1).ToString();
tb[j].Text = item_ans[j]["Title"].ToString();
lt[j] = new Literal();
lt[j].Text = "<br/>";
pl_hd_ans.Controls.Add(tb[j]);
pl_hd_ans.Controls.Add(lt[j]);
}
And on the Save Button click I am Retrieving those TextBoxes Like follows:
int n = Convert.ToInt32(ViewState["totalAns"].ToString());
foreach (var i in ans_id)
{
var item_ans = list_ans.GetItemById(i);
clientContext.Load(item_ans);
clientContext.ExecuteQuery();
for (int k = 0; k < n; k++)
{
TextBox tb = (TextBox)pl_hd_ans.FindControl("tb_ans" + (k + 1).ToString());
item_ans["Title"] = tb.Text;
item_ans.Update();
clientContext.Load(item_ans);
clientContext.ExecuteQuery();
}
}
But in this I check the Placeholder's Controls that were 0. Can Any one please help me to solve this..?
I'm assuming its ASP.NET WebForms what we're talking about here.
When you are adding controls dynamically to the webpage, you have to recreate them on each sequential postback. The reason for this, is that the dynamically created controls are not present in the .aspx file, nor in the viewstate, so asp.net cannot know that it has to recreate these controls. Therefore, you yourself have to recreate them in the initialized-event (before the page-load), including adding any event handlers that you need.
You can google about it.
I have a loop that is supposed to go through DataTable and for each row create a new GroupBox, set it's text to value from one column, in that GroupBox I want to put a Label with Text similar to another column in the table.
This is just part of the code!
for (int i = 0; i < tab.Rows.Count; i++)
{
lblbox[i] = new GroupBox();
lblbox[i].Text = tab.Rows[i]["text"].ToString();
lblbox[i].Name = "box no " + i.ToString();
lblbox[i].Visible = true;
this.Controls.Add(lblbox[i]);
lblbox[i].Location = new Point(5, 55 * i);
lblbox[i].Height = 50;
lblbox[i].SendToBack();
importancelbl[i] = new Label();
importancelbl[i].Text = "Importance: " + tab.Rows[i]["importance"].ToString();
importancelbl[i].Name = "implbl" + i.ToString();
importancelbl[i].Visible = true;
lblbox[i].Controls.Add(importancelbl[i]);
importancelbl[i].BringToFront();
Point locP = new Point();
locP.X = lblbox[i].Location.X + 5;
locP.Y = lblbox[i].Location.Y + 15;
importancelbl[i].Location = locP;
}
When i run the code it creates three (I have three rows in my table) GroupBoxes correctly and creates all the labels, but only first label is visible in its Groupbox. When I add those labels to the Form and not to the GroupBox, all of them are visible, but I want them to be in boxes...
I've tried pretty much everything and I'm still very confused (espacially by the behavior of the first label). I know the mistake is probably obvious and stupid, but I just can't find it!
Control.Location is relative to its parent, so set Location for the label to (5, 15).
locP.X = 5;
locP.Y = 15;
My guess is that they are somehow overlapping and making each other disappear somehow.
Could you try posting pictures of the form when it works and when it doesn't? Also add all your code?
Try to preform adding
lblbox[i].Controls.Add(importancelbl[i]);
this.Controls.Add(lblbox[i]);
after setting all your properties