Add html text to webcontrol Button in ASP codebehind file - c#

So I am looking for a way to add HTML text to a programmatically created button, or some other control that would accept HTML text and have a on_click_event to capture.
I have a table that I am building dynamically in the codebehind file based upon steps (attendance to a lesson, and the taking of a quiz) that are recorded as complete or incomplete in the database. If a step was not done, I entered a button with a simple X
If the step was done, I just added HTML month and day to the cell.
Attendance to the lesson is a step that can be repeated, so I wanted to make the lesson button look like the styling of a completed quiz, but still capture a 2nd+ attendance point with a click_event.
I am stumped in making a dynamically created button work with my HTML styling. My text style on an ASP.net webcontrol button in a codebehind file is being interpreted as literal text on that button.
Here is the code that fills the cells, either creates the buttons, or adds the text
if(lesson_completed == true)
{
DateTime date = Convert.ToDateTime(lesson);
Button markNewDateComplete = new Button();//System.Web.UI.WebControls.Button Class
markNewDateComplete.Text = "<font style='font-size:50%;color:silver;'>" + date.ToString("MMMM") + "</font><br><font style='color:light gray'>" + date.ToString("dd") + "<font>";
markNewDateComplete.CssClass = "mybtn";
markNewDateComplete.CommandName = lesson_detail_id.ToString();
markNewDateComplete.Click += new EventHandler(this.NewDateLesson_Click);
tc2x.Controls.Add(markNewDateComplete);
}
else
{
DateTime date = Convert.ToDateTime(quiz);
tc2x.Text = "<font style='font-size:50%;color:silver;'>" + date.ToString("MMMM") + "</font><br><font style='color:light gray'>" + date.ToString("dd") + "<font>";
}
Here is what the output looks like on-screen
There are many pages in SO that give insight how to do this in the regular ASPX page, but none that I could find to do this programmatically in a ASPX.cs codebehind file.
In programming there is always another way to do something; so I am looking for a way to add HTML text to a programmatically created button, or some other small control that would accept HTML text and have a on_click_event that I could use as a method back to SQL.
Example of what I want, but this is not for the codebehind file.Font awesome inside asp button

Related

Why label text is getting changed after clicking on asp.net button?

I have a WebForm ASP label and button. I am setting the label's value on page load. For example, the label text on page load is 2 items selected. This comes from the database. Then if the user changes the selection then it counts the selected values by jQuery and sets the text as 5 items selected.
When I click on the submit button to save changes, again it resets to 2 items selected. I didn't use an update panel. I don't know what is going on here. Can anyone please explain this scenario?
$("#lblCount").text($('#grdProducts').find('input#chkSelect:checked').length + ' Complementary Products added');
C# on page load:
lblCount.Text = ComplementaryproductCount.ToString() + " Complementary Products added";
I do not understand why the label text is changed on button click. I couldn't find anything while debugging too.
Thanks
When you set lblCount.Text in your code, that value is set into the ViewState of the page... that means when your page is posted back to the server (to handle an event, etc) ASP.Net knows what lblCount.Text was originally and can re-render the HTML with the same value.
As part of that post-back to the server, the browser will send back that ViewState along with any input control values (things like textboxes, dropdowns, hidden field).
What it does NOT do is post-back any changes you might have made to the elements on the page via things like jQuery (other than input controls I mentioned above).
The result is that although you've changed the element on the screen, the server knows absolutely nothing about that change, and it will re-send the original HTML for the label back to the browser.
Your only option is to do something as suggested by #John in his comment... you need to store the fact the element has changed in an input, and then use that.
For instance...
<asp:Label runat="server" id="lblCount" />
<asp:HiddenField runat="server" id="hdnCount" />
function updateCount(newCount) {
$("#<%=lblCount.ClientID%>").text("Count: " + newCount.toString());
$("#<%=hdnCount.ClientID%>").val(newCount.toString());
}
Then in your code-behind you can have...
if (!Page.IsPostBack)
{
var count = 1;
lblCount.Text = String.Format("Count: {0}", count);
hdnCount.Value = count.ToString();
}
else
{
lblCount.Text = String.Format("Count: {0}", hdnCount.Value);
}

Open a URL and pre-fill a textbox in an ASPX page from code-behind

So what I'm doing is a little different than this:
Access a control's property in an aspx page from another aspx page
because what I'm trying to do is "push" a value forward to the next page rather than "pull" it from the previous page.
What I need to do is open a new page from within an Intranet website application (actually, the page will have been previously used by the user) and pre-fill a textbox.
Can this be done? I'm currently forwarding to the new page using:
String SUrl = "frmAuditSearch.aspx";
Server.Transfer(SUrl, true);
What I also need to do is fill a textbox called txtAuditID on frmAuditSearch.aspx with the AuditID from the current page I'm on.
And yes, I know I can use something like:
String SUrl = "frmAuditSearch.aspx?AuditID = '" + MyAuditID + "'";
Server.Transfer(SUrl, true);
but I'm trying to avoid that because most of the time there's no AuditID and that'd require tinkering with too much code.
Sounds like you'd want to fill the text box with something like this.
txtAuditID.Text = (Request["AuditID"] ?? String.Empty).ToString().
This allows the absence of the parameter if needed

WebBrowser Control insert word in caret position

I have a WPF WebBrowser control where I need to insert some text (e.g. the selected item coming from a listbox). This way, if the editor contains "When " and the listbox contains "I, You, He" and I click on "You", the editor must contain "When you".
The code I'm using with the ActiveElement is working right now, but I'm finding some weird issues explained below.
HTMLDocument doc;
doc = webBrowser.Document as HTMLDocument;
doc.activeElement.innerHTML += " " + selectedItemFromListboxAsString + " ";
Then, the webbrowser gets appended the selectedItemFromListboxAsString properly, but when I continue typing after that, the new keystrokes go to the next line. In the sourcecode I can see that a </P> tag has been put after the selectedItemFromListboxAsString. No matter if I remove the </P> programmatically, it will insert the new typed content in a new line.
Is there any other way to achieve the same result instead of playing around with the activeElement?
Thanks in advance.

Programmatically Changing ASP.NET C# Tab Title Text

I am trying to change the Tab text when I navigate to a page. I have many many pages so I don't want to go to each page and add a title control. Instead I am trying to change the MasterPage.master.cs so I only have to add that one bit of code.
The following code is close to what I would like MasterPage.master.cs:
Page.Header.Title = this.Page.ToString():
This would display "ASP.shipping_aspx" (page name is Shipping.aspx) but I would like it to display "Shipping".
How can I programmatically change the tab title text so that it changes whenever I navigate to a new page?
string title = this.Page.ToString().Split('.')[1].Split('_')[0];
Page.Header.Title = char.ToUpper(title[0]) + title.Substring(1);

Get HTML text from page after scrolling

I am trying to capture HTML text from a page, in the case of this website more info is loaded dynamically as you scroll. So it is easy enough to capture the first page of text, but have do I get the next. I have the auto scroll part worked out fine like this:
string Java = "javascript:window.scroll(0,";//Builds up logic so screen will scroll by one page each time
string JavaScroll = Java + Scroll + ");";
webBrowser1.Navigate(JavaScroll);
Scroll += 3050;
String Morefriends = webBrowser1.DocumentText;
The problem is, even when I scroll, the Morefriends HTML text still remains the same as the first page, like it is not updating to reflect the HTML text from the new page that was just scrolled to.

Categories

Resources