Calendar.aspx.cs Cell.Text is duplicating - c#

We're trying to populate a Calendar for our Installation Team that shows distinct values from a query of Van Number and Install Time on each date in the calendar. We have it so there's a clickable Date in each cell that does something, and below that a Text String shows up with the Van # and Time. However this is duplicating.
We thought it was the String, but if we make build the same string and write it to the ToolTip for that Cell, it displays correctly. So it's that cell text for some reason.
Here's the relevant code:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
String Install = "";
String ToolTip = "";
Literal insta = new Literal();
Literal lit = new Literal();
e.Cell.BackColor = System.Drawing.Color.SkyBlue;
var rows = (from row in socialEvents.AsEnumerable()
where DateTime.Parse(row["InstallDate"].ToString()) >= e.Day.Date &&
DateTime.Parse(row["InstallDate"].ToString()) < e.Day.Date.AddDays(1)
select new
{
VanNum = row["VanNum"],
InstallTime = row["InstallTime"]
}).Distinct();
foreach (var row in rows)
{
String MyInstall = "";
MyInstall = "Van #: " + row.VanNum.ToString() + " / Install Time: " + row.InstallTime.ToString();
Install = Install + "<br/>" + MyInstall;
ToolTip = ToolTip + "/n" + MyInstall;
}
e.Cell.ToolTip = ToolTip;
lit.Visible = true;
e.Cell.Controls.Add(lit);
insta.Text = Install;
insta.Visible = true;
e.Cell.Controls.Add(insta);
}
e.Cell.ToolTip will display 3 rows, but the Literal insta.Text in the Cell Control will show it twice. The Literal lit seems to be adding the clickable Date (which I admit, I don't know how its doing that) which is why there's two Literal Controls. If we just write a single Literal Control then we lose the clickable Date but the Install data is still duplicated.

After testing more we found that the Control with text was being added twice. Not sure why we didn't see the date twice as well. But we found the answer here:
https://forums.asp.net/t/453021.aspx?My+DayRender+event+is+happening+twice+and+I+don+t+know+why+
The DayRender event was firing twice, so removing
OnDayRender="Calendar1_DayRender"
from the ascx page removed the redundant firing and text only displayed once. I'm still unsure why the Date gets added to lit and only once, but that seems to be generated elsewhere and probably accounted for. This also explains the ToolTip working, because its getting set where the Control is being added.

Related

First Character goes to the end of the text when I type in TextBox

I have a TextBoxnamed PercentageText. I used TextChanged event to Append "%" to text typed inside the TextBox. The code inside the TextChanged event is given below
if (skipTextChange)
skipTextChange = false;
else
{
skipTextChange = true;
if (PercentageText.Text =="")
{
PercentageText.Text = " ";
}
if (PercentageText.TextLength == 1)
{
if (PercentageText.Text != "%")
{
PercentageText.Text =""+ PercentageText.Text.Trim() + "%";
}
}
}
and initiallized SkipTextChange=false; out side the TextChanged Event Block. My Problem is When I Type Anything the first character goes all the way to the end of the text, for an example, if I type 152 it Shows 521 and When I cleared the TextBox using keyboard(Back Space key), and Type again it works Perfactly.
Instead of going for all this troubles I suggest you to simply add a label to the right of the textbox and put a % as the label's text.
However, if you really want to go for the TextChanged path then you need to test if your input ends with the % char and add it only if not. Also you need to set the position where the next char should be typed.
if (skipTextChange)
skipTextChange = false;
else
{
skipTextChange = true;
if (PercentageText.Text == "")
{
PercentageText.Text = " ";
}
if (!PercentageText.Text.EndsWith("%"))
{
PercentageText.Text = "" + PercentageText.Text.Trim() + "%";
PercentageText.SelectionStart = PercentageText.TextLength - 1;
}
}
Consider to test extensively with this approach. Copy/Paste, Delete and BackSpace behavior should be verified and insertion of multiple spaces or with the case of a % char typed directly by the user. Of course, if this textbox is supposed to contain only numbers a more complex verification code is required. If this is the context then I suggest to use NUmericUpDown control and the label trick to its right.

Make Part of table cell text bold C# asp.net

I have a following code:
DataSourceSelectArguments sr = new DataSourceSelectArguments();
DataView dv = DurationSQL.Select(sr) as DataView;
if (dv.Count != 0)
{
GridView2.Rows[0].Cells[0].Text = "Duration: \r" + dv[0][0].ToString() + "\r|";
}
I would like to make the static text:
Duration:
Displayed in bold while the rest of the text has no styling applied any way to achieve this?
You can put HTML into the cell text to achieve this, while at the same time preventing that HTML from being HtmlEncoded, like this:
Put HTML into the cell:
GridView2.Rows[0].Cells[0].Text = "<b>Duration:</b><br />"
+ HttpUtility.HtmlEncode(dv[0][0].ToString()) + "<br />|";
I included a call to .HtmlEncode(), but if the value is e.g. a number you may even skip that.
Prevent HTML from being encoded (use it for your first column, based on the fact that you use Cells[0]):
<asp:BoundField DataField="YourColumn" HtmlEncode="False" />
string text = GridView1.Rows[0].Cells[0].Text;
var span1 = new HtmlGenericControl("span");
span1.InnerHtml = "<strong>Duration:</strong> \r" + text;
GridView2.Rows[0].Cells[0].Text = span1.InnerHtml;
Seemed to work thanks for the comment

How to dynamically add new line to a label c#

I'm trying to print random dates in the format of Date-newline-Date-newline etc but dynamically add the dates from some c# code.
The current code I am using is shown below prints dates in one line with a space instead of a newline.
Here is my current code
private void WriteDates(int NumberOfDates)
{
string dates = "";
for(int i = 0 ; i < NumberOfDates; i++)
{
var print = RandomDay().ToShortDateString().ToString();
dates += print + "\n";
}
LblDate.Text = dates;
}
\n is a special "line feed" character that doesn't create a line break in the browser's rendering of the HTML. It will create a line break in the HTML file itself, but the browser doesn't take this into consideration when it renders out the markup.
Look at the source of the page,(Chrome -> Developer tools) and you will see the line break within the Label element..
Try br tag instead.
Try to avoid for loop where you can use LINQ:
private void WriteDates(int NumberOfDates)
{
LblDate.Text = string.Join(string.Empty,
Enumerable.Range(0, NumberOfDates)
.Select(n => $"{RandomDay().ToShortDateString()}<br />"));
}

Trackbar Percent to richtextbox Text

i have a Trackbar and want it to add the Current Value to a richtextbox Text without replacing the whole Text Line
richTextBox1.Rtf = richTextBox1.Rtf.Replace("aimbot_aimtime=85.000000", "aimbot_aimtime=" + trackbarpercent.Text + ".000000");
(i get the Value from my Label)
Thats what im using right now but it only Replaces it if the Text is "aimbot_aimtime=85.000000"
i want it to add the new Value after "aimbot_aimtime=NEWVALUE" but i cant get it to work atm
#Marc Lyon
I think a better way for me is to Replace the Line itself cause its always Line 7
Got it working, thanks to all who helped :)
void changeLine(RichTextBox RTB, int line, string text)
{
int s1 = RTB.GetFirstCharIndexFromLine(line);
int s2 = line < RTB.Lines.Count() - 1 ?
RTB.GetFirstCharIndexFromLine(line + 1) - 1 :
RTB.Text.Length;
RTB.Select(s1, s2 - s1);
RTB.SelectedText = text;
}
private void trackbarpercent_Click(object sender, EventArgs e)
{
changeLine(richTextBox1, 7, "aimbot_aimtime=" + aimtimetrackbar.Value + ".000000");
}
You have to know what the value is in order to replace it, which is why it only works when the value is your default value of 85.
In order to replace the text with the new text, you will have to track the previous value somewhere to use in your replacement. This means a field in your form, a property in some class. Let's say you create an int field on your form (myForm) called oldAimbot_aimtime. Every time the slider changes, put old value into this field. now your code becomes:
var prompt = "aimbot_aimtime=";
var oldvalue = string.Format("{0}{1}", prompt, myForm.oldAimbot_aimtime);
var newvalue = string.Format("{0}{1}", prompt, {NEWVALUE}.Format("#.######");
richTextBox1.Rtf = richTextBox1.Rtf.Replace(oldvalue, newvalue);
This code is off the top of my head and may not work exact, but it should replace the value. What is the value of using a richtextbox on a config screen? Can you post a screenshot?
OK, I see the screenshot. Ethics aside (not sure there is such a thing as a legit aimbot). You are using the richtextbox presumably because it was the easiest control for you to style...
Where you use the richtextbox is probably better suited to a GridView, ListBox, maybe even a treeview where you have finer control over each element.
If you want to use the richtext, write code which emits each option, then you can obtain exact values to use in rtf.Replace()commands
Hope this helps.

Use string variable content as label ID to update label.Text, I get error - 'string' does not contain a definition for 'Text'

C# newbie so be gentle! Here is the code creating a string using the arguments from a button to match a label id so I can update the labels text.
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] {','}); //Convert the buttons arguments to server/service variables
string strServerName = commandArgs[0];
string strServiceName = commandArgs[1];
string strLabelID = String.Format(strServerName + "_" + strServiceName + "_" + "Status"); //assign the strLabelID to the format: "servername_servicename_Status" for updating the label text
This works when used directly as the Label ID name is "serverx_spooler_Status"...
serverx_spooler_Status.Text = String.Format(strServiceName); //update label text
This fails even though the value of "strLabelID" is "serverx_spooler_Status"...
strLabelID.Text = String.Format(strServiceName); //update label text
Thank you Derek for the direction to search into! The solution was this...
// Find control on page.
Control myControl1 = FindControl(strLabelID);
Label myLabel1 = (Label)myControl1;
myLabel1.Text = "Updated Label Text!";
string service = "winmgmt";
string server = "DFS5600";
string labelText = string.Format("{0}_{1)_Status", server, service);
foreach (Control ctr in this.Controls)
{
if (ctr is Label)
{
if (ctr.Name == labelText)
{
ctr.Text = "Hello Label";
}
}
}
The type of serverx_spooler_Status is possibly a Label (not shown in question) that has a Text field, so serverx_spooler_Status.Text is valid.
The type of strLabelID is string (first inclusion), which does not have a Text field, so access to strLabelID.Text is invalid
try:
strLabelID = String.Format(strServiceName);
This will change the value of strLabelID to that of strServiceName (essentially same as: strLabelID = strServiceName;)
If you actually want to update a label, you will need an object of type Label, where you can access the Text field and update that (just lie you are doing with serverx_spooler_Status). Your code inclusions do not show if you have any other label objects you could use.
I think this is what you are looking for :-
Label.Text = String.Format("{0}_{1}_Status",strServerName,strServiceName);
That should work.
Or you could say :-
string strLabelID = String.Format("{0}_{1}_Status",strServerName,strServiceName);
label1.Text = strLabelID;
Not quite sure what you mean. Hope this helps.
I think this may help.
What you will need to do is loop through all labels in your project until you find a match like this :-
string strLabelID = String.Format("{0}_{1}_Status",strServerName,strServiceName);
foreach ( Control ctr in this.Controls)
{
if (ctr is Label)
{
if (ctr.Name == strLabelID)
{
//Do what ever in here
}
}
}

Categories